Skip to content

Commit b9853c9

Browse files
committed
Restrict upload to a file which upload completed already
1 parent 707b72f commit b9853c9

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

lib/endpoints.js

+18-2
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ const downloadPage = pug.compileFile(path.join(__dirname, '../public/pug/downloa
2929
const store = new Store(config.uploadDir);
3030
const Db = require('./db');
3131
const { createGzip } = require("zlib");
32+
const httpErrors = require("http-errors");
3233
const db = new Db(config.uploadDir, store);
3334
db.init();
3435
const app = express();
@@ -315,7 +316,7 @@ app.get(`${ config.baseUrl }files/:fid`, async (req, res, next) => {
315316

316317
// Upload file
317318
app.use(`${ config.uploadAppPath }files`,
318-
function(req, res, next) {
319+
async function(req, res, next) {
319320
// Upload password protection
320321
if (config.uploadPass) {
321322
const bfTimeout = 500;
@@ -331,6 +332,22 @@ app.use(`${ config.uploadAppPath }files`,
331332

332333
if (req.method === 'GET') return res.status(405).end();
333334

335+
// Restrict upload to a file which upload completed already
336+
if(['POST', 'PATCH'].includes(req.method)) {
337+
try {
338+
const fid = req.url.substring(1);
339+
const info = await store.info(fid);
340+
if(!info.isPartial) {
341+
return res.status(400).end('Upload already completed');
342+
}
343+
} catch(e) {
344+
if(! e instanceof httpErrors.NotFound) {
345+
console.error(e);
346+
return;
347+
}
348+
}
349+
}
350+
334351
if (req.method === 'POST') {
335352
// validate meta-data
336353
// !! tusMeta.encode supports only strings !!
@@ -387,7 +404,6 @@ app.use(`${ config.uploadAppPath }files`,
387404
afterComplete: (req, upload, fid) => {
388405
db.add(upload.metadata.sid, upload.metadata.key, upload);
389406
debug(`Completed upload ${ fid }, size=${ upload.size } name=${ upload.metadata.name }`);
390-
391407
eventBus.emit('fileUploaded', upload);
392408
},
393409
})

0 commit comments

Comments
 (0)