Files
cutdex/lib/workspace/external_media_import.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

122 lines
3.8 KiB
Dart

import 'dart:io';
import 'package:cutdex_sdk/project.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';
import 'media_import.dart';
import 'projects.dart';
part 'external_media_import.g.dart';
part 'external_media_import.freezed.dart';
@freezed
abstract class ExternalMediaSelection with _$ExternalMediaSelection {
const factory ExternalMediaSelection({
required List<AssetDocument> selected,
MediaImportResult? result,
@Default(false) bool failed,
}) = _ExternalMediaSelection;
}
@riverpod
MediaFileResolver mediaFileResolver(Ref ref) => const MediaFileResolver();
/// Resolves aliases once for both the media browser and the attachment composer.
class MediaFileResolver {
const MediaFileResolver();
Future<Map<ExternalMediaFile, Set<String>>> resolve(
List<ExternalMediaFile> files,
List<AssetDocument> assets,
) async {
final locations = <ExternalMediaFile, Set<String>>{};
for (final file in files) {
final candidates = {file.sourceUri.toString()};
final path = file.localPath;
if (path != null &&
!assets.any(
(asset) => candidates.contains(mediaSourceLocation(asset)),
) &&
File(path).existsSync()) {
try {
candidates.add(
Uri.file(await File(path).resolveSymbolicLinks()).toString(),
);
} on FileSystemException {
// Unavailable sources are reported by the importer.
}
}
locations[file] = candidates;
}
return locations;
}
}
@riverpod
class ExternalMediaImport extends _$ExternalMediaImport {
@override
AsyncValue<ExternalMediaSelection?> build(String projectId) =>
const AsyncData(null);
Future<ExternalMediaSelection?> importFiles(
List<ExternalMediaFile> files,
) async {
if (state.isLoading || files.isEmpty) return null;
final workspace = ref.read(projectsProvider).value;
if (workspace?.activeProject?.id != projectId || workspace!.projectBusy) {
return null;
}
final lease = ref.keepAlive();
final projects = ref.read(projectsProvider.notifier);
final resolver = ref.read(mediaFileResolverProvider);
state = const AsyncLoading();
try {
final locations = await resolver.resolve(files, workspace.assets);
if (!ref.mounted ||
ref.read(projectsProvider).value?.activeProject?.id != projectId) {
return null;
}
final existing = <AssetDocument>[];
final missing = <ExternalMediaFile>[];
for (final file in files) {
final asset = workspace.assets
.where(
(asset) => locations[file]!.contains(mediaSourceLocation(asset)),
)
.firstOrNull;
if (asset == null) {
missing.add(file);
} else if (!existing.any((item) => item.id == asset.id)) {
existing.add(asset);
}
}
final result = missing.isEmpty
? null
: await projects.importExternalMedia(projectId, missing);
final importedLocations = {
for (final file in missing) ...locations[file]!,
};
final selected = <String, AssetDocument>{
for (final asset in existing) asset.id: asset,
for (final asset in result?.assets ?? <AssetDocument>[])
if (importedLocations.contains(mediaSourceLocation(asset)))
asset.id: asset,
};
final value = ExternalMediaSelection(
selected: selected.values.toList(),
result: result,
failed: missing.isNotEmpty && result == null,
);
if (ref.mounted) state = AsyncData(value);
return value;
} catch (error, stack) {
if (ref.mounted) state = AsyncError(error, stack);
rethrow;
} finally {
if (ref.mounted && state.isLoading) state = const AsyncData(null);
lease.close();
}
}
}