From 443fea0fb00964259b68f424f5bd7af34b81eeac Mon Sep 17 00:00:00 2001 From: waveringana Date: Wed, 3 Jan 2024 11:39:25 -0500 Subject: [PATCH] fix duplicate --- README.md | 4 ++-- app/lib/multer.ts | 34 +++++++++++++++++++++++++--------- 2 files changed, 27 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 37fbd53..6275470 100644 --- a/README.md +++ b/README.md @@ -72,7 +72,7 @@ This project uses environmental variables to configure functions. ### Using Docker ```bash -docker run -d -p "3000:3000" -e EBPORT=3000 -e EBPASS=changeme -e EBAPI_KEY=changeme ghcr.io/waveringana/embedder:1.10.3 +docker run -d -p "3000:3000" -e EBPORT=3000 -e EBPASS=changeme -e EBAPI_KEY=changeme ghcr.io/waveringana/embedder:1.10.4 ``` ### Docker Compose @@ -90,7 +90,7 @@ services: volumes: - ./db:/var/db - ./uploads:/uploads - image: ghcr.io/waveringana/embedder:1.10.3 + image: ghcr.io/waveringana/embedder:1.10.4 ``` ## 📜 License diff --git a/app/lib/multer.ts b/app/lib/multer.ts index 4c6e738..ddd53b1 100644 --- a/app/lib/multer.ts +++ b/app/lib/multer.ts @@ -8,6 +8,7 @@ export type DestinationCallback = ( error: Error | null, destination: string, ) => void; + export type FileNameCallback = (error: Error | null, filename: string) => void; let randomizeNames = false; @@ -43,40 +44,55 @@ export const fileStorage = multer.diskStorage({ } let filenameSet = true; - let existsBool = true; + let existsBool = false; + let suffix: number; if ( - request.body.title == "" || - request.body.title == null || - request.body.title == undefined + request.body.title != "" || + request.body.title != null || + request.body.title != undefined ) { filenameSet = false; } - - if (exists.length == 0) { - existsBool = false; + + if (exists.length != 0) { + existsBool = true; + suffix = new Date().getTime() / 1000; } + console.log(request.body.title); + if (randomizeNames) { + //Random string of 8 alphanumeric characters //Chance of collision is extremely low, not worth checking for + console.log("Randomizing name"); callback(null, Math.random().toString(36).slice(2, 10) + fileExtension); return; } if (filenameSet && existsBool) { - callback(null, request.body.title + fileExtension); + console.log(`filenameSet is ${filenameSet} and existsBool is ${existsBool}`); + callback(null, request.body.title + "-" + suffix + fileExtension); return; } if (!filenameSet && existsBool) { - callback(null, filename + fileExtension); + console.log(`filenameSet is ${filenameSet} and existsBool is ${existsBool}`); + callback(null, filename + "-" + suffix + fileExtension); return; } if (filenameSet && !existsBool) { + console.log(`filenameSet is ${filenameSet} and existsBool is ${existsBool}`); callback(null, request.body.title + fileExtension); return; } + + if (!filenameSet && !existsBool) { + console.log(`filenameSet is ${filenameSet} and existsBool is ${existsBool}`); + callback(null, filename + fileExtension); + return; + } }, ); },