This commit is contained in:
Manav Rathi 2024-04-13 20:31:55 +05:30
parent 7838f7484d
commit 56d04066ea
No known key found for this signature in database
3 changed files with 33 additions and 13 deletions

View file

@ -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<string> => {
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<string, Set<string>>,
) => {
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 {

View file

@ -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<string> => {
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 {

View file

@ -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.