Files
lingjhf 06f1c26681
Gitea CI / Assemble and validate CI artifacts (push) Successful in 1m47s
Gitea CI / Check code and tests (push) Successful in 7m15s
Gitea CI / Build Android APK (push) Successful in 4m6s
Gitea CI / Build Windows x64 app (push) Successful in 3m34s
Gitea CI / Audit and package Windows x64 (push) Successful in 1m29s
Gitea CI / Gitea CI passed (push) Successful in 1s
Gitea Release / Prepare draft Release (push) Failing after 8s
Gitea Release / Upload Android to draft Release (push) Skipped
Gitea Release / Upload Windows to draft Release (push) Skipped
Gitea Release / Verify and publish Release (push) Skipped
Move editor UI into the app and release verified Core dependencies (#14)
2026-10-11 19:59:40 +00:00

109 lines
3.5 KiB
Dart

import 'dart:convert';
import 'dart:io';
import 'package:crypto/crypto.dart';
import 'package:cutdex_core/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())!;
}
}