Skip to content

Commit ae2cdad

Browse files
committed
Add file randomization option. Update README.md
1 parent 5dc63a7 commit ae2cdad

File tree

3 files changed

+59
-25
lines changed

3 files changed

+59
-25
lines changed

README.md

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,24 @@ Enabled at `/upload`. Requires authentication with key. `expire` key specifies d
5555
}
5656
```
5757

58+
### Configuration
59+
60+
This project uses environmental variables to configure functions.
61+
62+
`EBPASS` configures the password for the admin account.
63+
64+
`EBAPI_KEY` configures the key for API uploading support typically used for ShareX.
65+
66+
`EBPORT` configures the port the server runs on.
67+
68+
`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.
69+
70+
`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`.
71+
5872
### Using Docker
5973

6074
```bash
61-
docker run -d -p "3000:3000" -e EBPORT=3000 -e EBPASS=changeme -e EBAPI_KEY=changeme ghcr.io/waveringana/embedder:1.10.2
75+
docker run -d -p "3000:3000" -e EBPORT=3000 -e EBPASS=changeme -e EBAPI_KEY=changeme ghcr.io/waveringana/embedder:1.10.3
6276
```
6377

6478
### Docker Compose
@@ -76,7 +90,7 @@ services:
7690
volumes:
7791
- ./db:/var/db
7892
- ./uploads:/uploads
79-
image: ghcr.io/waveringana/embedder:1.10.2
93+
image: ghcr.io/waveringana/embedder:1.10.3
8094
```
8195
8296
## 📜 License

app/lib/ffmpeg.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { extension, videoExtensions, imageExtensions } from "./lib";
1+
import { videoExtensions, imageExtensions } from "./lib";
22

33
import ffmpeg, { FfprobeData, ffprobe } from "fluent-ffmpeg";
44
import ffmpegInstaller from "@ffmpeg-installer/ffmpeg";

app/lib/multer.ts

Lines changed: 42 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Request } from "express";
22
import multer, { FileFilterCallback } from "multer";
33

4-
import { db, MediaRow } from "./db";
4+
import { db } from "./db";
55
import { extension } from "./lib";
66

77
export type DestinationCallback = (
@@ -10,6 +10,14 @@ export type DestinationCallback = (
1010
) => void;
1111
export type FileNameCallback = (error: Error | null, filename: string) => void;
1212

13+
let randomizeNames = false;
14+
15+
if (process.env["EB_RANDOMIZE_NAMES"] === "true") {
16+
randomizeNames = true;
17+
}
18+
19+
console.log(`Randomize names is set ${randomizeNames}`);
20+
1321
export const fileStorage = multer.diskStorage({
1422
destination: (
1523
request: Request,
@@ -33,29 +41,41 @@ export const fileStorage = multer.diskStorage({
3341
console.log(err);
3442
callback(err, null);
3543
}
44+
45+
let filenameSet = true;
46+
let existsBool = true;
47+
48+
if (
49+
request.body.title == "" ||
50+
request.body.title == null ||
51+
request.body.title == undefined
52+
) {
53+
filenameSet = false;
54+
}
55+
56+
if (exists.length == 0) {
57+
existsBool = false;
58+
}
59+
60+
if (randomizeNames) {
61+
//Chance of collision is extremely low, not worth checking for
62+
callback(null, Math.random().toString(36).slice(2, 10) + fileExtension);
63+
return;
64+
}
3665

37-
if (exists.length != 0) {
38-
const suffix = new Date().getTime() / 1000;
66+
if (filenameSet && existsBool) {
67+
callback(null, request.body.title + fileExtension);
68+
return;
69+
}
70+
71+
if (!filenameSet && existsBool) {
72+
callback(null, filename + fileExtension);
73+
return;
74+
}
3975

40-
if (
41-
request.body.title == "" ||
42-
request.body.title == null ||
43-
request.body.title == undefined
44-
) {
45-
callback(null, filename + "-" + suffix + fileExtension);
46-
} else {
47-
callback(null, request.body.title + "-" + suffix + fileExtension);
48-
}
49-
} else {
50-
if (
51-
request.body.title == "" ||
52-
request.body.title == null ||
53-
request.body.title == undefined
54-
) {
55-
callback(null, filename + fileExtension);
56-
} else {
57-
callback(null, request.body.title + fileExtension);
58-
}
76+
if (filenameSet && !existsBool) {
77+
callback(null, request.body.title + fileExtension);
78+
return;
5979
}
6080
},
6181
);

0 commit comments

Comments
 (0)