Add file randomization option. Update README.md

This commit is contained in:
waveringana 2024-01-02 20:05:45 -05:00
parent 5dc63a74aa
commit ae2cdada07
3 changed files with 60 additions and 26 deletions

View file

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

View file

@ -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";

View file

@ -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,
@ -34,28 +42,40 @@ export const fileStorage = multer.diskStorage({
callback(err, null);
}
if (exists.length != 0) {
const suffix = new Date().getTime() / 1000;
let filenameSet = true;
let existsBool = true;
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);
filenameSet = false;
}
} else {
if (
request.body.title == "" ||
request.body.title == null ||
request.body.title == undefined
) {
callback(null, filename + fileExtension);
} else {
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;
}
},
);