From ae2cdada07dba6b306cd017f7874d72a0f732854 Mon Sep 17 00:00:00 2001 From: waveringana Date: Tue, 2 Jan 2024 20:05:45 -0500 Subject: [PATCH] Add file randomization option. Update README.md --- README.md | 18 +++++++++++-- app/lib/ffmpeg.ts | 2 +- app/lib/multer.ts | 66 ++++++++++++++++++++++++++++++----------------- 3 files changed, 60 insertions(+), 26 deletions(-) diff --git a/README.md b/README.md index 5ed35e1..37fbd53 100644 --- a/README.md +++ b/README.md @@ -55,10 +55,24 @@ Enabled at `/upload`. Requires authentication with key. `expire` key specifies d } ``` +### Configuration + +This project uses environmental variables to configure functions. + +`EBPASS` configures the password for the admin account. + +`EBAPI_KEY` configures the key for API uploading support typically used for ShareX. + +`EBPORT` configures the port the server runs on. + +`EB_FFMPEG_PATH` and `EB_FFPROBE_PATH` configures the path to the ffmpeg and ffprobe binaries respectively. If not set, it uses installed binaries set in the PATH. If none are detected, it will default to preinstalled binaries from the [node-ffmpeg-installer](https://www.npmjs.com/package/@ffmpeg-installer/ffmpeg) package. + +`EB_RANDOMIZE_NAMES` configures whether or not to randomize file names. If set to `true`, file names will be randomized. If not set or set to false, it will be `false`. + ### 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.2 +docker run -d -p "3000:3000" -e EBPORT=3000 -e EBPASS=changeme -e EBAPI_KEY=changeme ghcr.io/waveringana/embedder:1.10.3 ``` ### Docker Compose @@ -76,7 +90,7 @@ services: volumes: - ./db:/var/db - ./uploads:/uploads - image: ghcr.io/waveringana/embedder:1.10.2 + image: ghcr.io/waveringana/embedder:1.10.3 ``` ## 📜 License diff --git a/app/lib/ffmpeg.ts b/app/lib/ffmpeg.ts index 813d3a3..f9c215f 100644 --- a/app/lib/ffmpeg.ts +++ b/app/lib/ffmpeg.ts @@ -1,4 +1,4 @@ -import { extension, videoExtensions, imageExtensions } from "./lib"; +import { videoExtensions, imageExtensions } from "./lib"; import ffmpeg, { FfprobeData, ffprobe } from "fluent-ffmpeg"; import ffmpegInstaller from "@ffmpeg-installer/ffmpeg"; diff --git a/app/lib/multer.ts b/app/lib/multer.ts index a0cbe21..4c6e738 100644 --- a/app/lib/multer.ts +++ b/app/lib/multer.ts @@ -1,7 +1,7 @@ import { Request } from "express"; import multer, { FileFilterCallback } from "multer"; -import { db, MediaRow } from "./db"; +import { db } from "./db"; import { extension } from "./lib"; export type DestinationCallback = ( @@ -10,6 +10,14 @@ export type DestinationCallback = ( ) => void; export type FileNameCallback = (error: Error | null, filename: string) => void; +let randomizeNames = false; + +if (process.env["EB_RANDOMIZE_NAMES"] === "true") { + randomizeNames = true; +} + +console.log(`Randomize names is set ${randomizeNames}`); + export const fileStorage = multer.diskStorage({ destination: ( request: Request, @@ -33,29 +41,41 @@ export const fileStorage = multer.diskStorage({ console.log(err); callback(err, null); } - - if (exists.length != 0) { - const suffix = new Date().getTime() / 1000; - if ( - request.body.title == "" || - request.body.title == null || - request.body.title == undefined - ) { - callback(null, filename + "-" + suffix + fileExtension); - } else { - callback(null, request.body.title + "-" + suffix + fileExtension); - } - } else { - if ( - request.body.title == "" || - request.body.title == null || - request.body.title == undefined - ) { - callback(null, filename + fileExtension); - } else { - callback(null, request.body.title + fileExtension); - } + let filenameSet = true; + let existsBool = true; + + if ( + request.body.title == "" || + request.body.title == null || + request.body.title == undefined + ) { + filenameSet = false; + } + + if (exists.length == 0) { + existsBool = false; + } + + if (randomizeNames) { + //Chance of collision is extremely low, not worth checking for + callback(null, Math.random().toString(36).slice(2, 10) + fileExtension); + return; + } + + if (filenameSet && existsBool) { + callback(null, request.body.title + fileExtension); + return; + } + + if (!filenameSet && existsBool) { + callback(null, filename + fileExtension); + return; + } + + if (filenameSet && !existsBool) { + callback(null, request.body.title + fileExtension); + return; } }, );