From e33d2d7089ea887877e8917c7955dc1658c9c6aa Mon Sep 17 00:00:00 2001 From: zarzet <42882290+zarzet@users.noreply.github.com> Date: Fri, 30 Oct 2026 15:38:42 +0700 Subject: [PATCH] fix(covers): keep temporary player covers out of Loved and recents A song loved from the player without a Library record stored the item's artUri as its collection cover, and an album or artist opened from the player recorded its cover in recents. Older builds put loopback URLs there; a tag cover the store could not keep still shows from a temporary file on that play. Both writers now store only covers that outlast the run, through durableCoverReference. --- lib/providers/recent_access_provider.dart | 5 +-- lib/services/library_media_identity.dart | 6 ++-- test/library_media_identity_test.dart | 32 ++++++++++++++++++++ test/recent_access_persistence_test.dart | 37 +++++++++++++++++++++++ 4 files changed, 76 insertions(+), 4 deletions(-) create mode 100644 test/library_media_identity_test.dart diff --git a/lib/providers/recent_access_provider.dart b/lib/providers/recent_access_provider.dart index 947083e94..bf7066145 100644 --- a/lib/providers/recent_access_provider.dart +++ b/lib/providers/recent_access_provider.dart @@ -2,6 +2,7 @@ import 'dart:async'; import 'dart:convert'; import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:mornye/services/app_state_database.dart'; +import 'package:mornye/services/cover_store.dart'; import 'package:mornye/utils/logger.dart'; const _maxRecentItems = 20; @@ -195,7 +196,7 @@ class RecentAccessNotifier extends Notifier { RecentAccessItem( id: id, name: name, - imageUrl: imageUrl, + imageUrl: durableCoverReference(imageUrl), type: RecentAccessType.artist, accessedAt: DateTime.now(), providerId: providerId, @@ -215,7 +216,7 @@ class RecentAccessNotifier extends Notifier { id: id, name: name, subtitle: artistName, - imageUrl: imageUrl, + imageUrl: durableCoverReference(imageUrl), type: RecentAccessType.album, accessedAt: DateTime.now(), providerId: providerId, diff --git a/lib/services/library_media_identity.dart b/lib/services/library_media_identity.dart index 1e5f3761d..761842724 100644 --- a/lib/services/library_media_identity.dart +++ b/lib/services/library_media_identity.dart @@ -2,6 +2,7 @@ import 'package:audio_service/audio_service.dart' show MediaItem; import 'package:mornye/models/download_history.dart'; import 'package:mornye/models/track.dart'; import 'package:mornye/models/unified_library_item.dart'; +import 'package:mornye/services/cover_store.dart'; import 'package:mornye/services/library_database.dart'; /// The download or Library record of the file a media item plays: by its @@ -36,7 +37,8 @@ Future libraryItemForMedia({ } /// The collection track that loves [item]: its Library record's identity -/// when it has one ([match]), otherwise the media item's own fields. +/// when it has one ([match]), otherwise the media item's own fields, with +/// no cover when the item shows one that does not outlast this run. Track collectionTrackForMedia(MediaItem item, UnifiedLibraryItem? match) => match?.toTrack() ?? Track( @@ -44,7 +46,7 @@ Track collectionTrackForMedia(MediaItem item, UnifiedLibraryItem? match) => name: item.title, artistName: item.artist ?? '', albumName: item.album ?? '', - coverUrl: item.artUri?.toString(), + coverUrl: durableCoverReference(item.artUri?.toString()), duration: item.duration?.inSeconds ?? 0, source: 'local', ); diff --git a/test/library_media_identity_test.dart b/test/library_media_identity_test.dart new file mode 100644 index 000000000..3552583ec --- /dev/null +++ b/test/library_media_identity_test.dart @@ -0,0 +1,32 @@ +import 'dart:io'; + +import 'package:audio_service/audio_service.dart' show MediaItem; +import 'package:flutter_test/flutter_test.dart'; +import 'package:mornye/services/library_media_identity.dart'; + +void main() { + test('a loved song keeps only a cover that outlasts this run', () { + String? cover(String artUri) => collectionTrackForMedia( + MediaItem( + id: 'network://nas/Album/song.flac', + title: 'Song', + artUri: Uri.parse(artUri), + ), + null, + ).coverUrl; + + for (final durable in [ + 'file:///data/user/0/app/files/covers/a_b.image', + 'https://example.com/cover.jpg', + ]) { + expect(cover(durable), durable); + } + for (final temporary in [ + 'http://127.0.0.1:41234/7f3a', + 'file:///data/user/0/app/cache/network_metadata/a1.jpg', + Uri.file('${Directory.systemTemp.path}/cover.jpg').toString(), + ]) { + expect(cover(temporary), isNull, reason: temporary); + } + }); +} diff --git a/test/recent_access_persistence_test.dart b/test/recent_access_persistence_test.dart index d4b7a1048..79686096f 100644 --- a/test/recent_access_persistence_test.dart +++ b/test/recent_access_persistence_test.dart @@ -255,6 +255,43 @@ void main() { ); }); + test( + 'albums and artists opened from the player store no temporary cover', + () async { + final database = _Database(); + final container = _container(database); + addTearDown(container.dispose); + final notifier = container.read(recentAccessProvider.notifier); + database.releaseRead.complete(); + await _waitUntilLoaded(container); + const kept = 'https://example.com/album.jpg'; + for (final (id, cover) in [ + ('temporary', '/data/user/0/app/cache/network_metadata/a1.jpg'), + ('loopback', 'http://127.0.0.1:41234/7f3a'), + ('kept', kept), + ]) { + notifier + ..recordAlbumAccess(id: id, name: id, imageUrl: cover) + ..recordArtistAccess(id: id, name: id, imageUrl: cover); + } + await database.whenCompleted('record:artist:default:kept'); + expect( + { + for (final item in database.items.values) + '${item.type.name}:${item.id}': item.imageUrl, + }, + { + 'album:temporary': null, + 'artist:temporary': null, + 'album:loopback': null, + 'artist:loopback': null, + 'album:kept': kept, + 'artist:kept': kept, + }, + ); + }, + ); + test('failed load preserves early state and still owns its writes', () async { final database = _Database()..failRead = true; final container = _container(database);