Gitea CI / Check code and tests (pull_request) Failing after 8s
Gitea CI / Build Android APK (pull_request) Skipped
Gitea CI / Build Windows x64 app (pull_request) Skipped
Gitea CI / Audit and package Windows x64 (pull_request) Skipped
Gitea CI / Assemble and validate CI artifacts (pull_request) Skipped
Gitea CI / Gitea CI passed (pull_request) Failing after 1s
109 lines
3.5 KiB
Dart
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())!;
|
|
}
|
|
}
|