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
131 lines
4.3 KiB
Dart
131 lines
4.3 KiB
Dart
import 'dart:io';
|
|
import 'dart:ui' as ui;
|
|
|
|
import 'package:cutdex_core/project.dart';
|
|
import 'package:cutdex_native_flutter/cutdex_native_flutter.dart';
|
|
import 'package:flutter/foundation.dart';
|
|
import 'package:riverpod_annotation/riverpod_annotation.dart';
|
|
|
|
import '../../workspace/media_binding.dart';
|
|
import '../../workspace/projects.dart';
|
|
|
|
import 'package:flutter_riverpod/flutter_riverpod.dart'
|
|
show ProviderListenableSelect;
|
|
part 'media_thumbnails.g.dart';
|
|
|
|
@Riverpod(keepAlive: true)
|
|
MediaThumbnails mediaThumbnails(Ref ref) {
|
|
final projectId = ref.watch(
|
|
projectsProvider.select((s) => s.asData?.value.activeProject?.id),
|
|
);
|
|
return MediaThumbnails(
|
|
loadMedia: <T>(asset, action) => ref
|
|
.read(projectsProvider.notifier)
|
|
.withAssetMedia(projectId, asset, action),
|
|
);
|
|
}
|
|
|
|
/// Small runtime-generated browser covers, kept outside the project document.
|
|
class MediaThumbnails {
|
|
MediaThumbnails({required this.loadMedia});
|
|
final AssetMediaLoader loadMedia;
|
|
final _runtime = CutdexRuntime();
|
|
final _cache = <(String, int?, int), Future<Uint8List?>>{};
|
|
final _durations = <(String, int?), Future<double?>>{};
|
|
Future<void> _tail = Future.value();
|
|
|
|
Future<double?> duration(AssetDocument asset) {
|
|
final key = (asset.location, asset.byteLength);
|
|
if (_durations.containsKey(key)) return _durations[key]!;
|
|
if (_durations.length >= 64) _durations.remove(_durations.keys.first);
|
|
return _durations[key] = () async {
|
|
try {
|
|
final info = await loadMedia(asset, _runtime.probe);
|
|
final seconds = info.duration.numerator / info.duration.denominator;
|
|
return seconds.isFinite && seconds > 0 ? seconds : null;
|
|
} catch (_) {
|
|
return null;
|
|
}
|
|
}();
|
|
}
|
|
|
|
Future<Uint8List?> load(AssetDocument asset, {int timeMilliseconds = 0}) {
|
|
final key = (asset.location, asset.byteLength, timeMilliseconds);
|
|
final cached = _cache.remove(key);
|
|
if (cached != null) {
|
|
_cache[key] = cached;
|
|
return cached;
|
|
}
|
|
// Bound encoded cover retention and concurrent native generation.
|
|
if (_cache.length >= 64) _cache.remove(_cache.keys.first);
|
|
final future = _tail.then((_) async {
|
|
if (asset.metadata['kind'] == 'image') {
|
|
final image = await _imagePreview(asset);
|
|
if (image != null) return image;
|
|
}
|
|
try {
|
|
final thumbnail = await loadMedia(
|
|
asset,
|
|
(media) => _runtime.thumbnail(
|
|
ThumbnailRequest(
|
|
media: media,
|
|
time: MediaTime(timeMilliseconds, 1000),
|
|
),
|
|
),
|
|
);
|
|
return thumbnail.data;
|
|
} catch (_) {
|
|
return null;
|
|
}
|
|
});
|
|
_cache[key] = future;
|
|
_tail = future.then((_) {});
|
|
return future;
|
|
}
|
|
|
|
Future<Uint8List?> _imagePreview(AssetDocument asset) async {
|
|
if (kIsWeb) return null;
|
|
try {
|
|
return await loadMedia(asset, (media) async {
|
|
if (media.uri.scheme != 'file') return null;
|
|
final file = File.fromUri(media.uri);
|
|
if (!file.existsSync()) return null;
|
|
final bytes = await file.readAsBytes();
|
|
final buffer = await ui.ImmutableBuffer.fromUint8List(bytes);
|
|
try {
|
|
final descriptor = await ui.ImageDescriptor.encoded(buffer);
|
|
try {
|
|
final longestSide = descriptor.width > descriptor.height
|
|
? descriptor.width
|
|
: descriptor.height;
|
|
final scale = longestSide > 320 ? 320 / longestSide : 1.0;
|
|
final codec = await descriptor.instantiateCodec(
|
|
targetWidth: (descriptor.width * scale).round(),
|
|
targetHeight: (descriptor.height * scale).round(),
|
|
);
|
|
try {
|
|
final frame = await codec.getNextFrame();
|
|
try {
|
|
return (await frame.image.toByteData(
|
|
format: ui.ImageByteFormat.png,
|
|
))?.buffer.asUint8List();
|
|
} finally {
|
|
frame.image.dispose();
|
|
}
|
|
} finally {
|
|
codec.dispose();
|
|
}
|
|
} finally {
|
|
descriptor.dispose();
|
|
}
|
|
} finally {
|
|
buffer.dispose();
|
|
}
|
|
});
|
|
} catch (_) {
|
|
// Formats not handled by Flutter can still use the native thumbnailer.
|
|
return null;
|
|
}
|
|
}
|
|
}
|