Files
cutdex/lib/integrations/runtime/export_publication.dart
T
lingjhf 27e77fff39 feat: initial Cutdex application
Add the Flutter video editing application with project management, media
import, timeline editing, preview, AI assistance, diagnostics and settings.

Include shared UI and native host packages, platform runners, application
tests and release tooling.

Provide usage and contribution guides, the MIT license and third-party
notices. Exclude local credentials, build outputs and validation data.
2026-10-05 23:36:23 +08:00

109 lines
3.5 KiB
Dart

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<String> _digest(File file) async =>
(await sha256.bind(file.openRead()).first).toString();
Future<void> _save(Map<String, Object?> value) async {
final temporary = File('${receipt.path}.tmp');
await temporary.writeAsString(jsonEncode(value), flush: true);
await temporary.rename(receipt.path);
}
Future<Uri?> recover() async {
if (!await receipt.exists()) return null;
final value = (jsonDecode(await receipt.readAsString()) as Map)
.cast<String, Object?>();
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<Uri> run(String fingerprint, Future<Uri> 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 = <String, Object?>{
'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())!;
}
}