Skip to content

An open-source cross-platform alternative to AirDrop

Update files name duplicate counter (#2391) (#2455)

Ivan Saprykin3ea7f142f032
diff --git a/app/lib/util/file_path_helper.dart b/app/lib/util/file_path_helper.dart
index a3387d12..dd38783b 100644
--- a/app/lib/util/file_path_helper.dart
+++ b/app/lib/util/file_path_helper.dart
@@ -1,7 +1,7 @@
import 'package:common/model/file_type.dart';
-/// Matches myFile-123 -> 123
-final _fileNumberRegex = RegExp(r'^(.*)(?:-(\d+))$');
+/// Matches myFile (123) -> "myFile", " (123)"
+final _fileNumberRegex = RegExp(r'^(.*)(?:(\s\(\d+\)))$');
extension FilePathStringExt on String {
String get extension {
@@ -43,9 +43,9 @@ extension FilePathStringExt on String {
final match = _fileNumberRegex.firstMatch(fileName);
if (match != null) {
- return '${match.group(1)}-$count'.withExtension(extension);
+ return '${match.group(1)} ($count)'.withExtension(extension);
} else {
- return '$fileName-$count'.withExtension(extension);
+ return '$fileName ($count)'.withExtension(extension);
}
}
diff --git a/app/test/unit/util/file_path_helper_test.dart b/app/test/unit/util/file_path_helper_test.dart
new file mode 100644
index 00000000..dc9812f5
--- /dev/null
+++ b/app/test/unit/util/file_path_helper_test.dart
@@ -0,0 +1,9 @@
+import 'package:localsend_app/util/file_path_helper.dart';
+import 'package:test/test.dart';
+
+void main() {
+ test('fileName with counter', () async {
+ expect('myFile'.withCount(1), 'myFile (1)');
+ expect('myFile (1)'.withCount(2), 'myFile (2)');
+ });
+}