Skip to content

Commit

Permalink
Add file randomization option. Update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
WaveringAna committed Jan 3, 2024
1 parent 5dc63a7 commit ae2cdad
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 25 deletions.
18 changes: 16 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion app/lib/ffmpeg.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down
64 changes: 42 additions & 22 deletions app/lib/multer.ts
Original file line number Diff line number Diff line change
@@ -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 = (
Expand All @@ -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,
Expand All @@ -33,29 +41,41 @@ export const fileStorage = multer.diskStorage({
console.log(err);
callback(err, null);
}

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 (exists.length != 0) {
const suffix = new Date().getTime() / 1000;
if (filenameSet && existsBool) {
callback(null, request.body.title + fileExtension);
return;
}

if (!filenameSet && existsBool) {
callback(null, filename + fileExtension);
return;
}

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);
}
if (filenameSet && !existsBool) {
callback(null, request.body.title + fileExtension);
return;
}
},
);
Expand Down

0 comments on commit ae2cdad

Please sign in to comment.