Skip to content

armgit / ente

Public Imported from GitHub
0

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

Audit (and minor fix)

Manav Rathi0b73c92ee6ff
diff --git a/web/packages/gallery/services/upload/date.ts b/web/packages/gallery/services/upload/date.ts
index 054fb358bb..3b200bf83c 100644
--- a/web/packages/gallery/services/upload/date.ts
+++ b/web/packages/gallery/services/upload/date.ts
@@ -1,6 +1,3 @@
-// TODO: Audit this file
-/* eslint-disable @typescript-eslint/no-unnecessary-condition */
-/* eslint-disable @typescript-eslint/ban-ts-comment */
import { nameAndExtension } from "ente-base/file-name";
import log from "ente-base/log";
@@ -27,10 +24,10 @@ export const tryParseEpochMicrosecondsFromFileName = (
};
// Not sure why we have a try catch, but until there is a chance to validate
-// that it doesn't indeed throw, move out the actual logic into this separate
-// and more readable function.
+// that it doesn't indeed throw, keep this actual logic into this separate and
+// more readable function.
const parseEpochMicrosecondsFromFileName = (fileName: string) => {
- let date: Date;
+ let date: Date | undefined;
fileName = fileName.trim();
@@ -38,15 +35,16 @@ const parseEpochMicrosecondsFromFileName = (fileName: string) => {
if (fileName.startsWith("IMG-") || fileName.startsWith("VID-")) {
// WhatsApp media files
// - e.g. "IMG-20171218-WA0028.jpg"
- // @ts-ignore
- date = parseDateFromFusedDateString(fileName.split("-")[1]);
+ const p = fileName.split("-");
+ const dateString = p[1];
+ if (dateString) {
+ date = parseDateFromFusedDateString(dateString);
+ }
} else if (fileName.startsWith("Screenshot_")) {
// Screenshots on Android
// - e.g. "Screenshot_20181227-152914.jpg"
- // @ts-ignore
- date = parseDateFromFusedDateString(
- fileName.replaceAll("Screenshot_", ""),
- );
+ const dateString = fileName.replace("Screenshot_", "");
+ date = parseDateFromFusedDateString(dateString);
} else if (fileName.startsWith("signal-")) {
// Signal images
//
@@ -63,16 +61,13 @@ const parseEpochMicrosecondsFromFileName = (fileName: string) => {
const p = fileName.split("-");
if (p.length > 5) {
const dateString = `${p[1]}${p[2]}${p[3]}-${p[4]}${p[5]}${p[6]}`;
- // @ts-ignore
date = parseDateFromFusedDateString(dateString);
- } else {
- const dateString = `${p[1]}${p[2]}${p[3]}-${p[4]}`;
- // @ts-ignore
+ } else if (p.length > 1) {
+ const dateString = `${p[1]}${p[2] ?? ""}${p[3] ?? ""}-${p[4] ?? ""}`;
date = parseDateFromFusedDateString(dateString);
}
}
- // @ts-ignore
if (!date) {
const [name] = nameAndExtension(fileName);
@@ -97,16 +92,13 @@ const parseEpochMicrosecondsFromFileName = (fileName: string) => {
const p = name.split("_");
if (p.length == 3) {
const dateString = `${p[0]}-${p[1]}`;
- // @ts-ignore
date = parseDateFromFusedDateString(dateString);
}
}
}
// Generic pattern.
- // @ts-ignore
if (!date) {
- // @ts-ignore
date = parseDateFromDigitGroups(fileName);
}
@@ -131,8 +123,6 @@ const parseEpochMicrosecondsFromFileName = (fileName: string) => {
}
};
-const currentYear = new Date().getFullYear();
-
/**
* An intermediate data structure we use for the functions in this file. It
* stores the various components of a JavaScript date.
@@ -206,7 +196,10 @@ const validateAndGetDateFromComponents = (components: DateComponents) => {
if (!isDatePartValid(date, components)) {
return undefined;
}
- if (date.getFullYear() < 1990 || date.getFullYear() > currentYear + 1) {
+ if (
+ date.getFullYear() < 1990 ||
+ date.getFullYear() > new Date().getFullYear() + 1
+ ) {
return undefined;
}
return date;