diff --git a/lib/l10n/arb/app_en.arb b/lib/l10n/arb/app_en.arb index 89c946bf5..f16e12423 100644 --- a/lib/l10n/arb/app_en.arb +++ b/lib/l10n/arb/app_en.arb @@ -3235,6 +3235,26 @@ "@libraryCleanupMissingFilesSubtitle": { "description": "Subtitle for cleanup button" }, + "libraryFindMovedDownloads": "Find Moved Downloads", + "@libraryFindMovedDownloads": { + "description": "Library action that relinks downloads whose files were moved to the current download folder" + }, + "libraryFindMovedDownloadsSubtitle": "Relink downloads you moved into the current download folder, without removing anything", + "@libraryFindMovedDownloadsSubtitle": { + "description": "Subtitle for the Find Moved Downloads action" + }, + "libraryFindMovedDownloadsResult": "{relinked, plural, =0{No moved downloads found} =1{Relinked 1 download} other{Relinked {relinked} downloads}}{missing, plural, =0{} =1{ · 1 file still missing} other{ · {missing} files still missing}}", + "@libraryFindMovedDownloadsResult": { + "description": "Snackbar after Find Moved Downloads: downloads relinked, then downloads whose files are still missing", + "placeholders": { + "relinked": { + "type": "int" + }, + "missing": { + "type": "int" + } + } + }, "libraryClear": "Clear Library", "@libraryClear": { "description": "Button to clear all library entries" diff --git a/lib/providers/download_history_provider.dart b/lib/providers/download_history_provider.dart index 711a8cdb8..9cc7fa5ab 100644 --- a/lib/providers/download_history_provider.dart +++ b/lib/providers/download_history_provider.dart @@ -858,10 +858,17 @@ class DownloadHistoryNotifier extends Notifier { Map replacementFileNames, Map replacementRelativeDirs, Map pathById, + Set recoveredIds, }) > - _inspectOrphanedEntries(List> entries) async { + _inspectOrphanedEntries( + List> entries, { + bool recoverMovedFiles = false, + }) async { final orphanedIds = []; + // Downloads found in the current download folder after a move; recovery + // has already stored their new paths. + final recoveredIds = {}; final replacementPaths = {}; final replacementFileNames = {}; final replacementRelativeDirs = {}; @@ -896,12 +903,15 @@ class DownloadHistoryNotifier extends Notifier { try { if (await fileExists(filePath)) return MapEntry(id, true); - final moved = Platform.isAndroid + final moved = Platform.isAndroid || recoverMovedFiles ? await DownloadFileRecovery( history: _db, ).recover(filePath, historyId: id, knownMissing: true) : null; - if (moved != null) return MapEntry(id, true); + if (moved != null) { + recoveredIds.add(id); + return MapEntry(id, true); + } final sibling = await _findConvertedSibling(filePath); if (sibling != null) { _historyLog.i( @@ -968,7 +978,10 @@ class DownloadHistoryNotifier extends Notifier { historyId: id, knownMissing: true, ); - if (moved != null) continue; + if (moved != null) { + recoveredIds.add(id); + continue; + } } catch (error) { _historyLog.w( 'Retaining download while its new folder is unavailable: $error', @@ -1015,6 +1028,7 @@ class DownloadHistoryNotifier extends Notifier { replacementFileNames: replacementFileNames, replacementRelativeDirs: replacementRelativeDirs, pathById: pathById, + recoveredIds: recoveredIds, ); } @@ -1253,6 +1267,60 @@ class DownloadHistoryNotifier extends Notifier { return committed.deleted; } + /// Relinks downloads whose files were moved: into the current download + /// folder with the same subfolders and name, or to a converted copy next to + /// the old file. Unlike [cleanupOrphanedDownloads], nothing is removed; + /// downloads whose files cannot be found are only counted as [missing]. + Future<({int relinked, int missing})> findMovedDownloads() async { + _historyLog.i('Looking for moved downloads...'); + final missingIds = {}; + final recoveredIds = {}; + final replacementPaths = {}; + final replacementFileNames = {}; + final replacementRelativeDirs = {}; + final inspectedEntries = >[]; + const pageSize = 256; + var offset = 0; + + while (true) { + final entries = await _db.getEntriesWithPathsPage( + limit: pageSize, + offset: offset, + ); + if (entries.isEmpty) break; + inspectedEntries.addAll(entries); + final result = await _inspectOrphanedEntries( + entries, + recoverMovedFiles: true, + ); + missingIds.addAll(result.orphanedIds); + recoveredIds.addAll(result.recoveredIds); + replacementPaths.addAll(result.replacementPaths); + replacementFileNames.addAll(result.replacementFileNames); + replacementRelativeDirs.addAll(result.replacementRelativeDirs); + if (entries.length < pageSize) break; + // Recovery changes paths, not rows, so the pages stay in place. + offset += entries.length; + } + + var repaired = 0; + if (replacementPaths.isNotEmpty) { + final committed = await _commitInspectedHistoryChanges( + entries: inspectedEntries, + orphanedIds: const [], + replacementPaths: replacementPaths, + replacementFileNames: replacementFileNames, + replacementRelativeDirs: replacementRelativeDirs, + ); + repaired = committed.repaired; + } + final relinked = recoveredIds.length + repaired; + _historyLog.i( + 'Relinked $relinked moved downloads; ${missingIds.length} still missing', + ); + return (relinked: relinked, missing: missingIds.length); + } + Future clearHistory() async { try { await _enqueueHistoryWrite(() async { diff --git a/lib/screens/settings/library_settings_page.dart b/lib/screens/settings/library_settings_page.dart index 9cd3c1caf..1a250cd4d 100644 --- a/lib/screens/settings/library_settings_page.dart +++ b/lib/screens/settings/library_settings_page.dart @@ -42,6 +42,7 @@ class _LibrarySettingsPageState extends ConsumerState { int _androidSdkVersion = 0; bool _hasStoragePermission = false; bool _isCleaningMissingFiles = false; + bool _isFindingMovedDownloads = false; bool _isLoadingReEnrich = false; String _getDisplayPath(String path) { @@ -249,7 +250,9 @@ class _LibrarySettingsPageState extends ConsumerState { } Future _cleanupMissingFiles() async { - if (_isCleaningMissingFiles || ref.read(localLibraryProvider).isScanning) { + if (_isCleaningMissingFiles || + _isFindingMovedDownloads || + ref.read(localLibraryProvider).isScanning) { return; } final library = ref.read(localLibraryProvider.notifier); @@ -292,6 +295,38 @@ class _LibrarySettingsPageState extends ConsumerState { } } + Future _findMovedDownloads() async { + if (_isFindingMovedDownloads || _isCleaningMissingFiles) return; + setState(() => _isFindingMovedDownloads = true); + try { + final result = await ref + .read(downloadHistoryProvider.notifier) + .findMovedDownloads(); + if (!mounted) return; + ScaffoldMessenger.of(context).showSnackBar( + SnackBar( + content: Text( + context.l10n.libraryFindMovedDownloadsResult( + result.relinked, + result.missing, + ), + ), + ), + ); + } catch (error) { + if (!mounted) return; + ScaffoldMessenger.of(context).showSnackBar( + SnackBar( + content: Text( + context.l10n.snackbarError(context.friendlyError(error)), + ), + ), + ); + } finally { + if (mounted) setState(() => _isFindingMovedDownloads = false); + } + } + Future _removeSource(LocalLibrarySource source) async { final confirmed = await showAppConfirmDialog( context, @@ -725,8 +760,13 @@ class _LibrarySettingsPageState extends ConsumerState { (state) => state.totalCount > 0, ), ); + final canFindMoved = + !_isFindingMovedDownloads && + !_isCleaningMissingFiles && + hasDownloads; final canCleanup = !_isCleaningMissingFiles && + !_isFindingMovedDownloads && !libraryState.isScanning && (libraryState.totalCount > 0 || hasDownloads || @@ -792,6 +832,24 @@ class _LibrarySettingsPageState extends ConsumerState { ), ), ], + Opacity( + opacity: canFindMoved || _isFindingMovedDownloads + ? 1.0 + : 0.5, + child: SettingsItem( + icon: Icons.manage_search, + title: context.l10n.libraryFindMovedDownloads, + subtitle: + context.l10n.libraryFindMovedDownloadsSubtitle, + trailing: _isFindingMovedDownloads + ? const AppLoadingIndicator( + size: 20, + strokeWidth: 2, + ) + : null, + onTap: canFindMoved ? _findMovedDownloads : null, + ), + ), Opacity( opacity: canCleanup || _isCleaningMissingFiles ? 1.0 diff --git a/lib/utils/settings_search_catalog.dart b/lib/utils/settings_search_catalog.dart index 81d0a435e..30154f84c 100644 --- a/lib/utils/settings_search_catalog.dart +++ b/lib/utils/settings_search_catalog.dart @@ -244,6 +244,12 @@ class SettingsSearchCatalog { subtitle: l10n.libraryCleanupMissingFilesSubtitle, keywords: const ['remove missing'], ), + SettingsSearchEntry( + icon: Icons.manage_search, + title: l10n.libraryFindMovedDownloads, + subtitle: l10n.libraryFindMovedDownloadsSubtitle, + keywords: const ['moved files', 'relink', 'resync', 'missing files'], + ), SettingsSearchEntry( icon: Icons.delete_outline, title: l10n.libraryClear, diff --git a/test/find_moved_downloads_test.dart b/test/find_moved_downloads_test.dart new file mode 100644 index 000000000..a635d5537 --- /dev/null +++ b/test/find_moved_downloads_test.dart @@ -0,0 +1,189 @@ +import 'dart:convert'; +import 'dart:io'; + +import 'package:flutter_riverpod/flutter_riverpod.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:mornye/models/settings.dart'; +import 'package:mornye/providers/download_history_provider.dart'; +import 'package:mornye/services/download_file_recovery.dart'; +import 'package:mornye/services/history_database.dart'; +import 'package:path/path.dart' as p; +import 'package:shared_preferences/shared_preferences.dart'; + +/// History rows in memory. Any method not listed here, such as deleting +/// entries, fails the test. +class _History extends Fake implements HistoryDatabase { + _History(Iterable items) + : rows = {for (final item in items) item.id: item}; + + final Map rows; + + @override + Future>> getEntriesWithPathsPage({ + required int limit, + int offset = 0, + }) async => [ + for (final item in rows.values.skip(offset).take(limit)) + { + 'id': item.id, + 'file_path': item.filePath, + 'storage_mode': item.storageMode, + 'download_tree_uri': item.downloadTreeUri, + 'saf_relative_dir': item.safRelativeDir, + 'saf_file_name': item.safFileName, + 'downloaded_at': item.downloadedAt.toUtc().toIso8601String(), + }, + ]; + + @override + Future?> getById(String id) async => rows[id]?.toJson(); + + @override + Future?> findByFilePath(String path) async => + rows.values.where((item) => item.filePath == path).firstOrNull?.toJson(); + + @override + Future updateFilePath( + String id, + String newFilePath, { + String? newStorageMode, + String? newDownloadTreeUri, + String? newSafFileName, + String? newSafRelativeDir, + String? newQuality, + int? newBitDepth, + int? newSampleRate, + int? newBitrate, + String? newFormat, + bool clearAudioSpecs = false, + String? expectedFilePath, + String? expectedDownloadedAt, + }) async { + final item = rows[id]; + if (item == null || + (expectedFilePath != null && item.filePath != expectedFilePath)) { + return false; + } + rows[id] = item.copyWith(filePath: newFilePath); + return true; + } +} + +class _Notifier extends DownloadHistoryNotifier { + _Notifier(this.history) : super(database: history); + + final _History history; + + @override + DownloadHistoryState build() => DownloadHistoryState( + items: history.rows.values.toList(), + totalCount: history.rows.length, + ); +} + +/// [DownloadFileRecovery] follows only `/` paths and Android SAF folders, so +/// a file moved between drive-letter folders is not recovered on Windows. +final String? _movedFilesSkip = Platform.isWindows + ? 'Moved files are only recovered on / paths and SAF folders' + : null; + +DownloadHistoryItem _download(String id, String path) => DownloadHistoryItem( + id: id, + trackName: 'Song $id', + artistName: 'Artist', + albumName: 'Album', + filePath: path, + service: 'example', + downloadedAt: DateTime.utc(2026, 1, 1), +); + +void main() { + TestWidgetsFlutterBinding.ensureInitialized(); + late Directory root; + late String oldFolder; + late String newFolder; + + setUp(() async { + root = await Directory.systemTemp.createTemp('moved_downloads_'); + oldFolder = p.join(root.path, 'old'); + newFolder = p.join(root.path, 'new'); + await Directory(oldFolder).create(); + await Directory(newFolder).create(); + SharedPreferences.setMockInitialValues({ + 'app_settings': jsonEncode( + AppSettings(downloadDirectory: newFolder).toJson(), + ), + DownloadFileRecovery.previousDirectoriesKey: [oldFolder], + }); + }); + tearDown(() => root.delete(recursive: true)); + + Future file(String path) async { + final created = File(path); + await created.parent.create(recursive: true); + return created.writeAsString('audio'); + } + + Future<({int relinked, int missing})> run(_History history) async { + final container = ProviderContainer( + overrides: [ + downloadHistoryProvider.overrideWith(() => _Notifier(history)), + ], + ); + addTearDown(container.dispose); + return container + .read(downloadHistoryProvider.notifier) + .findMovedDownloads(); + } + + test('relinks moved downloads and only counts the missing ones', () async { + final movedOld = p.join(oldFolder, 'Artist', 'Album', '01 - Song.flac'); + final movedNew = p.join(newFolder, 'Artist', 'Album', '01 - Song.flac'); + final present = p.join(newFolder, 'Artist', 'Album', '02 - Kept.flac'); + final gone = p.join(oldFolder, 'Artist', 'Album', '03 - Gone.flac'); + final elsewhere = p.join(oldFolder, 'Other', '04 - Elsewhere.flac'); + await file(movedNew); + await file(present); + // Same file name in another folder of the new download folder: not a + // match, as the subfolders differ. + await file(p.join(newFolder, 'Somewhere', '04 - Elsewhere.flac')); + + final history = _History([ + _download('moved', movedOld), + _download('present', present), + _download('gone', gone), + _download('elsewhere', elsewhere), + ]); + final result = await run(history); + + expect(result.relinked, 1); + expect(result.missing, 2); + expect(history.rows['moved']!.filePath, movedNew); + expect(history.rows['present']!.filePath, present); + // Nothing is removed from History. + expect(history.rows.keys, {'moved', 'present', 'gone', 'elsewhere'}); + expect(history.rows['gone']!.filePath, gone); + expect(history.rows['elsewhere']!.filePath, elsewhere); + }, skip: _movedFilesSkip); + + test('relinks a download converted next to its old file', () async { + final original = p.join(newFolder, 'Artist', 'Album', '01 - Song.flac'); + final converted = p.join(newFolder, 'Artist', 'Album', '01 - Song.opus'); + await file(converted); + + final history = _History([_download('converted', original)]); + final result = await run(history); + + expect(result, (relinked: 1, missing: 0)); + expect(history.rows['converted']!.filePath, converted); + }); + + test('reports nothing when every download is in place', () async { + final present = p.join(newFolder, 'Song.flac'); + await file(present); + + final history = _History([_download('present', present)]); + expect(await run(history), (relinked: 0, missing: 0)); + expect(history.rows['present']!.filePath, present); + }); +} diff --git a/test/library_settings_cleanup_test.dart b/test/library_settings_cleanup_test.dart index cd81d1770..8a232dd7b 100644 --- a/test/library_settings_cleanup_test.dart +++ b/test/library_settings_cleanup_test.dart @@ -34,16 +34,21 @@ class _Library extends LocalLibraryNotifier { } class _History extends DownloadHistoryNotifier { - _History(this._count, this._cleanup); + _History(this._count, this._cleanup, {this.findMoved}); final int _count; final Future Function() _cleanup; + final Future<({int relinked, int missing})> Function()? findMoved; @override DownloadHistoryState build() => DownloadHistoryState(totalCount: _count); @override Future cleanupOrphanedDownloads() => _cleanup(); + + @override + Future<({int relinked, int missing})> findMovedDownloads() => + findMoved?.call() ?? Future.value((relinked: 0, missing: 0)); } class _RetryLibrary extends _Library { @@ -76,6 +81,10 @@ Finder get _cleanupRow => find.byWidgetPredicate( (widget) => widget is SettingsItem && widget.title == 'Cleanup Missing Files', ); +Finder get _findMovedRow => find.byWidgetPredicate( + (widget) => widget is SettingsItem && widget.title == 'Find Moved Downloads', +); + Future _mount( WidgetTester tester, { required bool mornye, @@ -138,6 +147,9 @@ void main() { library: library, history: _History(0, () async => 0), ); + // The folder rows sit above the actions the setup scrolled to. + await tester.drag(find.byType(Scrollable).first, const Offset(0, 3000)); + await tester.pumpAndSettle(); await tester.scrollUntilVisible(find.text('Saved music'), 200); await tester.pumpAndSettle(); expect(find.textContaining(error), findsOneWidget); @@ -247,6 +259,97 @@ void main() { expect(tester.takeException(), isNull); }); + testWidgets('Find Moved Downloads relinks without removing ($mornye)', ( + tester, + ) async { + final done = Completer<({int relinked, int missing})>(); + var findCalls = 0; + var cleanupCalls = 0; + await _mount( + tester, + mornye: mornye, + library: _Library(LocalLibraryState(), () async { + cleanupCalls++; + return 0; + }), + history: _History( + 3, + () async { + cleanupCalls++; + return 0; + }, + findMoved: () { + findCalls++; + return done.future; + }, + ), + ); + await tester.ensureVisible(find.text('Find Moved Downloads')); + await tester.pumpAndSettle(); + expect(tester.widget(_findMovedRow).onTap, isNotNull); + await tester.tap(find.text('Find Moved Downloads')); + await tester.pump(); + expect(findCalls, 1); + expect( + find.descendant( + of: _findMovedRow, + matching: find.byType(AppLoadingIndicator), + ), + findsOneWidget, + ); + // Neither action can start while the other runs. + expect(tester.widget(_findMovedRow).onTap, isNull); + expect(tester.widget(_cleanupRow).onTap, isNull); + + done.complete((relinked: 2, missing: 1)); + await tester.pumpAndSettle(); + expect( + find.text('Relinked 2 downloads · 1 file still missing'), + findsOneWidget, + ); + expect(cleanupCalls, 0); + expect(tester.widget(_findMovedRow).onTap, isNotNull); + expect(tester.widget(_cleanupRow).onTap, isNotNull); + expect(tester.takeException(), isNull); + }); + + testWidgets('Find Moved Downloads reports errors ($mornye)', ( + tester, + ) async { + await _mount( + tester, + mornye: mornye, + library: _Library(LocalLibraryState(), () async => 0), + history: _History( + 1, + () async => 0, + findMoved: () async => throw StateError('folder unavailable'), + ), + ); + await tester.ensureVisible(find.text('Find Moved Downloads')); + await tester.pumpAndSettle(); + await tester.tap(find.text('Find Moved Downloads')); + await tester.pumpAndSettle(); + expect(find.textContaining('Error:'), findsOneWidget); + expect(tester.widget(_findMovedRow).onTap, isNotNull); + expect(tester.takeException(), isNull); + }); + + testWidgets('Find Moved Downloads needs downloads ($mornye)', ( + tester, + ) async { + await _mount( + tester, + mornye: mornye, + library: _Library(LocalLibraryState(totalCount: 4), () async => 0), + history: _History(0, () async => 0), + ); + await tester.ensureVisible(find.text('Find Moved Downloads')); + await tester.pumpAndSettle(); + expect(tester.widget(_findMovedRow).onTap, isNull); + expect(tester.takeException(), isNull); + }); + testWidgets('cleanup stays available for hidden source rows ($mornye)', ( tester, ) async { @@ -304,4 +407,21 @@ void main() { expect(tester.takeException(), isNull); }); } + + test('the Find Moved Downloads result reads naturally', () async { + final l10n = await AppLocalizations.delegate.load(const Locale('en')); + expect( + l10n.libraryFindMovedDownloadsResult(0, 0), + 'No moved downloads found', + ); + expect(l10n.libraryFindMovedDownloadsResult(1, 0), 'Relinked 1 download'); + expect( + l10n.libraryFindMovedDownloadsResult(0, 3), + 'No moved downloads found · 3 files still missing', + ); + expect( + l10n.libraryFindMovedDownloadsResult(5, 1), + 'Relinked 5 downloads · 1 file still missing', + ); + }); }