Skip to content

Commit 71950f2

Browse files
committed
fix: handle file uploading properly
1 parent 5c5da9d commit 71950f2

File tree

1 file changed

+19
-22
lines changed

1 file changed

+19
-22
lines changed

src/index.ts

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ export class ShareXServer {
1818
#server = express();
1919
#password: string;
2020
#fsPath: string;
21-
#multer;
2221

2322
constructor({
2423
port = 8080,
@@ -62,26 +61,6 @@ export class ShareXServer {
6261
}
6362
this.#password = password;
6463

65-
this.#multer = multer({
66-
storage: diskStorage({
67-
destination: (_req, _file, cb) => cb(null, this.#fsPath),
68-
filename: async (_req, file, cb) => {
69-
let name = nanoid(this.filenameLength);
70-
const extension =
71-
file.originalname.split(".").length > 1
72-
? "." + file.originalname.split(".").pop()
73-
: "";
74-
75-
// Little safeguard to prevent overwriting files (although extremely unlikely https://zelark.github.io/nano-id-cc/)
76-
if (await this.#checkFileExists(name)) {
77-
name = nanoid(this.filenameLength);
78-
}
79-
80-
cb(null, `${name}${extension}`);
81-
},
82-
}),
83-
});
84-
8564
this.#fsPath = join("./", this.savePath);
8665
this.#ensureSavePath().then(() => this.#startServer());
8766
}
@@ -112,7 +91,25 @@ export class ShareXServer {
11291
// Make sure only authorized users can upload
11392
(req, res, next) => this.#checkAuth(req, res, next),
11493
// Handle the file upload
115-
this.#multer.single("file"),
94+
multer({
95+
storage: diskStorage({
96+
destination: (_req, _file, cb) => cb(null, this.#fsPath),
97+
filename: async (_req, file, cb) => {
98+
let name = nanoid(this.filenameLength);
99+
const extension =
100+
file.originalname.split(".").length > 1
101+
? "." + file.originalname.split(".").pop()
102+
: "";
103+
104+
// Little safeguard to prevent overwriting files (although extremely unlikely https://zelark.github.io/nano-id-cc/)
105+
if (await this.#checkFileExists(name)) {
106+
name = nanoid(this.filenameLength);
107+
}
108+
109+
cb(null, `${name}${extension}`);
110+
},
111+
}),
112+
}).single("file"),
116113
// This returns the URL to the user
117114
(req, res) => this.#uploadFile(req, res)
118115
);

0 commit comments

Comments
 (0)