Browse Source

Use improved logic for parsing dateTime from fileName

Neeraj Gupta 2 years ago
parent
commit
ccf09cf19f
3 changed files with 46 additions and 3 deletions
  1. 2 2
      lib/models/file.dart
  2. 43 0
      lib/utils/date_time_util.dart
  3. 1 1
      lib/utils/share_util.dart

+ 2 - 2
lib/models/file.dart

@@ -77,8 +77,8 @@ class File extends EnteFile {
     file.creationTime = asset.createDateTime.microsecondsSinceEpoch;
     if (file.creationTime == null || (file.creationTime! <= jan011991Time)) {
       try {
-        final parsedDateTime =
-            parseDateFromFileName(basenameWithoutExtension(file.title ?? ""));
+        final parsedDateTime = parseDateTimeFromFileNameV2(
+            basenameWithoutExtension(file.title ?? ""));
 
         file.creationTime = parsedDateTime?.microsecondsSinceEpoch ??
             asset.modifiedDateTime.microsecondsSinceEpoch;

+ 43 - 0
lib/utils/date_time_util.dart

@@ -1,3 +1,4 @@
+import 'package:flutter/foundation.dart';
 import 'package:flutter/material.dart';
 import 'package:intl/intl.dart';
 
@@ -269,6 +270,7 @@ bool isValidDate({
   return true;
 }
 
+@Deprecated("Use parseDateTimeV2 ")
 DateTime? parseDateFromFileName(String fileName) {
   if (fileName.startsWith('IMG-') || fileName.startsWith('VID-')) {
 // Whatsapp media files
@@ -288,3 +290,44 @@ DateTime? parseDateFromFileName(String fileName) {
     );
   }
 }
+
+final RegExp exp = RegExp('[A-Za-z]');
+
+DateTime? parseDateTimeFromFileNameV2(String fileName) {
+  String val = fileName.replaceAll(exp, '');
+  if (val.isNotEmpty && !isNumeric(val[0])) {
+    val = val.substring(1, val.length);
+  }
+  if (val.isNotEmpty && !isNumeric(val[val.length - 1])) {
+    val = val.substring(0, val.length - 1);
+  }
+  final int countOfHyphen = val.split("-").length - 1;
+  final int countUnderScore = val.split("_").length - 1;
+  String valForParser = val;
+  if (countOfHyphen == 1) {
+    valForParser = val.replaceAll("-", "T");
+  } else if (countUnderScore == 1 || countUnderScore == 2) {
+    valForParser = val.replaceFirst("_", "T");
+    if (countUnderScore == 2) {
+      valForParser = valForParser.split("_")[0];
+    }
+  } else if (countOfHyphen == 2) {
+    valForParser = val.replaceAll(".", ":");
+  } else if (countOfHyphen == 6) {
+    final splits = val.split("-");
+    valForParser =
+        "${splits[0]}${splits[1]}${splits[2]}T${splits[3]}${splits[4]}${splits[5]}";
+  }
+  final result = DateTime.tryParse(valForParser);
+  if (kDebugMode && result == null) {
+    debugPrint("Failed to parse $fileName dateTime from $valForParser");
+  }
+  return result;
+}
+
+bool isNumeric(String? s) {
+  if (s == null) {
+    return false;
+  }
+  return double.tryParse(s) != null;
+}

+ 1 - 1
lib/utils/share_util.dart

@@ -99,7 +99,7 @@ Future<List<File>> convertIncomingSharedMediaToFile(
     }
     if (enteFile.creationTime == null || enteFile.creationTime == 0) {
       final parsedDateTime =
-          parseDateFromFileName(basenameWithoutExtension(media.path));
+          parseDateTimeFromFileNameV2(basenameWithoutExtension(media.path));
       if (parsedDateTime != null) {
         enteFile.creationTime = parsedDateTime.microsecondsSinceEpoch;
       } else {