import 'dart:convert'; import 'dart:io'; import 'package:crypto/crypto.dart'; import 'package:cutdex_sdk/editor.dart'; import 'package:path/path.dart' as p; /// A completed staging file and its digest are durable before publication. /// Only this operation's recorded output is eligible for recovery. final class ExportPublication { ExportPublication(this.destination, this.operationId); final Uri destination; final String operationId; File get output => File.fromUri(destination); Directory get directory => Directory( p.join( output.parent.path, '.cutdex-export-${sha256.convert(utf8.encode('$operationId|$destination'))}', ), ); File get staged => File(p.join(directory.path, 'output.mp4')); File get receipt => File(p.join(directory.path, 'receipt.json')); Future _digest(File file) async => (await sha256.bind(file.openRead()).first).toString(); Future _save(Map value) async { final temporary = File('${receipt.path}.tmp'); await temporary.writeAsString(jsonEncode(value), flush: true); await temporary.rename(receipt.path); } Future recover() async { if (!await receipt.exists()) return null; final value = (jsonDecode(await receipt.readAsString()) as Map) .cast(); if (value['operationId'] != operationId || value['destination'] != destination.toString()) { throw const CommandRejected( 'operationIdConflict', 'Export receipt belongs to another request', ); } if (value['sha256'] == null) return null; final candidate = await output.exists() ? output : staged; if (!await candidate.exists() || await candidate.length() != value['bytes'] || await _digest(candidate) != value['sha256']) { throw const CommandRejected( 'exportArtifactChanged', 'Export output is missing or changed; choose a new destination to retry', ); } if (candidate.path == staged.path) await staged.rename(output.path); return destination; } Future run(String fingerprint, Future Function(Uri) encode) async { if (await receipt.exists()) { final value = jsonDecode(await receipt.readAsString()) as Map; if (value['fingerprint'] != fingerprint) { throw const CommandRejected( 'operationIdConflict', 'Export snapshot changed', ); } } final recovered = await recover(); if (recovered != null) return recovered; if (await output.exists()) { throw const CommandRejected( 'destinationExists', 'Destination already exists', ); } if (!await output.parent.exists()) { throw const CommandRejected( 'invalidDestination', 'Destination directory does not exist', ); } if (await receipt.exists()) { throw const CommandRejected( 'exportInterrupted', 'Export was interrupted before publication; explicitly retry with a new operation', ); } await directory.create(); final record = { 'operationId': operationId, 'destination': destination.toString(), 'fingerprint': fingerprint, }; await _save(record); await encode(staged.uri); final handle = await staged.open(mode: FileMode.append); try { await handle.flush(); } finally { await handle.close(); } await _save({ ...record, 'bytes': await staged.length(), 'sha256': await _digest(staged), }); return (await recover())!; } }