From 532452d0af05447074345f736025d80d1133ab05 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 31 Oct 2026 23:03:35 +0000 Subject: [PATCH] Auto convert: name the converted SAF file after the document, not the document URI When the download result carried no file name, the converted name was taken from the last segment of the SAF content URI, i.e. the percent-encoded document id ('primary%3AMusic%2FAlbum%2FSong.opus'). The name is now the part of the document id after the last '/' and ':' ('Song.opus'). Regular file paths are unchanged. --- lib/services/download_file_finalizer.dart | 16 +++++- test/download_file_finalizer_test.dart | 60 +++++++++++++++++++++++ 2 files changed, 75 insertions(+), 1 deletion(-) diff --git a/lib/services/download_file_finalizer.dart b/lib/services/download_file_finalizer.dart index 2186a0c80..22c1519e6 100644 --- a/lib/services/download_file_finalizer.dart +++ b/lib/services/download_file_finalizer.dart @@ -219,7 +219,7 @@ class DownloadFileFinalizer { } final baseName = fileName?.trim().isNotEmpty == true ? fileName!.trim() - : File(filePath).uri.pathSegments.last; + : downloadSourceFileName(filePath); final convertedName = convertedOutputFileName( originalFileName: baseName, targetFormat: targetFormat, @@ -306,6 +306,20 @@ class DownloadFileFinalizer { } } +/// The file name of a downloaded file. A SAF document URI has no file name segment: its +/// last segment is the percent-encoded document id (`primary:Music/Album/Song.flac`), of +/// which the name is the part after the last `/` and `:`. +String downloadSourceFileName(String filePath) { + if (!isContentUri(filePath)) return File(filePath).uri.pathSegments.last; + final segments = Uri.tryParse(filePath)?.pathSegments ?? const []; + var name = segments.isEmpty ? filePath : segments.last; + for (final separator in const ['/', ':']) { + final index = name.lastIndexOf(separator); + if (index >= 0 && index + 1 < name.length) name = name.substring(index + 1); + } + return name; +} + bool _isMp4Container(String path) { final lower = path.toLowerCase(); return lower.endsWith('.m4a') || lower.endsWith('.mp4'); diff --git a/test/download_file_finalizer_test.dart b/test/download_file_finalizer_test.dart index f9f6ca16a..40cb935e9 100644 --- a/test/download_file_finalizer_test.dart +++ b/test/download_file_finalizer_test.dart @@ -332,6 +332,66 @@ void main() { }, ); + for (final uri in [ + 'content://com.android.externalstorage.documents/tree/primary%3AMusic/document/primary%3AMusic%2FAlbum%2FSong.flac', + 'content://com.android.providers.downloads.documents/document/msf%3A1234', + ]) { + test( + 'SAF conversion without a file name never publishes a path as name ($uri)', + () async { + final output = File('${directory.path}/cache2.opus'); + String? published; + final stage = finalizer( + replaceSaf: + ({ + required uri, + required treeUri, + required relativeDir, + required op, + avoidOverwrite = false, + onPublishedFileName, + }) async { + final done = await op('${directory.path}/cache2.flac', (_) {}); + published = done?.$2; + return 'content://converted'; + }, + convert: + ({ + required inputPath, + required targetFormat, + required bitrate, + required deleteOriginal, + }) async => (await output.writeAsString('converted')).path, + ); + await convert(stage, encrypted(), path: uri, name: null, saf: true); + expect(published, isNotNull); + expect(published, isNot(contains('/'))); + expect(published, isNot(contains(':'))); + expect(published, isNot(contains('%'))); + expect(published, endsWith('.opus')); + }, + ); + } + + test('downloadSourceFileName reads the name out of SAF document ids', () { + expect( + downloadSourceFileName( + 'content://com.android.externalstorage.documents/tree/primary%3AMusic/document/primary%3AMusic%2FAlbum%2FSong.flac', + ), + 'Song.flac', + ); + expect( + downloadSourceFileName( + 'content://com.android.providers.downloads.documents/document/msf%3A1234', + ), + '1234', + ); + expect( + downloadSourceFileName('/storage/emulated/0/Music/Song.flac'), + 'Song.flac', + ); + }); + test( 'SAF conversion adopts the collision-safe published name and cleans temps', () async {