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: (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>{}; final _durations = <(String, int?), Future>{}; Future _tail = Future.value(); Future 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 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 _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; } } }