Skip to content

An open-source cross-platform alternative to AirDrop

fix: preserve extension case and leading dot in FilePathStringExt.withCount (#3756)

lingyicute / lyi 梨89881acb32c9
diff --git a/app/test/unit/util/file_path_helper_test.dart b/app/test/unit/util/file_path_helper_test.dart
index 99e7e9d4..9c6213c5 100644
--- a/app/test/unit/util/file_path_helper_test.dart
+++ b/app/test/unit/util/file_path_helper_test.dart
@@ -14,4 +14,18 @@ void main() {
expect('myFile'.withCount(1), 'myFile (1)');
expect('myFile (1)'.withCount(2), 'myFile (2)');
});
+
+ test('fileName with counter keeps the extension', () {
+ expect('photo.jpg'.withCount(2), 'photo (2).jpg');
+ expect('photo (1).jpg'.withCount(3), 'photo (3).jpg');
+ });
+
+ test('fileName with counter keeps the case of the extension', () {
+ expect('Photo.JPG'.withCount(2), 'Photo (2).JPG');
+ });
+
+ test('fileName with counter treats a leading dot as part of the name', () {
+ expect('.bashrc'.withCount(2), '.bashrc (2)');
+ expect('.config.json'.withCount(2), '.config (2).json');
+ });
}
diff --git a/packages/localsend_isolates/lib/util/file_path_helper.dart b/packages/localsend_isolates/lib/util/file_path_helper.dart
index a6513cef..ab70ff81 100644
--- a/packages/localsend_isolates/lib/util/file_path_helper.dart
+++ b/packages/localsend_isolates/lib/util/file_path_helper.dart
@@ -33,9 +33,11 @@ extension FilePathStringExt on String {
final index = lastIndexOf('.');
final String fileName;
final String extension;
- if (index != -1) {
+ // A leading dot (e.g. ".bashrc") belongs to the name of a hidden file and does not start an extension.
+ // The extension keeps its original case, the counter must not rename "Photo.JPG" to "Photo (2).jpg".
+ if (index > 0) {
fileName = substring(0, index);
- extension = substring(index + 1).toLowerCase();
+ extension = substring(index + 1);
} else {
fileName = this;
extension = '';