From 56d04066eaa913145fe9b61e5b35fe4bbd4fc667 Mon Sep 17 00:00:00 2001 From: Manav Rathi Date: Sat, 13 Apr 2024 20:31:55 +0530 Subject: [PATCH] sfn --- .../photos/src/services/export/migration.ts | 18 +++++++++------- web/apps/photos/src/utils/native-fs.ts | 21 +++++++++++++------ web/docs/dependencies.md | 7 +++++++ 3 files changed, 33 insertions(+), 13 deletions(-) diff --git a/web/apps/photos/src/services/export/migration.ts b/web/apps/photos/src/services/export/migration.ts index 49265cf348..cb785749d3 100644 --- a/web/apps/photos/src/services/export/migration.ts +++ b/web/apps/photos/src/services/export/migration.ts @@ -26,7 +26,7 @@ import { getPersonalFiles, mergeMetadata, } from "utils/file"; -import { sanitizeName } from "utils/native-fs"; +import { sanitizeFilename } from "utils/native-fs"; import { ENTE_METADATA_FOLDER, getCollectionIDFromFileUID, @@ -502,10 +502,10 @@ const getUniqueCollectionFolderPath = async ( dir: string, collectionName: string, ): Promise => { - let collectionFolderPath = `${dir}/${sanitizeName(collectionName)}`; + let collectionFolderPath = `${dir}/${sanitizeFilename(collectionName)}`; let count = 1; while (await exportService.exists(collectionFolderPath)) { - collectionFolderPath = `${dir}/${sanitizeName( + collectionFolderPath = `${dir}/${sanitizeFilename( collectionName, )}(${count})`; count++; @@ -520,14 +520,16 @@ const getUniqueFileSaveName = async ( collectionPath: string, filename: string, ) => { - let fileSaveName = sanitizeName(filename); + let fileSaveName = sanitizeFilename(filename); let count = 1; while ( await exportService.exists( getFileSavePath(collectionPath, fileSaveName), ) ) { - const filenameParts = splitFilenameAndExtension(sanitizeName(filename)); + const filenameParts = splitFilenameAndExtension( + sanitizeFilename(filename), + ); if (filenameParts[1]) { fileSaveName = `${filenameParts[0]}(${count}).${filenameParts[1]}`; } else { @@ -570,14 +572,16 @@ const getUniqueFileExportNameForMigration = ( filename: string, usedFilePaths: Map>, ) => { - let fileExportName = sanitizeName(filename); + let fileExportName = sanitizeFilename(filename); let count = 1; while ( usedFilePaths .get(collectionPath) ?.has(getFileSavePath(collectionPath, fileExportName)) ) { - const filenameParts = splitFilenameAndExtension(sanitizeName(filename)); + const filenameParts = splitFilenameAndExtension( + sanitizeFilename(filename), + ); if (filenameParts[1]) { fileExportName = `${filenameParts[0]}(${count}).${filenameParts[1]}`; } else { diff --git a/web/apps/photos/src/utils/native-fs.ts b/web/apps/photos/src/utils/native-fs.ts index f52dea81ea..22aba96e48 100644 --- a/web/apps/photos/src/utils/native-fs.ts +++ b/web/apps/photos/src/utils/native-fs.ts @@ -4,8 +4,15 @@ import { splitFilenameAndExtension } from "utils/file"; export const ENTE_TRASH_FOLDER = "Trash"; -export const sanitizeName = (name: string) => - sanitize(name, { replacement: "_" }); +/** + * Sanitize string for use as file or directory name. + * + * Return a string suitable for use as a file or directory name by replacing + * directory separators and invalid characters in the input string {@link s} + * with "_". + */ +export const sanitizeFilename = (s: string) => + sanitize(s, { replacement: "_" }); const exists = (path: string) => ensureElectron().fs.exists(path); @@ -13,13 +20,13 @@ export const getUniqueCollectionExportName = async ( dir: string, collectionName: string, ): Promise => { - let collectionExportName = sanitizeName(collectionName); + let collectionExportName = sanitizeFilename(collectionName); let count = 1; while ( (await exists(`${dir}/${collectionExportName}`)) || collectionExportName === ENTE_TRASH_FOLDER ) { - collectionExportName = `${sanitizeName(collectionName)}(${count})`; + collectionExportName = `${sanitizeFilename(collectionName)}(${count})`; count++; } return collectionExportName; @@ -29,10 +36,12 @@ export const getUniqueFileExportName = async ( collectionExportPath: string, filename: string, ) => { - let fileExportName = sanitizeName(filename); + let fileExportName = sanitizeFilename(filename); let count = 1; while (await exists(`${collectionExportPath}/${fileExportName}`)) { - const filenameParts = splitFilenameAndExtension(sanitizeName(filename)); + const filenameParts = splitFilenameAndExtension( + sanitizeFilename(filename), + ); if (filenameParts[1]) { fileExportName = `${filenameParts[0]}(${count}).${filenameParts[1]}`; } else { diff --git a/web/docs/dependencies.md b/web/docs/dependencies.md index 285adc8da5..d0660bb3e8 100644 --- a/web/docs/dependencies.md +++ b/web/docs/dependencies.md @@ -130,3 +130,10 @@ For some of our newer code, we have started to use [Vite](https://vitejs.dev). It is more lower level than Next, but the bells and whistles it doesn't have are the bells and whistles (and the accompanying complexity) that we don't need in some cases. + +## Photos + +### Misc + +- "sanitize-filename" is for converting arbitrary strings into strings that + are suitable for being used as filenames.