@@ -29,6 +29,7 @@ const downloadPage = pug.compileFile(path.join(__dirname, '../public/pug/downloa
29
29
const store = new Store ( config . uploadDir ) ;
30
30
const Db = require ( './db' ) ;
31
31
const { createGzip } = require ( "zlib" ) ;
32
+ const httpErrors = require ( "http-errors" ) ;
32
33
const db = new Db ( config . uploadDir , store ) ;
33
34
db . init ( ) ;
34
35
const app = express ( ) ;
@@ -315,7 +316,7 @@ app.get(`${ config.baseUrl }files/:fid`, async (req, res, next) => {
315
316
316
317
// Upload file
317
318
app . use ( `${ config . uploadAppPath } files` ,
318
- function ( req , res , next ) {
319
+ async function ( req , res , next ) {
319
320
// Upload password protection
320
321
if ( config . uploadPass ) {
321
322
const bfTimeout = 500 ;
@@ -331,6 +332,22 @@ app.use(`${ config.uploadAppPath }files`,
331
332
332
333
if ( req . method === 'GET' ) return res . status ( 405 ) . end ( ) ;
333
334
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
+
334
351
if ( req . method === 'POST' ) {
335
352
// validate meta-data
336
353
// !! tusMeta.encode supports only strings !!
@@ -387,7 +404,6 @@ app.use(`${ config.uploadAppPath }files`,
387
404
afterComplete : ( req , upload , fid ) => {
388
405
db . add ( upload . metadata . sid , upload . metadata . key , upload ) ;
389
406
debug ( `Completed upload ${ fid } , size=${ upload . size } name=${ upload . metadata . name } ` ) ;
390
-
391
407
eventBus . emit ( 'fileUploaded' , upload ) ;
392
408
} ,
393
409
} )
0 commit comments