Skip to content

Commit 0014d81

Browse files
committed
Lock the bucket when the files upload completes
1 parent b9853c9 commit 0014d81

File tree

3 files changed

+38
-3
lines changed

3 files changed

+38
-3
lines changed

app/src/Upload/store/upload.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,10 @@ export default {
231231
progress: { percentage: 100, humanFileSize: file.humanSize, bytesUploaded: file._File.size }
232232
}
233233
});
234-
if (state.files.every(f => f.uploaded)) commit('STATE', 'uploaded', { root: true });
234+
if (state.files.every(f => f.uploaded)) {
235+
fetch(state.uploadURI + '/' + state.sid + '?lock=yes', { method: 'PATCH' });
236+
commit('STATE', 'uploaded', { root: true });
237+
}
235238
}
236239
}).start();
237240
}

lib/db.js

+13
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,19 @@ module.exports = class DB {
132132
await this.store.update(`${ sid }++${ key }`, file);
133133
}
134134

135+
async lock(sid) {
136+
const files = this.get(sid);
137+
if(!files) return;
138+
await Promise.all(files.map(async file => {
139+
await this.updateMetadata(sid, file.key, { buckedLocked: true });
140+
}));
141+
}
142+
143+
isLocked(sid) {
144+
const files = this.get(sid);
145+
if(!files) return false;
146+
return files.some(file => file.metadata.buckedLocked);
147+
}
135148

136149
get(sid) {
137150
return this.db[sid];

lib/endpoints.js

+21-2
Original file line numberDiff line numberDiff line change
@@ -332,11 +332,25 @@ app.use(`${ config.uploadAppPath }files`,
332332

333333
if (req.method === 'GET') return res.status(405).end();
334334

335-
// Restrict upload to a file which upload completed already
335+
// Lock bucket by PATCH /files/:sid?lock=yes
336+
const fid = req.path.substring(1);
337+
if(!fid.includes('++') && req.method === 'PATCH' && req.query.lock) {
338+
await db.lock(fid);
339+
return res.status(204).end('Bucket locked');
340+
}
341+
336342
if(['POST', 'PATCH'].includes(req.method)) {
343+
// Restrict upload to the bucket if it is locked
344+
if(!fid.includes('++') && db.isLocked(fid)) {
345+
return res.status(400).end('Bucket locked');
346+
}
337347
try {
338-
const fid = req.url.substring(1);
339348
const info = await store.info(fid);
349+
// Restrict upload to the bucket if it is locked
350+
if(info.metadata.locked) {
351+
return res.status(400).end('Bucket locked');
352+
}
353+
// Restrict upload to a file which upload completed already
340354
if(!info.isPartial) {
341355
return res.status(400).end('Upload already completed');
342356
}
@@ -363,6 +377,11 @@ app.use(`${ config.uploadAppPath }files`,
363377
const uploadLength = req.get('Upload-Length');
364378
assert(uploadLength, 'missing Upload-Length header');
365379

380+
// Restrict creating new files for locked buckets
381+
if(db.isLocked(meta.sid)) {
382+
return res.status(400).end('Bucket locked');
383+
}
384+
366385
meta.uploadLength = uploadLength;
367386
meta.key = uuid();
368387
meta.createdAt = Date.now().toString();

0 commit comments

Comments
 (0)