diff --git a/lib/l10n/arb/app_en.arb b/lib/l10n/arb/app_en.arb index aa747e15d..9b71436e3 100644 --- a/lib/l10n/arb/app_en.arb +++ b/lib/l10n/arb/app_en.arb @@ -3449,6 +3449,89 @@ "@libraryReEnrichAllBusy": { "description": "Snackbar when Re-enrich All is started during a Library scan" }, + "libraryFindArtwork": "Find Missing Artwork", + "@libraryFindArtwork": { + "description": "Library settings action - look up covers online for albums without one, kept in the app" + }, + "libraryFindArtworkSubtitle": "Look up covers for albums without one. Your music files stay unchanged.", + "@libraryFindArtworkSubtitle": { + "description": "Subtitle for the Find Missing Artwork action" + }, + "libraryFindArtworkCovers": "Missing Covers", + "@libraryFindArtworkCovers": { + "description": "Find Missing Artwork option - only covers for albums that have none" + }, + "libraryFindArtworkCoversSubtitle": "Albums whose songs have no cover", + "@libraryFindArtworkCoversSubtitle": { + "description": "Subtitle for the Missing Covers option" + }, + "libraryFindArtworkMotion": "Missing Covers and Animated Covers", + "@libraryFindArtworkMotion": { + "description": "Find Missing Artwork option - covers for albums without one plus animated covers for every album" + }, + "libraryFindArtworkMotionSubtitle": "Checks every album, so it takes longer", + "@libraryFindArtworkMotionSubtitle": { + "description": "Subtitle for the option that also looks up animated covers" + }, + "libraryFindArtworkRunning": "Finding artwork...", + "@libraryFindArtworkRunning": { + "description": "Title of the progress row while artwork is looked up" + }, + "libraryFindArtworkProgress": "{done} of {total} albums", + "@libraryFindArtworkProgress": { + "description": "Progress of the artwork lookup", + "placeholders": { + "done": { + "type": "int" + }, + "total": { + "type": "int" + } + } + }, + "libraryFindArtworkStopping": "Stopping...", + "@libraryFindArtworkStopping": { + "description": "Progress row text after the artwork lookup was stopped, while the last albums finish" + }, + "libraryFindArtworkNoProvider": "Enable a metadata extension to look up artwork.", + "@libraryFindArtworkNoProvider": { + "description": "Snackbar when no enabled extension can look up albums" + }, + "libraryFindArtworkNothing": "Every album already has its artwork.", + "@libraryFindArtworkNothing": { + "description": "Snackbar when no album needs a cover or animated cover" + }, + "libraryFindArtworkResult": "{covers, plural, =0{No new covers} =1{Found 1 cover} other{Found {covers} covers}}{motion, plural, =0{} =1{ · 1 animated cover} other{ · {motion} animated covers}}{failed, plural, =0{} =1{ · 1 album not checked} other{ · {failed} albums not checked}}", + "@libraryFindArtworkResult": { + "description": "Snackbar after Find Missing Artwork: covers found, animated covers found, then albums no extension could be asked about", + "placeholders": { + "covers": { + "type": "int" + }, + "motion": { + "type": "int" + }, + "failed": { + "type": "int" + } + } + }, + "albumFindArtwork": "Find Artwork", + "@albumFindArtwork": { + "description": "Local album menu action - look up this album's cover and animated cover online" + }, + "albumFindArtworkNotFound": "No matching album found", + "@albumFindArtworkNotFound": { + "description": "Snackbar when Find Artwork finds no provider album that matches" + }, + "albumRemoveFoundArtwork": "Remove Found Artwork", + "@albumRemoveFoundArtwork": { + "description": "Local album menu action - remove a cover and animated cover found online, for a wrong match" + }, + "albumRemoveFoundArtworkDone": "Removed. Find Missing Artwork leaves this album alone now.", + "@albumRemoveFoundArtworkDone": { + "description": "Snackbar after removing found artwork from an album" + }, "libraryFilterDownloaded": "Downloaded", "@libraryFilterDownloaded": { "description": "Filter chip - show only downloaded items" diff --git a/lib/providers/library_album_artwork_provider.dart b/lib/providers/library_album_artwork_provider.dart new file mode 100644 index 000000000..055dea925 --- /dev/null +++ b/lib/providers/library_album_artwork_provider.dart @@ -0,0 +1,262 @@ +import 'dart:async'; +import 'dart:io'; + +import 'package:flutter_riverpod/flutter_riverpod.dart'; +import 'package:path_provider/path_provider.dart'; +import 'package:mornye/providers/extension_provider.dart'; +import 'package:mornye/providers/local_library_provider.dart'; +import 'package:mornye/providers/player_motion_artwork_provider.dart'; +import 'package:mornye/providers/settings_provider.dart'; +import 'package:mornye/services/library_album_artwork.dart'; +import 'package:mornye/services/library_database.dart'; +import 'package:mornye/services/motion_artwork_store.dart'; +import 'package:mornye/services/platform_bridge.dart'; +import 'package:mornye/utils/logger.dart'; + +final _log = AppLogger('AlbumArtwork'); + +/// The most a found cover keeps on its longest side, as for scanned covers. +const _coverMaxDimension = 800; + +final libraryAlbumArtworkStoreProvider = Provider( + (ref) => LibraryAlbumArtworkStore( + database: () => LibraryDatabase.instance.database, + coverDirectory: () async => Directory( + '${(await getApplicationSupportDirectory()).path}/library_covers', + ), + ), +); + +/// The album of the Library song at a path, and what was found for it, for +/// the album's page to show a found cover and offer removing it. +final libraryAlbumArtworkForSongProvider = FutureProvider.autoDispose + .family<({String albumKey, AlbumArtworkRecord? record})?, String>(( + ref, + filePath, + ) async { + final store = ref.watch(libraryAlbumArtworkStoreProvider); + final albumKey = await store.albumKeyOf(filePath); + if (albumKey == null) return null; + final records = await store.records(albumKey: albumKey); + return (albumKey: albumKey, record: records[albumKey]); + }); + +/// A run of [AlbumArtworkJob] in progress, or its last result. +class LibraryAlbumArtworkState { + const LibraryAlbumArtworkState({ + this.running = false, + this.albumKey, + this.done = 0, + this.total = 0, + this.cancelling = false, + }); + + final bool running; + + /// The album a single-album run is for; null for the whole Library. + final String? albumKey; + final int done; + final int total; + final bool cancelling; +} + +/// Looks up artwork for Library albums in the background, so leaving the +/// page that started it does not stop it. +class LibraryAlbumArtworkNotifier extends Notifier { + bool _cancelled = false; + Future? _restoring; + + @override + LibraryAlbumArtworkState build() => const LibraryAlbumArtworkState(); + + MotionArtworkStore get _motion => ref.read(motionArtworkStoreProvider); + + Map _providers() => { + for (final extension in ref.read(extensionProvider).extensions) + if (extension.enabled && extension.hasMetadataProvider) + extension.id: extension, + }; + + /// Whether an enabled extension can look albums up. + bool get canLookUp => _providers().isNotEmpty; + + AlbumArtworkJob _job() { + final extensions = ref.read(extensionProvider); + final byId = _providers(); + final providers = [ + for (final id in extensions.metadataProviderPriority) + if (byId.containsKey(id)) id, + for (final id in byId.keys) + if (!extensions.metadataProviderPriority.contains(id)) id, + ]; + return AlbumArtworkJob( + store: ref.read(libraryAlbumArtworkStoreProvider), + lookup: AlbumArtworkLookup( + providers: providers, + search: (providerId, query) { + final provider = byId[providerId]; + if (provider != null && provider.hasCustomSearch) { + final filter = provider.searchBehavior?.filterIdForKind('album'); + return PlatformBridge.customSearchWithExtension( + providerId, + query, + options: {'limit': 10, 'filter': ?filter}, + ); + } + return PlatformBridge.searchTracksWithMetadataProvider( + providerId, + query, + limit: 10, + ); + }, + details: (providerId, id) => + PlatformBridge.getProviderMetadata(providerId, 'album', id), + ), + downloadCover: (url, path) async { + final result = await PlatformBridge.downloadCoverToFile( + url, + path, + maxDimension: _coverMaxDimension, + ); + return result['error'] == null && result['success'] != false; + }, + hasMotion: (album) async => + await _motion.find(_motionKeys(album).first) != null, + saveMotion: (album, url) async { + final keys = _motionKeys(album); + final saved = await _motion.save( + keys.first, + resolveSource: () async => url, + ); + if (saved == null) return false; + for (final key in keys.skip(1)) { + await _motion.alias(key, keys.first); + } + return true; + }, + ); + } + + /// The names players look the album's animated cover up by: the album + /// with each song artist, and with the album artist. + static List _motionKeys(LibraryArtworkAlbum album) => [ + for (final artist in {...album.artists, album.album.artist}) + (album: album.album.album, artist: artist), + ]; + + /// Looks up covers for the albums without one ([covers]) and animated + /// covers ([motion]) across the Library. Null while another run is busy. + Future findMissing({ + required bool covers, + required bool motion, + }) => _run(covers: covers, motion: motion); + + /// Looks [albumKey] up again, also bringing back artwork removed from it. + /// [motion] asks for an animated cover too, where the page can show one. + Future findForAlbum( + String albumKey, { + required bool motion, + }) => _run( + covers: true, + motion: motion && ref.read(settingsProvider).motionArtworkEnabled, + albumKey: albumKey, + ); + + Future _run({ + required bool covers, + required bool motion, + String? albumKey, + }) async { + if (state.running || (!covers && !motion)) return null; + _cancelled = false; + state = LibraryAlbumArtworkState(running: true, albumKey: albumKey); + try { + final job = _job(); + if (albumKey == null) await job.restore(cancelled: () => _cancelled); + final summary = await job.run( + covers: covers, + motion: motion, + albumKey: albumKey, + force: albumKey != null, + cancelled: () => _cancelled, + progress: (done, total) { + if (!ref.mounted) return; + state = LibraryAlbumArtworkState( + running: true, + albumKey: albumKey, + done: done, + total: total, + cancelling: _cancelled, + ); + }, + ); + await _changed(covers: summary.songs > 0, motion: summary.motion > 0); + return summary; + } catch (error, stack) { + _log.e('Album artwork lookup failed: $error', error, stack); + rethrow; + } finally { + if (ref.mounted) state = const LibraryAlbumArtworkState(); + } + } + + void cancel() { + if (!state.running) return; + _cancelled = true; + state = LibraryAlbumArtworkState( + running: true, + albumKey: state.albumKey, + done: state.done, + total: state.total, + cancelling: true, + ); + } + + /// Takes the found cover and animated cover off [albumKey] as wrong, and + /// keeps later runs from bringing them back. + Future removeForAlbum(String albumKey) async { + final store = ref.read(libraryAlbumArtworkStoreProvider); + final album = (await store.albums(albumKey: albumKey)).firstOrNull; + final record = await store.removeCover(albumKey); + if (record == null) return; + if (record.motionSaved && album != null) { + for (final key in _motionKeys(album)) { + await _motion.remove(key); + } + } + await _changed( + covers: record.coverFile != null, + motion: record.motionSaved, + ); + } + + /// Fetches again the found covers a cleared cache deleted. Runs after + /// scans, which set found covers again only while their files exist. + Future restoreMissing() => + _restoring ??= _restore().whenComplete(() => _restoring = null); + + Future _restore() async { + try { + final changed = await _job().restore(); + await _changed(covers: changed > 0, motion: false); + return changed; + } catch (error) { + _log.w('Could not restore found album covers: $error'); + return 0; + } + } + + Future _changed({required bool covers, required bool motion}) async { + if (!ref.mounted) return; + ref.invalidate(libraryAlbumArtworkForSongProvider); + if (motion) ref.invalidate(playerMotionArtworkProvider); + if (covers) { + await ref.read(localLibraryProvider.notifier).reloadFromStorage(); + } + } +} + +final libraryAlbumArtworkProvider = + NotifierProvider( + LibraryAlbumArtworkNotifier.new, + ); diff --git a/lib/providers/local_library_provider.dart b/lib/providers/local_library_provider.dart index d20d2e567..f1e48413f 100644 --- a/lib/providers/local_library_provider.dart +++ b/lib/providers/local_library_provider.dart @@ -5,6 +5,7 @@ import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:path_provider/path_provider.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'package:mornye/models/library_scan_failure.dart'; +import 'package:mornye/providers/library_album_artwork_provider.dart'; import 'package:mornye/providers/settings_provider.dart'; import 'package:mornye/services/library_cleanup.dart'; import 'package:mornye/services/library_database.dart'; diff --git a/lib/providers/local_library_provider_scan.dart b/lib/providers/local_library_provider_scan.dart index f4f2d5537..77d255f77 100644 --- a/lib/providers/local_library_provider_scan.dart +++ b/lib/providers/local_library_provider_scan.dart @@ -347,6 +347,8 @@ extension _LocalLibraryScan on LocalLibraryNotifier { '${state.scanErrorCount} errors', ); } else { + await _markSongsForCoverReread(); + if (!ref.mounted || _scanCancelRequested) return; final nativeIngestion = PlatformBridge.supportsCoreBackend; final existingFiles = nativeIngestion ? const {} @@ -624,6 +626,14 @@ extension _LocalLibraryScan on LocalLibraryNotifier { } catch (e) { _log.w('Failed to save lastScannedAt: $e'); } + // The scan cleared the covers of songs whose files have none, including + // covers found online for their albums. + try { + await ref.read(libraryAlbumArtworkStoreProvider).apply(); + } catch (e) { + _log.w('Failed to apply found album covers: $e'); + } + if (!ref.mounted) return; await _refreshSummaryFromStorage( lastScannedAt: now, @@ -639,6 +649,8 @@ extension _LocalLibraryScan on LocalLibraryNotifier { excludedDownloadedCount: skippedDownloads, ); if (pruneCoverCache) await _pruneLibraryCoverCache(); + if (!ref.mounted) return; + unawaited(ref.read(libraryAlbumArtworkProvider.notifier).restoreMissing()); final message = summary(); if (state.scanErrorCount > 0) { @@ -920,6 +932,29 @@ extension _LocalLibraryScan on LocalLibraryNotifier { return decoded; } + /// Lets an incremental scan read again the songs whose cover file is gone, + /// and once the songs without a cover, which may have a folder image, so + /// neither waits for a full scan. + Future _markSongsForCoverReread() async { + try { + final appSupportDir = await getApplicationSupportDirectory(); + final libraryDatabase = await _db.database; + final missing = await markLibraryRowsWithMissingCovers( + Directory('${appSupportDir.path}/library_covers'), + libraryDatabase, + ); + final coverless = await markCoverlessLibraryRowsOnce(libraryDatabase); + if (missing > 0 || coverless > 0) { + _log.i( + 'Rereading $missing songs whose cover file is gone and ' + '$coverless songs without a cover', + ); + } + } catch (e) { + _log.w('Could not check library covers: $e'); + } + } + Future _pruneLibraryCoverCache() async { try { final appSupportDir = await getApplicationSupportDirectory(); diff --git a/lib/screens/local_album_screen.dart b/lib/screens/local_album_screen.dart index 693604785..c603b308a 100644 --- a/lib/screens/local_album_screen.dart +++ b/lib/screens/local_album_screen.dart @@ -15,6 +15,7 @@ import 'package:mornye/utils/adaptive_layout.dart'; import 'package:mornye/utils/audio_quality_badge_policy.dart'; import 'package:mornye/utils/file_access.dart'; import 'package:mornye/utils/image_cache_utils.dart'; +import 'package:mornye/services/library_album_artwork.dart'; import 'package:mornye/services/library_database.dart'; import 'package:mornye/services/batch_track_actions.dart'; import 'package:mornye/models/album_track_order.dart'; @@ -22,6 +23,7 @@ import 'package:mornye/models/track.dart'; import 'package:mornye/models/unified_library_item.dart'; import 'package:mornye/services/local_track_redownload_service.dart'; import 'package:mornye/providers/deleted_library_files.dart'; +import 'package:mornye/providers/library_album_artwork_provider.dart'; import 'package:mornye/providers/local_library_provider.dart'; import 'package:mornye/providers/playback_provider.dart'; import 'package:mornye/providers/music_player_provider.dart'; @@ -258,10 +260,13 @@ class _LocalAlbumScreenState extends ConsumerState ) .firstOrNull ?? _sortedTracksCache.firstOrNull; + // Songs handed to the page keep the covers they had when it opened, so + // a cover found from here shows from its record. + final foundCover = _albumArtwork()?.record?.coverFile; return LocalLibraryArtworkBuilder( coverPath: albumCover?.isNotEmpty == true ? albumCover - : artworkTrack?.coverPath, + : artworkTrack?.coverPath ?? foundCover, filePath: artworkTrack?.filePath, builder: (context, coverPath) => context.isMornye ? MornyeArtistSurface( @@ -508,15 +513,117 @@ class _LocalAlbumScreenState extends ConsumerState ); }, ), - moreMenu: () => libraryTrackMenuGroups( - context, - ref, - items: _albumItems(), - onPlay: _playAll, + moreMenu: () => _withArtworkActions( + libraryTrackMenuGroups( + context, + ref, + items: _albumItems(), + onPlay: _playAll, + ), + hasCover: coverPath != null, + hasMotion: + motion != null || + !context.isMornye || + MediaQuery.disableAnimationsOf(context), ), ); } + /// The album's key and what Find Missing Artwork found for it. Null for + /// songs from a network connection, which keep their server's covers. + ({String albumKey, AlbumArtworkRecord? record})? _albumArtwork() { + final path = _sortedTracksCache.firstOrNull?.filePath; + if (path == null || path.startsWith('network://')) return null; + return ref.watch(libraryAlbumArtworkForSongProvider(path)).value; + } + + /// [groups] with Find Artwork, while the album lacks a cover or an + /// animated cover it could show, and Remove Found Artwork, once something + /// was found. They go before a last group of destructive actions. + List> _withArtworkActions( + List> groups, { + required bool hasCover, + required bool hasMotion, + }) { + final artwork = _albumArtwork(); + if (artwork == null) return groups; + final record = artwork.record; + final busy = ref.read(libraryAlbumArtworkProvider).running; + final wantsMotion = + !hasMotion && ref.read(settingsProvider).motionArtworkEnabled; + final actions = [ + if (!hasCover || wantsMotion) + ContextMenuAction( + icon: Icons.image_search_outlined, + label: context.l10n.albumFindArtwork, + onSelected: busy + ? null + : () => _findArtwork(artwork.albumKey, motion: wantsMotion), + ), + if (record?.coverFile != null || record?.motionSaved == true) + ContextMenuAction( + icon: Icons.hide_image_outlined, + label: context.l10n.albumRemoveFoundArtwork, + onSelected: busy ? null : () => _removeFoundArtwork(artwork.albumKey), + ), + ]; + if (actions.isEmpty) return groups; + final destructiveLast = + groups.isNotEmpty && groups.last.every((action) => action.destructive); + return [ + ...destructiveLast ? groups.take(groups.length - 1) : groups, + actions, + if (destructiveLast) groups.last, + ]; + } + + Future _findArtwork(String albumKey, {required bool motion}) async { + final artwork = ref.read(libraryAlbumArtworkProvider.notifier); + final l10n = context.l10n; + if (!artwork.canLookUp) { + showAppSnackBar( + context, + content: Text(l10n.libraryFindArtworkNoProvider), + ); + return; + } + try { + final summary = await artwork.findForAlbum(albumKey, motion: motion); + if (summary == null || !mounted) return; + final found = + summary.covers > 0 || summary.motion > 0 || summary.failed > 0; + showAppSnackBar( + context, + content: Text( + found + ? l10n.libraryFindArtworkResult( + summary.covers, + summary.motion, + summary.failed, + ) + : l10n.albumFindArtworkNotFound, + ), + ); + if (summary.songs > 0) _loadTracks(); + } catch (error) { + if (!mounted) return; + showAppSnackBar( + context, + content: Text(l10n.snackbarError(context.friendlyError(error))), + ); + } + } + + Future _removeFoundArtwork(String albumKey) async { + final l10n = context.l10n; + await ref + .read(libraryAlbumArtworkProvider.notifier) + .removeForAlbum(albumKey); + if (!mounted) return; + showAppSnackBar(context, content: Text(l10n.albumRemoveFoundArtworkDone)); + _loadTracks(); + } + AlbumTrackSortKey get _sortKey => (album: widget.albumName, artist: widget.artistName); diff --git a/lib/screens/settings/cache_management_page.dart b/lib/screens/settings/cache_management_page.dart index 1d46d7169..cea881678 100644 --- a/lib/screens/settings/cache_management_page.dart +++ b/lib/screens/settings/cache_management_page.dart @@ -1,3 +1,4 @@ +import 'dart:async'; import 'dart:io'; import 'package:flutter/material.dart'; @@ -10,6 +11,7 @@ import 'package:path_provider/path_provider.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'package:mornye/l10n/l10n.dart'; import 'package:mornye/providers/download_queue_provider.dart'; +import 'package:mornye/providers/library_album_artwork_provider.dart'; import 'package:mornye/providers/local_library_provider.dart'; import 'package:mornye/providers/settings_provider.dart'; import 'package:mornye/services/audio_analysis_cache.dart'; @@ -167,6 +169,13 @@ class _CacheManagementPageState extends ConsumerState { libraryCoverDir, await LibraryDatabase.instance.database, ); + // Covers found online for albums come back from their saved addresses + // without a scan or a new search. + if (mounted) { + unawaited( + ref.read(libraryAlbumArtworkProvider.notifier).restoreMissing(), + ); + } } Future _clearAudioAnalysisCache() async { diff --git a/lib/screens/settings/library_settings_page.dart b/lib/screens/settings/library_settings_page.dart index 1a250cd4d..05b644e00 100644 --- a/lib/screens/settings/library_settings_page.dart +++ b/lib/screens/settings/library_settings_page.dart @@ -10,8 +10,10 @@ import 'package:mornye/controllers/library_re_enrich_all.dart'; import 'package:mornye/l10n/l10n.dart'; import 'package:mornye/models/library_scan_failure.dart'; import 'package:mornye/models/settings.dart'; +import 'package:mornye/theme/mornye_theme.dart'; import 'package:mornye/providers/settings_provider.dart'; import 'package:mornye/providers/download_history_provider.dart'; +import 'package:mornye/providers/library_album_artwork_provider.dart'; import 'package:mornye/providers/local_library_provider.dart'; import 'package:mornye/services/library_database.dart'; import 'package:mornye/services/link/link_source_names.dart'; @@ -295,6 +297,74 @@ class _LibrarySettingsPageState extends ConsumerState { } } + Future _findArtwork() async { + if (ref.read(libraryAlbumArtworkProvider).running) return; + final l10n = context.l10n; + final messenger = ScaffoldMessenger.of(context); + if (!ref.read(libraryAlbumArtworkProvider.notifier).canLookUp) { + messenger.showSnackBar( + SnackBar(content: Text(l10n.libraryFindArtworkNoProvider)), + ); + return; + } + var motion = false; + // Animated covers mean a lookup for every album, so they are a choice, + // offered only where the players and album pages show them. + if (context.isMornye && ref.read(settingsProvider).motionArtworkEnabled) { + final choice = await showAppBottomSheet( + context: context, + useRootNavigator: true, + title: l10n.libraryFindArtwork, + builder: (context) => Column( + mainAxisSize: MainAxisSize.min, + children: [ + AppSheetOption( + leading: const AppIcon(Icons.image_outlined), + title: Text(context.l10n.libraryFindArtworkCovers), + subtitle: Text(context.l10n.libraryFindArtworkCoversSubtitle), + onTap: () => Navigator.pop(context, false), + ), + AppSheetOption( + leading: const AppIcon(Icons.motion_photos_on_outlined), + title: Text(context.l10n.libraryFindArtworkMotion), + subtitle: Text(context.l10n.libraryFindArtworkMotionSubtitle), + onTap: () => Navigator.pop(context, true), + ), + const SizedBox(height: 8), + ], + ), + ); + if (choice == null || !mounted) return; + motion = choice; + } + try { + final summary = await ref + .read(libraryAlbumArtworkProvider.notifier) + .findMissing(covers: true, motion: motion); + if (summary == null) return; + messenger.showSnackBar( + SnackBar( + content: Text( + summary.albums == 0 + ? l10n.libraryFindArtworkNothing + : l10n.libraryFindArtworkResult( + summary.covers, + summary.motion, + summary.failed, + ), + ), + ), + ); + } catch (error) { + if (!mounted) return; + messenger.showSnackBar( + SnackBar( + content: Text(l10n.snackbarError(context.friendlyError(error))), + ), + ); + } + } + Future _findMovedDownloads() async { if (_isFindingMovedDownloads || _isCleaningMissingFiles) return; setState(() => _isFindingMovedDownloads = true); @@ -774,6 +844,9 @@ class _LibrarySettingsPageState extends ConsumerState { final canReEnrich = !_isLoadingReEnrich && (libraryState.totalCount > 0 || hasDownloads); + final artwork = ref.watch(libraryAlbumArtworkProvider); + final canFindArtwork = + !artwork.running && libraryState.totalCount > 0; return SettingsGroup( connected: true, children: [ @@ -831,6 +904,31 @@ class _LibrarySettingsPageState extends ConsumerState { onTap: canReEnrich ? _reEnrichAll : null, ), ), + if (artwork.running && artwork.albumKey == null) + _ArtworkProgressTile( + done: artwork.done, + total: artwork.total, + stopping: artwork.cancelling, + onStop: ref + .read(libraryAlbumArtworkProvider.notifier) + .cancel, + ) + else + Opacity( + opacity: canFindArtwork ? 1.0 : 0.5, + child: SettingsItem( + icon: Icons.image_search_outlined, + title: context.l10n.libraryFindArtwork, + subtitle: context.l10n.libraryFindArtworkSubtitle, + trailing: artwork.running + ? const AppLoadingIndicator( + size: 20, + strokeWidth: 2, + ) + : null, + onTap: canFindArtwork ? _findArtwork : null, + ), + ), ], Opacity( opacity: canFindMoved || _isFindingMovedDownloads diff --git a/lib/screens/settings/library_settings_page_widgets.dart b/lib/screens/settings/library_settings_page_widgets.dart index ac00816ca..885dd2ef2 100644 --- a/lib/screens/settings/library_settings_page_widgets.dart +++ b/lib/screens/settings/library_settings_page_widgets.dart @@ -697,3 +697,74 @@ class _ScanProgressTile extends StatelessWidget { ); } } + +/// Find Missing Artwork while it runs, laid out like [_ScanProgressTile]. +class _ArtworkProgressTile extends StatelessWidget { + final int done; + final int total; + final bool stopping; + final VoidCallback onStop; + + const _ArtworkProgressTile({ + required this.done, + required this.total, + required this.stopping, + required this.onStop, + }); + + @override + Widget build(BuildContext context) { + final colorScheme = Theme.of(context).colorScheme; + return Padding( + padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12), + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Row( + children: [ + AppIcon(Icons.image_search_outlined, color: colorScheme.primary), + const SizedBox(width: 16), + Expanded( + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Text( + context.l10n.libraryFindArtworkRunning, + style: Theme.of(context).textTheme.bodyLarge?.copyWith( + fontWeight: FontWeight.w500, + ), + ), + if (stopping || total > 0) + Text( + stopping + ? context.l10n.libraryFindArtworkStopping + : context.l10n.libraryFindArtworkProgress( + done, + total, + ), + style: Theme.of(context).textTheme.bodySmall?.copyWith( + color: colorScheme.onSurfaceVariant, + ), + ), + ], + ), + ), + ExpressiveButton( + text: true, + onPressed: stopping ? null : onStop, + child: Text(context.l10n.actionStop), + ), + ], + ), + const SizedBox(height: 8), + AppProgressIndicator.linear( + value: total > 0 ? done / total : null, + trackColor: colorScheme.surfaceContainerHighest, + color: colorScheme.primary, + borderRadius: BorderRadius.circular(4), + ), + ], + ), + ); + } +} diff --git a/lib/services/library_album_artwork.dart b/lib/services/library_album_artwork.dart new file mode 100644 index 000000000..6e42a9e7a --- /dev/null +++ b/lib/services/library_album_artwork.dart @@ -0,0 +1,696 @@ +import 'dart:async'; +import 'dart:convert'; +import 'dart:io'; + +import 'package:crypto/crypto.dart'; +import 'package:sqflite/sqflite.dart'; +import 'package:mornye/services/library_schema.dart'; +import 'package:mornye/services/sqlite_helpers.dart' as sqlite; +import 'package:mornye/utils/album_artwork_match.dart'; +import 'package:mornye/utils/file_access.dart'; +import 'package:mornye/utils/logger.dart'; + +final _log = AppLogger('AlbumArtwork'); + +/// What was found for one Library album, kept so a later run neither looks +/// it up again nor puts back artwork the user removed. +class AlbumArtworkRecord { + const AlbumArtworkRecord({ + required this.albumKey, + required this.matched, + required this.checkedAt, + this.providerId, + this.albumId, + this.coverUrl, + this.motionUrl, + this.coverFile, + this.coverRemoved = false, + this.motionSaved = false, + this.motionRemoved = false, + }); + + final String albumKey; + + /// Whether a provider album matched. A record without a match stops the + /// album from being looked up again for [AlbumArtworkJob.recheckAfter]. + final bool matched; + final DateTime checkedAt; + final String? providerId; + final String? albumId; + final String? coverUrl; + final String? motionUrl; + + /// The cover saved for the album's songs that have none. Clearing the + /// cover cache deletes it; [coverUrl] fetches it again without a search. + final String? coverFile; + + /// The user removed the cover as wrong; runs leave the album alone. + final bool coverRemoved; + final bool motionSaved; + final bool motionRemoved; + + AlbumArtworkRecord copyWith({ + String? coverFile, + bool clearCoverFile = false, + bool? coverRemoved, + bool? motionSaved, + bool? motionRemoved, + }) => AlbumArtworkRecord( + albumKey: albumKey, + matched: matched, + checkedAt: checkedAt, + providerId: providerId, + albumId: albumId, + coverUrl: coverUrl, + motionUrl: motionUrl, + coverFile: clearCoverFile ? null : coverFile ?? this.coverFile, + coverRemoved: coverRemoved ?? this.coverRemoved, + motionSaved: motionSaved ?? this.motionSaved, + motionRemoved: motionRemoved ?? this.motionRemoved, + ); + + Map toRow() => { + 'album_key': albumKey, + 'matched': matched ? 1 : 0, + 'provider_id': providerId, + 'album_id': albumId, + 'cover_url': coverUrl, + 'motion_url': motionUrl, + 'cover_file': coverFile, + 'cover_removed': coverRemoved ? 1 : 0, + 'motion_saved': motionSaved ? 1 : 0, + 'motion_removed': motionRemoved ? 1 : 0, + 'checked_at': checkedAt.millisecondsSinceEpoch, + }; + + static AlbumArtworkRecord fromRow(Map row) => + AlbumArtworkRecord( + albumKey: row['album_key'] as String, + matched: row['matched'] == 1, + checkedAt: DateTime.fromMillisecondsSinceEpoch( + (row['checked_at'] as num?)?.toInt() ?? 0, + ), + providerId: row['provider_id'] as String?, + albumId: row['album_id'] as String?, + coverUrl: row['cover_url'] as String?, + motionUrl: row['motion_url'] as String?, + coverFile: row['cover_file'] as String?, + coverRemoved: row['cover_removed'] == 1, + motionSaved: row['motion_saved'] == 1, + motionRemoved: row['motion_removed'] == 1, + ); +} + +/// A Library album with what it already has. +class LibraryArtworkAlbum { + const LibraryArtworkAlbum({ + required this.album, + required this.hasCover, + required this.artists, + }); + + final ArtworkAlbum album; + + /// Whether any of its songs has a cover, its own or a found one. + final bool hasCover; + + /// Each song artist on the album. Players find an animated cover by the + /// album and the playing song's artist. + final List artists; +} + +/// Artwork found online for Library albums, kept in the app and never +/// written to the music files. +/// +/// Covers are saved beside the scan's covers and set on the album's songs +/// that have no cover, so every screen, the player and Link show them as +/// they show embedded art. A scan clears a song's cover when the file has +/// none, so [apply] runs after each scan to set them again. +class LibraryAlbumArtworkStore { + LibraryAlbumArtworkStore({ + required Future Function() database, + required Future Function() coverDirectory, + }) : _database = database, + _coverDirectory = coverDirectory; + + static const table = 'library_album_artwork'; + + final Future Function() _database; + final Future Function() _coverDirectory; + Future? _ready; + + Future _open() async { + final db = await _database(); + await (_ready ??= db + .execute(''' + CREATE TABLE IF NOT EXISTS $table ( + album_key TEXT PRIMARY KEY, + matched INTEGER NOT NULL DEFAULT 0, + provider_id TEXT, + album_id TEXT, + cover_url TEXT, + motion_url TEXT, + cover_file TEXT, + cover_removed INTEGER NOT NULL DEFAULT 0, + motion_saved INTEGER NOT NULL DEFAULT 0, + motion_removed INTEGER NOT NULL DEFAULT 0, + checked_at INTEGER NOT NULL DEFAULT 0 + )''') + .catchError((Object error) { + _ready = null; + throw error; + })); + return db; + } + + /// The visible local albums, or only the one with [albumKey]. Network + /// songs keep the covers their server gives them. + Future> albums({String? albumKey}) async { + final db = await _open(); + final rows = await db.rawQuery( + 'SELECT album_key, MAX(album_name) AS album_name, ' + "MAX(COALESCE(NULLIF(TRIM(album_artist), ''), artist_name)) AS artist, " + 'COUNT(*) AS tracks, ' + "MIN(NULLIF(release_date, '')) AS release_date, " + "MAX(CASE WHEN cover_path IS NOT NULL AND cover_path != '' " + 'THEN 1 ELSE 0 END) AS has_cover ' + 'FROM ${LibrarySchema.visibleView} ' + "WHERE file_path NOT LIKE 'network://%' AND album_key IS NOT NULL " + "AND TRIM(COALESCE(album_name, '')) != '' " + '${albumKey == null ? '' : 'AND album_key = ? '}' + 'GROUP BY album_key', + [?albumKey], + ); + final artists = >{}; + for (final row in await db.rawQuery( + 'SELECT DISTINCT album_key, artist_name ' + 'FROM ${LibrarySchema.visibleView} ' + "WHERE file_path NOT LIKE 'network://%' AND album_key IS NOT NULL " + '${albumKey == null ? '' : 'AND album_key = ? '}', + [?albumKey], + )) { + final artist = (row['artist_name'] as String? ?? '').trim(); + if (artist.isEmpty) continue; + artists.putIfAbsent(row['album_key'] as String, () => []).add(artist); + } + final albums = []; + for (final row in rows) { + final name = (row['album_name'] as String? ?? '').trim(); + final artist = (row['artist'] as String? ?? '').trim(); + if (_unknown.contains(artworkMatchText(name)) || + _unknown.contains(artworkMatchText(artist))) { + continue; + } + albums.add( + LibraryArtworkAlbum( + album: ArtworkAlbum( + key: row['album_key'] as String, + album: name, + artist: artist, + trackCount: (row['tracks'] as num?)?.toInt() ?? 0, + year: artworkYear(row['release_date'] as String?), + ), + hasCover: row['has_cover'] == 1, + artists: artists[row['album_key']] ?? const [], + ), + ); + } + return albums; + } + + // Tags read from file names fall back to these; nothing can match them. + static const _unknown = {'', 'unknown', 'unknown album', 'unknown artist'}; + + /// The `album_key` of the Library song at [filePath]. + Future albumKeyOf(String filePath) async { + final db = await _open(); + final rows = await db.query( + 'library', + columns: ['album_key'], + where: 'file_path = ?', + whereArgs: [filePath], + limit: 1, + ); + return rows.firstOrNull?['album_key'] as String?; + } + + Future> records({String? albumKey}) async { + final db = await _open(); + final rows = await db.query( + table, + where: albumKey == null ? null : 'album_key = ?', + whereArgs: [?albumKey], + ); + return { + for (final row in rows) + row['album_key'] as String: AlbumArtworkRecord.fromRow(row), + }; + } + + Future save(AlbumArtworkRecord record) async { + final db = await _open(); + await db.insert( + table, + record.toRow(), + conflictAlgorithm: ConflictAlgorithm.replace, + ); + } + + /// Where a new cover for [albumKey] is saved. Each cover gets its own + /// name, so image caches keyed by path never show the one it replaced. + Future newCoverPath(String albumKey, DateTime now) async { + final directory = await _coverDirectory(); + await directory.create(recursive: true); + final hash = sha1 + .convert(utf8.encode(albumKey)) + .toString() + .substring(0, 16); + return '${directory.path}/album_${hash}_' + '${now.microsecondsSinceEpoch.toRadixString(36)}.jpg'; + } + + /// Sets each found cover on its album's songs that have none. A cover + /// whose file is gone is dropped from its record and from those songs, + /// so [AlbumArtworkJob.restore] can fetch it again. Returns the number of + /// songs that changed. + Future apply() async { + final db = await _open(); + final rows = await db.query( + table, + columns: ['album_key', 'cover_file'], + where: 'cover_file IS NOT NULL AND cover_removed = 0', + ); + final gone = {}; + for (final row in rows) { + final path = row['cover_file'] as String; + if (!await File(path).exists()) gone[row['album_key'] as String] = path; + } + return sqlite.transactionWithBusyRetry(db, (txn) async { + var changed = 0; + for (final MapEntry(key: albumKey, value: path) in gone.entries) { + await txn.update( + table, + {'cover_file': null}, + where: 'album_key = ?', + whereArgs: [albumKey], + ); + changed += await txn.update( + 'library', + {'cover_path': null}, + where: 'cover_path = ?', + whereArgs: [path], + ); + } + changed += await txn.rawUpdate( + 'UPDATE library SET cover_path = (' + 'SELECT a.cover_file FROM $table a ' + 'WHERE a.album_key = library.album_key) ' + "WHERE (cover_path IS NULL OR cover_path = '') " + "AND file_path NOT LIKE 'network://%' " + 'AND album_key IN (SELECT album_key FROM $table ' + 'WHERE cover_file IS NOT NULL AND cover_removed = 0)', + ); + return changed; + }); + } + + /// Takes the found cover off [albumKey]'s songs and keeps runs from + /// putting it back. Returns the record as it was. + Future removeCover(String albumKey) async { + final db = await _open(); + final record = (await records(albumKey: albumKey))[albumKey]; + if (record == null) return null; + final file = record.coverFile; + await sqlite.transactionWithBusyRetry(db, (txn) async { + if (file != null) { + await txn.update( + 'library', + {'cover_path': null}, + where: 'cover_path = ?', + whereArgs: [file], + ); + } + await txn.insert( + table, + record + .copyWith( + clearCoverFile: true, + coverRemoved: true, + motionSaved: false, + motionRemoved: record.motionSaved || record.motionRemoved, + ) + .toRow(), + conflictAlgorithm: ConflictAlgorithm.replace, + ); + }); + if (file != null) await deleteFileQuietly(File(file)); + return record; + } +} + +/// How a lookup for one album ended. +sealed class ArtworkLookup { + const ArtworkLookup(); +} + +/// A provider album matched. +class ArtworkFound extends ArtworkLookup { + const ArtworkFound(this.candidate, {this.coverUrl, this.motionUrl}); + + final ArtworkCandidate candidate; + final String? coverUrl; + final String? motionUrl; +} + +/// Every provider answered and none had the album. +class ArtworkNotFound extends ArtworkLookup { + const ArtworkNotFound(); +} + +/// No provider answered, so nothing is known yet. +class ArtworkLookupFailed extends ArtworkLookup { + const ArtworkLookupFailed(); +} + +/// Searches providers in order for an album, then reads the matched one's +/// details for its full-size cover and animated cover. +class AlbumArtworkLookup { + const AlbumArtworkLookup({ + required this.providers, + required this.search, + required this.details, + }); + + /// Metadata provider IDs in the user's priority order. + final List providers; + + /// Album search results from one provider. + final Future>> Function( + String providerId, + String query, + ) + search; + + /// One provider album, as `getProviderMetadata` returns it. + final Future> Function(String providerId, String id) + details; + + Future find(ArtworkAlbum album) async { + var answered = false; + for (final provider in providers) { + final List> results; + try { + results = await search(provider, '${album.artist} ${album.album}'); + answered = true; + } catch (error) { + _log.d('Album search failed on $provider: $error'); + continue; + } + final match = pickArtworkCandidate(album, [ + for (final result in results) + ?ArtworkCandidate.fromSearchResult(provider, result), + ]); + if (match == null) continue; + String? coverUrl; + String? motionUrl; + try { + final result = await details(provider, match.albumId); + final info = result['album_info'] is Map + ? Map.from(result['album_info'] as Map) + : result; + String? url(Object? value) => normalizeArtworkUrl(value?.toString()); + coverUrl = + url(info['cover_url'] ?? info['images']) ?? + url(result['cover_url']); + motionUrl = url(info['header_video'] ?? result['header_video']); + } catch (error) { + _log.d('Album details failed on $provider: $error'); + } + return ArtworkFound( + match, + coverUrl: coverUrl ?? match.coverUrl, + motionUrl: motionUrl, + ); + } + return answered ? const ArtworkNotFound() : const ArtworkLookupFailed(); + } +} + +/// What a run found. +class AlbumArtworkSummary { + const AlbumArtworkSummary({ + this.albums = 0, + this.covers = 0, + this.motion = 0, + this.failed = 0, + this.songs = 0, + this.cancelled = false, + }); + + /// Albums that needed something. + final int albums; + final int covers; + final int motion; + + /// Albums no provider could be asked about. + final int failed; + + /// Songs whose cover changed. + final int songs; + final bool cancelled; +} + +/// Finds covers for Library albums without one, and animated covers, one +/// lookup per album instead of one per song, and keeps them in the app. +class AlbumArtworkJob { + AlbumArtworkJob({ + required this.store, + required this.lookup, + required this.downloadCover, + required this.hasMotion, + required this.saveMotion, + DateTime Function()? now, + this.concurrency = 2, + }) : _now = now ?? DateTime.now; + + /// How long an album without a match waits before it is looked up again, + /// as providers add albums. + static const recheckAfter = Duration(days: 30); + + final LibraryAlbumArtworkStore store; + final AlbumArtworkLookup lookup; + + /// Saves the cover at a URL to a path, at most library cover size. + final Future Function(String url, String path) downloadCover; + + /// Whether an animated cover is saved for the album already. + final Future Function(LibraryArtworkAlbum album) hasMotion; + + /// Saves the animated cover at a URL for the album. + final Future Function(LibraryArtworkAlbum album, String url) saveMotion; + final DateTime Function() _now; + final int concurrency; + + /// Looks up the albums that need a cover ([covers]) or an animated cover + /// ([motion]). [albumKey] limits it to one album; [force] looks that one + /// up again and lets back artwork the user removed. + Future run({ + required bool covers, + required bool motion, + String? albumKey, + bool force = false, + bool Function()? cancelled, + void Function(int done, int total)? progress, + }) async { + final isCancelled = cancelled ?? () => false; + final albums = await store.albums(albumKey: albumKey); + final records = await store.records(albumKey: albumKey); + final work = <({LibraryArtworkAlbum album, bool cover, bool motion})>[]; + for (final album in albums) { + final record = records[album.album.key]; + final wantCover = + covers && !album.hasCover && (force || record?.coverRemoved != true); + final wantMotion = + motion && + (force || record?.motionRemoved != true) && + !await hasMotion(album); + if (wantCover || wantMotion) { + work.add((album: album, cover: wantCover, motion: wantMotion)); + } + } + var done = 0; + var foundCovers = 0; + var foundMotion = 0; + var failed = 0; + progress?.call(0, work.length); + var next = 0; + Future worker() async { + while (next < work.length && !isCancelled()) { + final item = work[next++]; + final outcome = await _process( + item.album, + records[item.album.album.key], + cover: item.cover, + motion: item.motion, + force: force, + cancelled: isCancelled, + ); + if (outcome.failed) failed++; + if (outcome.cover) foundCovers++; + if (outcome.motion) foundMotion++; + progress?.call(++done, work.length); + } + } + + await Future.wait([for (var i = 0; i < concurrency; i++) worker()]); + final songs = await store.apply(); + return AlbumArtworkSummary( + albums: work.length, + covers: foundCovers, + motion: foundMotion, + failed: failed, + songs: songs, + cancelled: isCancelled(), + ); + } + + Future<({bool cover, bool motion, bool failed})> _process( + LibraryArtworkAlbum album, + AlbumArtworkRecord? record, { + required bool cover, + required bool motion, + required bool force, + required bool Function() cancelled, + }) async { + final now = _now(); + final AlbumArtworkRecord? latest; + if (record != null && !force && !_wantsLookup(record, now, cover, motion)) { + latest = record; + } else { + latest = await _lookUp(album, record, now, force); + } + if (latest == null) return (cover: false, motion: false, failed: true); + var current = latest; + if (cancelled()) return (cover: false, motion: false, failed: false); + var gotCover = false; + var gotMotion = false; + final coverUrl = current.coverUrl; + if (cover && current.matched && coverUrl != null) { + final path = await store.newCoverPath(album.album.key, now); + try { + if (await downloadCover(coverUrl, path) && await File(path).exists()) { + final previous = current.coverFile; + current = current.copyWith(coverFile: path, coverRemoved: false); + await store.save(current); + if (previous != null && previous != path) { + await deleteFileQuietly(File(previous)); + } + gotCover = true; + } + } catch (error) { + _log.w('Could not save the cover for an album: $error'); + } + if (!gotCover) await deleteFileQuietly(File(path)); + } + final motionUrl = current.motionUrl; + if (motion && current.matched && motionUrl != null && !cancelled()) { + try { + if (await saveMotion(album, motionUrl)) { + await store.save( + current.copyWith(motionSaved: true, motionRemoved: false), + ); + gotMotion = true; + } + } catch (error) { + _log.w('Could not save the animated cover for an album: $error'); + } + } + return (cover: gotCover, motion: gotMotion, failed: false); + } + + /// A known album is looked up again only once its result is old and + /// lacks what is wanted now: a match, a cover or an animated cover. + bool _wantsLookup( + AlbumArtworkRecord record, + DateTime now, + bool cover, + bool motion, + ) { + if (now.difference(record.checkedAt) < recheckAfter) return false; + return !record.matched || + (cover && record.coverUrl == null) || + (motion && record.motionUrl == null); + } + + /// Looks [album] up and saves what was found, keeping what [record] had. + /// Null when no provider could be asked. + Future _lookUp( + LibraryArtworkAlbum album, + AlbumArtworkRecord? record, + DateTime now, + bool force, + ) async { + final result = await lookup.find(album.album); + if (result is ArtworkLookupFailed) return null; + final found = result is ArtworkFound ? result : null; + final latest = AlbumArtworkRecord( + albumKey: album.album.key, + matched: found != null, + checkedAt: now, + providerId: found?.candidate.providerId, + albumId: found?.candidate.albumId, + coverUrl: found?.coverUrl, + motionUrl: found?.motionUrl, + coverFile: record?.coverFile, + coverRemoved: !force && (record?.coverRemoved ?? false), + motionSaved: record?.motionSaved ?? false, + motionRemoved: !force && (record?.motionRemoved ?? false), + ); + await store.save(latest); + return latest; + } + + /// Fetches again the found covers whose files are gone, as after clearing + /// the cover cache, from their saved URLs and without a search. Returns + /// the number of songs that changed. + Future restore({bool Function()? cancelled}) async { + // [apply] first drops the covers whose file is gone, so their songs count + // as without a cover below. + var changed = await store.apply(); + final lacking = { + for (final album in await store.albums()) + if (!album.hasCover) album.album.key, + }; + var restored = false; + for (final record in (await store.records()).values) { + if (cancelled?.call() == true) break; + final url = record.coverUrl; + if (!record.matched || + record.coverRemoved || + record.coverFile != null || + url == null || + !lacking.contains(record.albumKey)) { + continue; + } + final path = await store.newCoverPath(record.albumKey, _now()); + try { + if (await downloadCover(url, path) && await File(path).exists()) { + await store.save(record.copyWith(coverFile: path)); + restored = true; + continue; + } + } catch (error) { + _log.w('Could not restore the cover for an album: $error'); + } + await deleteFileQuietly(File(path)); + } + if (restored) changed += await store.apply(); + return changed; + } +} + +/// [value] when it is an http(s) URL. +String? normalizeArtworkUrl(String? value) { + final url = value?.trim() ?? ''; + return url.startsWith('https://') || url.startsWith('http://') ? url : null; +} diff --git a/lib/services/library_cleanup.dart b/lib/services/library_cleanup.dart index 73bf85cbf..89412413d 100644 --- a/lib/services/library_cleanup.dart +++ b/lib/services/library_cleanup.dart @@ -318,6 +318,110 @@ Future clearLibraryCoverFiles( ); } +/// Marks the local Library rows whose cover file in [directory] is gone, so +/// the next incremental scan reads those songs again and extracts their +/// covers, with no full scan. Covers can disappear outside Settings: a +/// system cleanup, a restored backup or a moved data folder. Rows keep the +/// scan's spelling of the folder, so a file is matched by its path inside +/// it. Returns the number of marked rows. +Future markLibraryRowsWithMissingCovers( + Directory directory, + Database libraryDatabase, +) async { + final present = {}; + if (await directory.exists()) { + await for (final entry in directory.list( + recursive: true, + followLinks: false, + )) { + if (entry is! File) continue; + present.add( + p.relative(entry.path, from: directory.path).replaceAll(r'\', '/'), + ); + } + } + final rows = await libraryDatabase.rawQuery( + 'SELECT DISTINCT cover_path FROM library ' + 'WHERE audio_metadata_scan_version > ? ' + r"AND cover_path GLOB '*[/\]library_covers[/\]*' " + "AND file_path NOT LIKE 'network://%'", + [libraryLyricsMetadataScanVersion], + ); + final missing = [ + for (final row in rows) + if (row['cover_path'] case final String path + when !present.contains(_libraryCoverName(path))) + path, + ]; + var marked = 0; + for (var start = 0; start < missing.length; start += 500) { + final chunk = missing.skip(start).take(500).toList(growable: false); + marked += await sqlite.transactionWithBusyRetry( + libraryDatabase, + (txn) => txn.rawUpdate( + 'UPDATE library SET audio_metadata_scan_version = ? ' + 'WHERE audio_metadata_scan_version > ? ' + 'AND cover_path IN (${List.filled(chunk.length, '?').join(', ')})', + [ + libraryLyricsMetadataScanVersion, + libraryLyricsMetadataScanVersion, + ...chunk, + ], + ), + ); + } + return marked; +} + +/// The path of a cover file inside its `library_covers` folder, with `/`. +String _libraryCoverName(String path) { + final normalized = path.replaceAll(r'\', '/'); + const folder = '/library_covers/'; + final at = normalized.lastIndexOf(folder); + return at < 0 ? normalized : normalized.substring(at + folder.length); +} + +const _coverlessRereadKey = 'coverless_reread_v1'; + +/// Marks the local Library rows without a cover for one reread, so the next +/// incremental scan looks for a folder image beside each song. Scans before +/// folder images were read left those songs without one. [onceKey] limits +/// this to one run per database. Returns the number of marked rows. +Future markCoverlessLibraryRows( + Database libraryDatabase, { + String? onceKey, +}) { + return sqlite.transactionWithBusyRetry(libraryDatabase, (txn) async { + if (onceKey != null) { + final marker = await txn.query( + 'library_state', + columns: ['value'], + where: 'key = ?', + whereArgs: [onceKey], + ); + if (marker.firstOrNull?['value'] == '1') return 0; + } + final marked = await txn.rawUpdate( + 'UPDATE library SET audio_metadata_scan_version = ? ' + 'WHERE audio_metadata_scan_version > ? ' + "AND (cover_path IS NULL OR cover_path = '') " + "AND file_path NOT LIKE 'network://%'", + [libraryLyricsMetadataScanVersion, libraryLyricsMetadataScanVersion], + ); + if (onceKey != null) { + await txn.insert('library_state', { + 'key': onceKey, + 'value': '1', + }, conflictAlgorithm: ConflictAlgorithm.replace); + } + return marked; + }); +} + +/// [markCoverlessLibraryRows] once, for the first scan with folder images. +Future markCoverlessLibraryRowsOnce(Database libraryDatabase) => + markCoverlessLibraryRows(libraryDatabase, onceKey: _coverlessRereadKey); + final _subsonicCover = RegExp(r'^subsonic_([0-9a-fA-F]{64})(?:_.*)?\.image$'); /// A Subsonic cover (`subsonic_.image`, or `subsonic__.image` diff --git a/lib/services/motion_artwork_store.dart b/lib/services/motion_artwork_store.dart index 9b8d2e7cc..a9a313da7 100644 --- a/lib/services/motion_artwork_store.dart +++ b/lib/services/motion_artwork_store.dart @@ -58,11 +58,50 @@ class MotionArtworkStore { .toString(); } + static final _storedKey = RegExp(r'^[0-9a-f]{64}$'); + + /// The key whose video [key] shows: its own, or the one its alias names. + Future _resolve(Directory root, String key) async { + if (await File('${root.path}/$key.mp4').exists()) return key; + final alias = File('${root.path}/$key.alias'); + if (!await alias.exists()) return key; + final target = (await alias.readAsString()).trim(); + return _storedKey.hasMatch(target) ? target : key; + } + + /// Lets [album] show the video saved for [target] without a second copy. + /// Players find a video by the album and the playing song's artist, so + /// each song artist of one album needs a name for the same video. + Future alias( + MotionArtworkAlbum album, + MotionArtworkAlbum target, + ) async { + final key = _key(album); + final targetKey = _key(target); + if (_clearing || key == targetKey) return; + final root = await _directory(); + await root.create(recursive: true); + await File('${root.path}/$key.alias').writeAsString(targetKey, flush: true); + } + + /// Deletes the video saved for [album], or its alias. + Future remove(MotionArtworkAlbum album) async { + if (_clearing) return; + final key = _key(album); + await _pending[key]; + final root = await _directory(); + for (final name in ['$key.mp4', '$key.json', '$key.alias']) { + await deleteFileQuietly(File('${root.path}/$name')); + } + _invalid.remove(key); + _legacyChecks.remove(key); + } + Future find(MotionArtworkAlbum album) async { if (_clearing) return null; try { final root = await _directory(); - final key = _key(album); + final key = await _resolve(root, _key(album)); if (_invalid.contains(key)) return null; final file = File('${root.path}/$key.mp4'); if (!await file.exists() || await file.length() == 0) return null; diff --git a/lib/utils/album_artwork_match.dart b/lib/utils/album_artwork_match.dart new file mode 100644 index 000000000..40ad4728c --- /dev/null +++ b/lib/utils/album_artwork_match.dart @@ -0,0 +1,181 @@ +import 'package:mornye/utils/provider_resource_ids.dart'; +import 'package:mornye/utils/string_utils.dart'; + +/// A Local Library album that artwork is looked up for. +class ArtworkAlbum { + const ArtworkAlbum({ + required this.key, + required this.album, + required this.artist, + this.trackCount = 0, + this.year, + }); + + /// The Library's `album_key`. + final String key; + final String album; + + /// The album artist, or the song artist when the tags have none. + final String artist; + final int trackCount; + final int? year; +} + +/// An album a metadata provider returned for an [ArtworkAlbum]. +class ArtworkCandidate { + const ArtworkCandidate({ + required this.providerId, + required this.albumId, + required this.name, + required this.artists, + this.coverUrl, + this.totalTracks = 0, + this.year, + }); + + final String providerId; + final String albumId; + final String name; + final String artists; + final String? coverUrl; + final int totalTracks; + final int? year; + + /// Reads a search result. An album result names the album itself; a song + /// result names the album it is on, which works as well for its cover. + static ArtworkCandidate? fromSearchResult( + String providerId, + Map result, + ) { + String text(String key) => result[key]?.toString().trim() ?? ''; + final type = text('item_type').toLowerCase(); + final album = type == 'album'; + final name = album ? text('name') : text('album_name'); + final id = album ? text('id') : text('album_id'); + if (type.isNotEmpty && !album && type != 'track') return null; + if (name.isEmpty || id.isEmpty) return null; + final albumArtist = text('album_artist'); + return ArtworkCandidate( + providerId: providerId, + albumId: stripPrefixedResourceId(id), + name: name, + artists: albumArtist.isNotEmpty && !album + ? albumArtist + : text('artists').isNotEmpty + ? text('artists') + : text('artist'), + coverUrl: normalizeRemoteHttpUrl( + (result['cover_url'] ?? result['images'])?.toString(), + ), + totalTracks: album ? int.tryParse(text('total_tracks')) ?? 0 : 0, + year: album ? artworkYear(text('release_date')) : null, + ); + } +} + +/// The year of a release date such as `2011-05-02` or `2011`. +int? artworkYear(String? value) { + final match = RegExp(r'^\s*(\d{4})').firstMatch(value ?? ''); + return match == null ? null : int.parse(match.group(1)!); +} + +final _separators = RegExp(r'[^\p{L}\p{N}]+', unicode: true); + +/// [value] in lower case with `&` read as "and" and punctuation as spaces, +/// so "Tom & Jerry!" and "tom and jerry" compare equal. +String artworkMatchText(String value) => value + .toLowerCase() + .replaceAll('&', ' and ') + .replaceAll(_separators, ' ') + .trim(); + +final _trailingQualifier = RegExp(r'\s*[(\[][^()\[\]]*[)\]]\s*$'); +final _trailingEdition = RegExp( + r'\s+-\s+(ep|single|(deluxe|expanded|remaster|anniversary|special|bonus)' + r'[^-]*)$', + caseSensitive: false, +); + +/// An album title without the edition it ends with, such as +/// "(Deluxe Edition)", "[Remastered 2011]" or " - EP". +String artworkBaseTitle(String value) { + var title = value.trim(); + while (true) { + final shorter = title + .replaceFirst(_trailingQualifier, '') + .replaceFirst(_trailingEdition, '') + .trim(); + if (shorter == title || shorter.isEmpty) break; + title = shorter; + } + return artworkMatchText(title); +} + +bool _artistMatches(String local, String candidate) { + final mine = artworkMatchText(local); + final theirs = artworkMatchText(candidate); + if (mine.isEmpty || theirs.isEmpty) return false; + return mine == theirs || + ' $theirs '.contains(' $mine ') || + ' $mine '.contains(' $theirs '); +} + +/// The lowest score a candidate needs: the same title, or the same title +/// without its edition with nothing else against it. +const artworkMatchThreshold = 70; + +/// How well [candidate] fits [album], or null when it is another album. +/// +/// The artist must match and so must the title, exactly or without the +/// edition. A provider album with fewer songs than the local one, or a +/// release year more than a year apart, is likely another edition and +/// counts against it; a matching song count or year counts for it. +int? artworkMatchScore(ArtworkAlbum album, ArtworkCandidate candidate) { + if (!_artistMatches(album.artist, candidate.artists)) return null; + final int base; + if (artworkMatchText(album.album) == artworkMatchText(candidate.name)) { + base = 100; + } else { + final title = artworkBaseTitle(album.album); + if (title.isEmpty || title != artworkBaseTitle(candidate.name)) { + return null; + } + base = 70; + } + var score = base; + if (album.trackCount > 0 && candidate.totalTracks > 0) { + if (album.trackCount > candidate.totalTracks + 1) { + score -= 40; + } else if (album.trackCount == candidate.totalTracks) { + score += 10; + } + } + final (mine, theirs) = (album.year, candidate.year); + if (mine != null && theirs != null) { + final gap = (mine - theirs).abs(); + if (gap == 0) { + score += 10; + } else if (gap > 1) { + score -= 20; + } + } + return score; +} + +/// The best of [candidates] for [album], or null when none is close enough. +/// Ties keep the earlier one, which the provider ranked higher. +ArtworkCandidate? pickArtworkCandidate( + ArtworkAlbum album, + Iterable candidates, +) { + ArtworkCandidate? best; + var bestScore = artworkMatchThreshold - 1; + for (final candidate in candidates) { + final score = artworkMatchScore(album, candidate); + if (score != null && score > bestScore) { + best = candidate; + bestScore = score; + } + } + return best; +} diff --git a/lib/utils/settings_search_catalog.dart b/lib/utils/settings_search_catalog.dart index 651df7fc7..c8880b20a 100644 --- a/lib/utils/settings_search_catalog.dart +++ b/lib/utils/settings_search_catalog.dart @@ -238,6 +238,18 @@ class SettingsSearchCatalog { 'retag', ], ), + SettingsSearchEntry( + icon: Icons.image_search_outlined, + title: l10n.libraryFindArtwork, + subtitle: l10n.libraryFindArtworkSubtitle, + keywords: const [ + 'album art', + 'cover', + 'artwork', + 'animated cover', + 'motion cover', + ], + ), SettingsSearchEntry( icon: Icons.cleaning_services_outlined, title: l10n.libraryCleanupMissingFiles, diff --git a/test/album_artwork_match_test.dart b/test/album_artwork_match_test.dart new file mode 100644 index 000000000..75f3bff03 --- /dev/null +++ b/test/album_artwork_match_test.dart @@ -0,0 +1,154 @@ +import 'package:flutter_test/flutter_test.dart'; +import 'package:mornye/utils/album_artwork_match.dart'; + +ArtworkCandidate _candidate( + String name, + String artists, { + int tracks = 0, + int? year, + String id = 'a1', +}) => ArtworkCandidate( + providerId: 'provider', + albumId: id, + name: name, + artists: artists, + totalTracks: tracks, + year: year, +); + +void main() { + const album = ArtworkAlbum( + key: 'first light|example band', + album: 'First Light', + artist: 'Example Band', + trackCount: 10, + year: 2019, + ); + + test('normalizes punctuation, case and ampersands', () { + expect(artworkMatchText(' Tom & Jerry! '), 'tom and jerry'); + expect(artworkMatchText('Café: Ünïcode'), 'café ünïcode'); + }); + + test('strips the edition an album title ends with', () { + expect(artworkBaseTitle('First Light (Deluxe Edition)'), 'first light'); + expect(artworkBaseTitle('First Light [Remastered 2011]'), 'first light'); + expect(artworkBaseTitle('First Light - EP'), 'first light'); + expect( + artworkBaseTitle('First Light (Live) [Bonus Tracks]'), + 'first light', + ); + expect(artworkBaseTitle('(Untitled)'), 'untitled'); + }); + + test('needs both the artist and the title', () { + expect( + artworkMatchScore(album, _candidate('First Light', 'Example Band')), + 100, + ); + expect( + artworkMatchScore(album, _candidate('First Light', 'Other Band')), + isNull, + ); + expect( + artworkMatchScore(album, _candidate('Last Light', 'Example Band')), + isNull, + ); + // A featured or joint artist still names the album artist. + expect( + artworkMatchScore( + album, + _candidate('First Light', 'Example Band, Guest Singer'), + ), + 100, + ); + }); + + test('accepts another edition only when nothing speaks against it', () { + final deluxe = _candidate('First Light (Deluxe)', 'Example Band'); + expect(artworkMatchScore(album, deluxe), artworkMatchThreshold); + expect( + pickArtworkCandidate(album, [ + _candidate('First Light (Deluxe)', 'Example Band', year: 2023), + ]), + isNull, + reason: 'A deluxe release four years later is another album', + ); + expect( + pickArtworkCandidate(album, [ + _candidate('First Light', 'Example Band', tracks: 4), + ]), + isNull, + reason: 'A single often shares the title of its album', + ); + expect( + pickArtworkCandidate(album, [ + _candidate('First Light', 'Example Band', tracks: 9), + ]), + isNotNull, + reason: 'One song more locally, such as a bonus track', + ); + expect( + pickArtworkCandidate(album, [ + _candidate('First Light (Live)', 'Example Band', tracks: 4), + ]), + isNull, + reason: 'Another edition with fewer songs than the local album', + ); + }); + + test('prefers the closest release and keeps the first on a tie', () { + final picked = pickArtworkCandidate(album, [ + _candidate('First Light', 'Example Band', id: 'reissue', year: 2024), + _candidate('First Light', 'Example Band', id: 'original', year: 2019), + _candidate('First Light', 'Example Band', id: 'copy', year: 2019), + ]); + expect(picked?.albumId, 'original'); + }); + + test('reads album results and the album of song results', () { + final fromAlbum = ArtworkCandidate.fromSearchResult('provider', { + 'item_type': 'album', + 'id': 'provider:7', + 'name': 'First Light', + 'artists': 'Example Band', + 'total_tracks': '10', + 'release_date': '2019-03-01', + 'cover_url': 'https://example.com/cover.jpg', + }); + expect(fromAlbum?.albumId, '7'); + expect(fromAlbum?.totalTracks, 10); + expect(fromAlbum?.year, 2019); + expect(fromAlbum?.coverUrl, 'https://example.com/cover.jpg'); + + final fromSong = ArtworkCandidate.fromSearchResult('provider', { + 'item_type': 'track', + 'id': 't1', + 'name': 'Opening', + 'artists': 'Example Band, Guest', + 'album_name': 'First Light', + 'album_id': '7', + 'album_artist': 'Example Band', + 'images': 'https://example.com/small.jpg', + }); + expect(fromSong?.name, 'First Light'); + expect(fromSong?.artists, 'Example Band'); + expect(fromSong?.albumId, '7'); + + expect( + ArtworkCandidate.fromSearchResult('provider', { + 'item_type': 'artist', + 'id': 'x', + 'name': 'Example Band', + }), + isNull, + ); + expect( + ArtworkCandidate.fromSearchResult('provider', { + 'item_type': 'track', + 'name': 'No album', + }), + isNull, + ); + }); +} diff --git a/test/library_album_artwork_test.dart b/test/library_album_artwork_test.dart new file mode 100644 index 000000000..0394e521b --- /dev/null +++ b/test/library_album_artwork_test.dart @@ -0,0 +1,254 @@ +import 'dart:io'; + +import 'package:flutter_test/flutter_test.dart'; +import 'package:mornye/services/library_album_artwork.dart'; + +import 'support/sqlite_process_database.dart'; + +void main() { + late Directory root; + late Directory covers; + late SqliteProcessDatabase database; + late LibraryAlbumArtworkStore store; + late DateTime now; + late List searches; + late List downloads; + late Set motion; + late bool searchFails; + + // Albums the fake provider knows, by "artist album" query. + const catalog = { + 'Example Band First Light': { + 'item_type': 'album', + 'id': 'fl', + 'name': 'First Light', + 'artists': 'Example Band', + 'total_tracks': 2, + 'cover_url': 'https://example.com/fl-small.jpg', + }, + 'Other Band Second Sun': { + 'item_type': 'album', + 'id': 'ss', + 'name': 'Second Sun', + 'artists': 'Other Band', + 'cover_url': 'https://example.com/ss-small.jpg', + }, + }; + + setUp(() async { + root = await Directory.systemTemp.createTemp('album-artwork-'); + covers = Directory('${root.path}/library_covers'); + database = await SqliteProcessDatabase.open(path: '${root.path}/lib.db'); + await database.execute( + 'CREATE TABLE library (id TEXT PRIMARY KEY, file_path TEXT, ' + 'album_key TEXT, album_name TEXT, album_artist TEXT, artist_name TEXT, ' + 'release_date TEXT, cover_path TEXT)', + ); + await database.execute( + 'CREATE VIEW library_visible AS SELECT * FROM library', + ); + store = LibraryAlbumArtworkStore( + database: () async => database, + coverDirectory: () async => covers, + ); + now = DateTime(2026, 11, 8); + searches = []; + downloads = []; + motion = {}; + searchFails = false; + Future song( + String id, + String album, + String artist, { + String? cover, + String? path, + }) => database.rawInsert( + 'INSERT INTO library VALUES (?, ?, ?, ?, NULL, ?, NULL, ?)', + [ + id, + path ?? '/music/$id.flac', + '${album.toLowerCase()}|${artist.toLowerCase()}', + album, + artist, + cover, + ], + ); + await song('a1', 'First Light', 'Example Band'); + await song('a2', 'First Light', 'Example Band'); + await song('b1', 'Second Sun', 'Other Band', cover: '/covers/b.jpg'); + await song('c1', 'Unknown Album', 'Unknown Artist'); + await song('d1', 'Lost Tapes', 'Example Band'); + await song('n1', 'First Light', 'Example Band', path: 'network://nas/n1'); + }); + + tearDown(() async { + await database.close(); + await root.delete(recursive: true); + }); + + AlbumArtworkJob job({List providers = const ['empty', 'full']}) => + AlbumArtworkJob( + store: store, + now: () => now, + concurrency: 1, + lookup: AlbumArtworkLookup( + providers: providers, + search: (provider, query) async { + searches.add('$provider:$query'); + if (searchFails) throw StateError('offline'); + final result = catalog[query]; + return provider == 'full' && result != null ? [result] : []; + }, + details: (provider, id) async => { + 'album_info': { + 'cover_url': 'https://example.com/$id-large.jpg', + if (id == 'fl') 'header_video': 'https://example.com/fl.mp4', + }, + }, + ), + downloadCover: (url, path) async { + downloads.add(url); + await File(path).writeAsBytes([1, 2, 3]); + return true; + }, + hasMotion: (album) async => motion.contains(album.album.key), + saveMotion: (album, url) async => motion.add(album.album.key), + ); + + Future> songCovers() async => { + for (final row in await database.rawQuery( + 'SELECT id, cover_path FROM library', + )) + row['id'] as String: row['cover_path'] as String?, + }; + + test('finds covers once per album, for local albums without one', () async { + final summary = await job().run(covers: true, motion: false); + + expect(searches, [ + 'empty:Example Band First Light', + 'full:Example Band First Light', + 'empty:Example Band Lost Tapes', + 'full:Example Band Lost Tapes', + ]); + expect(downloads, ['https://example.com/fl-large.jpg']); + expect(summary.albums, 2); + expect(summary.covers, 1); + expect(summary.songs, 2); + final found = await songCovers(); + expect(found['a1'], startsWith('${covers.path}/album_')); + expect(found['a2'], found['a1']); + expect(File(found['a1']!).existsSync(), isTrue); + expect(found['b1'], '/covers/b.jpg'); + expect(found['c1'], isNull); + expect(found['d1'], isNull); + expect(found['n1'], isNull, reason: 'Network songs keep their own'); + + // Neither album is looked up again until a missing one may have come. + searches.clear(); + await job().run(covers: true, motion: false); + expect(searches, isEmpty); + now = now.add(AlbumArtworkJob.recheckAfter); + await job().run(covers: true, motion: false); + expect(searches, [ + 'empty:Example Band Lost Tapes', + 'full:Example Band Lost Tapes', + ]); + }); + + test('puts found covers back after a scan and a cleared cache', () async { + await job().run(covers: true, motion: false); + final cover = (await songCovers())['a1']!; + + // A scan clears the cover of a song whose file has none. + await database.rawUpdate('UPDATE library SET cover_path = NULL'); + await database.rawUpdate( + "UPDATE library SET cover_path = '/covers/b.jpg' WHERE id = 'b1'", + ); + expect(await store.apply(), 2); + expect((await songCovers())['a1'], cover); + + // Clearing the cache deletes the file; it comes back without a search. + await File(cover).delete(); + searches.clear(); + expect(await job().restore(), greaterThan(0)); + expect(searches, isEmpty); + expect(downloads, [ + 'https://example.com/fl-large.jpg', + 'https://example.com/fl-large.jpg', + ]); + final restored = (await songCovers())['a1']!; + expect(File(restored).existsSync(), isTrue); + expect((await songCovers())['a2'], restored); + }); + + test('a removed cover stays away until the album is looked up', () async { + await job().run(covers: true, motion: false); + final cover = (await songCovers())['a1']!; + const key = 'first light|example band'; + + await store.removeCover(key); + expect((await songCovers())['a1'], isNull); + expect(File(cover).existsSync(), isFalse); + + searches.clear(); + await job().run(covers: true, motion: false); + expect(searches, isEmpty); + expect((await songCovers())['a1'], isNull); + + final summary = await job().run( + covers: true, + motion: false, + albumKey: key, + force: true, + ); + expect(summary.covers, 1); + expect(searches, contains('full:Example Band First Light')); + expect((await songCovers())['a1'], isNotNull); + }); + + test('animated covers are looked up for albums with covers too', () async { + final summary = await job().run(covers: false, motion: true); + + expect(summary.albums, 3); + expect(summary.motion, 1); + expect(summary.covers, 0); + expect(motion, {'first light|example band'}); + expect(searches, contains('full:Other Band Second Sun')); + expect((await songCovers())['a1'], isNull); + + final records = await store.records(); + expect(records['first light|example band']?.motionSaved, isTrue); + expect(records['second sun|other band']?.matched, isTrue); + expect(records['second sun|other band']?.motionUrl, isNull); + + searches.clear(); + await job().run(covers: false, motion: true); + expect(searches, isEmpty); + }); + + test('an album no provider could answer for is tried again', () async { + searchFails = true; + final failed = await job().run(covers: true, motion: false); + expect(failed.failed, 2); + expect(await store.records(), isEmpty); + + searchFails = false; + searches.clear(); + final summary = await job().run(covers: true, motion: false); + expect(summary.covers, 1); + expect(searches, isNotEmpty); + }); + + test('stops between albums when cancelled', () async { + var done = 0; + final summary = await job().run( + covers: true, + motion: false, + cancelled: () => done > 0, + progress: (finished, total) => done = finished, + ); + expect(summary.cancelled, isTrue); + expect(searches, hasLength(2), reason: 'Only the first album'); + }); +} diff --git a/test/library_cleanup_test.dart b/test/library_cleanup_test.dart index f45a17fc6..f03d8b851 100644 --- a/test/library_cleanup_test.dart +++ b/test/library_cleanup_test.dart @@ -232,6 +232,116 @@ void main() { }, ); + group('cover rereads', () { + late Directory root; + late Directory covers; + late SqliteProcessDatabase database; + + setUp(() async { + root = await Directory.systemTemp.createTemp('reread-covers-'); + covers = await Directory('${root.path}/library_covers').create(); + database = await SqliteProcessDatabase.open( + path: '${root.path}/local_library.db', + ); + await database.execute( + 'CREATE TABLE library (id TEXT PRIMARY KEY, file_path TEXT, ' + 'cover_path TEXT, file_mod_time INTEGER, ' + 'audio_metadata_scan_version INTEGER)', + ); + await database.execute( + 'CREATE TABLE library_state (key TEXT PRIMARY KEY, value TEXT)', + ); + }); + + tearDown(() async { + await database.close(); + await root.delete(recursive: true); + }); + + Future> snapshot() async { + final rows = await database.rawQuery( + 'SELECT id, file_mod_time, audio_metadata_scan_version FROM library', + ); + return { + for (final row in rows) + row['id'] as String: libraryIncrementalSnapshotModTime( + storedModTime: row['file_mod_time'] as int, + storedScanVersion: row['audio_metadata_scan_version'] as int, + ), + }; + } + + test('rereads only the songs whose cover file is gone', () async { + await File('${covers.path}/kept.jpg').writeAsBytes([1]); + await Directory('${covers.path}/nested').create(); + await File('${covers.path}/nested/kept.png').writeAsBytes([2]); + // The scan's spelling of the folder may differ from the app's. + const other = '/data/data/app/files/library_covers'; + for (final (id, path, cover) in [ + ('kept', '/music/a.flac', '${covers.path}/kept.jpg'), + ('alias', '/music/b.flac', '$other/kept.jpg'), + ('nested', '/music/c.flac', '${covers.path}/nested/kept.png'), + ('gone', '/music/d.flac', '${covers.path}/gone.jpg'), + ('gone too', '/music/e.flac', '${covers.path}/gone.jpg'), + ('windows', r'C:\Music\f.flac', r'C:\Data\library_covers\lost.jpg'), + ('network', 'network://nas/g.flac', '$other/lost.image'), + ('download', '/music/h.flac', '${root.path}/downloads/h.jpg'), + ('none', '/music/i.flac', null), + ]) { + await database.rawInsert( + 'INSERT INTO library VALUES (?, ?, ?, 1000, 4)', + [id, path, cover], + ); + } + + expect(await markLibraryRowsWithMissingCovers(covers, database), 3); + expect(await snapshot(), { + 'kept': 1000, + 'alias': 1000, + 'nested': 1000, + 'gone': -1, + 'gone too': -1, + 'windows': -1, + 'network': 1000, + 'download': 1000, + 'none': 1000, + }); + + await covers.delete(recursive: true); + expect(await markLibraryRowsWithMissingCovers(covers, database), 3); + expect(await snapshot(), containsPair('kept', -1)); + }); + + test('rereads songs without a cover once', () async { + for (final (id, path, cover) in [ + ('none', '/music/a.flac', null), + ('empty', 'content://tree/b.flac', ''), + ('covered', '/music/c.flac', '${covers.path}/c.jpg'), + ('network', 'network://nas/d.flac', null), + ]) { + await database.rawInsert( + 'INSERT INTO library VALUES (?, ?, ?, 1000, 4)', + [id, path, cover], + ); + } + + expect(await markCoverlessLibraryRowsOnce(database), 2); + expect(await snapshot(), { + 'none': -1, + 'empty': -1, + 'covered': 1000, + 'network': 1000, + }); + + // The scan reads them again; a later run leaves them alone. + await database.rawUpdate( + 'UPDATE library SET audio_metadata_scan_version = 4', + ); + expect(await markCoverlessLibraryRowsOnce(database), 0); + expect(await markCoverlessLibraryRows(database), 2); + }); + }); + test( 'cover cleanup retains recent payloads awaiting a DB reference', () async { diff --git a/test/motion_artwork_store_test.dart b/test/motion_artwork_store_test.dart index 3e8ea1ca2..52eda93be 100644 --- a/test/motion_artwork_store_test.dart +++ b/test/motion_artwork_store_test.dart @@ -40,6 +40,44 @@ void main() { return file; } + test('an alias shows one saved video under another artist', () async { + var downloads = 0; + final store = MotionArtworkStore( + directory: () async => root, + validate: (_) async => true, + download: (_, output) async { + downloads++; + await File(output).writeAsBytes([1]); + return 1; + }, + ); + const guest = (album: 'An Album', artist: 'An Artist feat. A Guest'); + final saved = await store.save( + album, + resolveSource: () async => 'https://example.com/cover.mp4', + ); + expect(await store.find(guest), isNull); + + await store.alias(guest, album); + expect((await store.find(guest))?.source, saved!.source); + // Saving under the alias reuses the video instead of fetching it again. + expect( + (await store.save( + guest, + resolveSource: () async => 'https://example.com/cover.mp4', + ))?.source, + saved.source, + ); + expect(downloads, 1); + + await store.remove(guest); + expect(await store.find(guest), isNull); + expect(await store.find(album), isNotNull); + await store.remove(album); + expect(await store.find(album), isNull); + expect(root.listSync(), isEmpty); + }); + test('successful transfers with broken video data are not cached', () async { final store = MotionArtworkStore( directory: () async => root,