Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

[web] Add support for iOS date format

Manav Rathi7215f12d76a1
diff --git a/web/apps/photos/src/services/upload/date.ts b/web/apps/photos/src/services/upload/date.ts
index 44a867ce89..c1e2abf4da 100644
--- a/web/apps/photos/src/services/upload/date.ts
+++ b/web/apps/photos/src/services/upload/date.ts
@@ -1,3 +1,4 @@
+import { nameAndExtension } from "ente-base/file-name";
import log from "ente-base/log";
/**
@@ -64,6 +65,35 @@ const parseEpochMicrosecondsFromFileName = (fileName: string) => {
}
}
+ if (!date) {
+ const [name] = nameAndExtension(fileName);
+
+ if (name.endsWith("_iOS")) {
+ // Parse (some) iOS filenames.
+ //
+ // As reported by a customer, iOS sometimes (unknown exactly when)
+ // does not retain the Exif date but instead puts it in the filename
+ // in a format like `20230427_145116000_iOS.jpg`, and that Apple
+ // Photos can parse it back from there.
+ //
+ // I couldn't find more official documentation from a quick look,
+ // but there do seem to be other people on the internet
+ // corraborating this:
+ //
+ // > iOS 11, (older versions do the same thing), when I take a photo
+ // > or video, the filename consists of the following:
+ // > 20170923_220934000_iOS ... file name is based on UTC/GMT time.
+ // >
+ // > https://discussions.apple.com/thread/8087977
+
+ const p = name.split("_");
+ if (p.length == 3) {
+ const dateString = `${p[0]}-${p[1]}`;
+ date = parseDateFromFusedDateString(dateString);
+ }
+ }
+ }
+
// Generic pattern.
if (!date) {
date = parseDateFromDigitGroups(fileName);
@@ -109,7 +139,8 @@ interface DateComponents {
}
/**
- * Parse a date from a string of the form "YYYYMMDD-HHMMSS".
+ * Parse a date from a string of the form "YYYYMMDD-HHMMSS". Any extra
+ * characters at the end are ignored.
*/
const parseDateFromFusedDateString = (s: string) =>
validateAndGetDateFromComponents({
diff --git a/web/apps/photos/tests/upload.test.ts b/web/apps/photos/tests/upload.test.ts
index b73d2a5085..a9347f092f 100644
--- a/web/apps/photos/tests/upload.test.ts
+++ b/web/apps/photos/tests/upload.test.ts
@@ -5,7 +5,10 @@ import {
getLocalFiles,
groupFilesByCollectionID,
} from "ente-new/photos/services/files";
-import { parseDateFromDigitGroups } from "services/upload/date";
+import {
+ parseDateFromDigitGroups,
+ tryParseEpochMicrosecondsFromFileName,
+} from "services/upload/date";
import {
matchTakeoutMetadata,
metadataJSONMapKeyForJSON,
@@ -59,6 +62,13 @@ const DATE_TIME_PARSING_TEST_FILE_NAMES = [
},
];
+const dateTimeParsingTestFilenames2 = [
+ {
+ fileName: "20170923_220934000_iOS.jpg",
+ expectedDateTime: "2017-09-23 22:09:34",
+ },
+];
+
const DATE_TIME_PARSING_TEST_FILE_NAMES_MUST_FAIL = [
"Snapchat-431959199.mp4.",
"Snapchat-400000000.mp4",
@@ -406,6 +416,22 @@ function parseDateTimeFromFileNameTest() {
}
},
);
+
+ dateTimeParsingTestFilenames2.forEach(({ fileName, expectedDateTime }) => {
+ const epochMicroseconds =
+ tryParseEpochMicrosecondsFromFileName(fileName);
+ const formattedDateTime = getFormattedDateTime(
+ new Date(epochMicroseconds / 1000),
+ );
+ if (formattedDateTime !== expectedDateTime) {
+ throw Error(
+ `parseDateTimeFromFileNameTest2 failed ❌ ,
+ for ${fileName}
+ expected: ${expectedDateTime} got: ${formattedDateTime}`,
+ );
+ }
+ });
+
DATE_TIME_PARSING_TEST_FILE_NAMES_MUST_FAIL.forEach((fileName) => {
const dateTime = parseDateFromDigitGroups(fileName);
if (dateTime) {