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
122 lines
3.8 KiB
Dart
122 lines
3.8 KiB
Dart
import 'dart:io';
|
|
|
|
import 'package:cutdex_core/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();
|
|
}
|
|
}
|
|
}
|