Skip to content

Commit 743ea99

Browse files
authoredOct 3, 2022
Emit upload complete event when using creation-with-upload extension (#301)
1 parent 9eeb26c commit 743ea99

File tree

3 files changed

+41
-1
lines changed

3 files changed

+41
-1
lines changed
 

‎lib/handlers/PatchHandler.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ class PatchHandler extends BaseHandler {
6363
}
6464

6565
const new_offset = await this.store.write(req, file_id, offset);
66-
if (parseInt(new_offset, 10) === parseInt(upload_length, 10)) {
66+
if (new_offset === parseInt(upload_length, 10)) {
6767
this.emit(EVENTS.EVENT_UPLOAD_COMPLETE, { file: new File(file_id, file.upload_length, file.upload_defer_length, file.upload_metadata) });
6868
}
6969

‎lib/handlers/PostHandler.js

+4
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ class PostHandler extends BaseHandler {
6565
if (!RequestValidator.isInvalidHeader('content-type', req.headers['content-type'])) {
6666
const new_offset = await this.store.write(req, file.id, 0);
6767
optional_headers['Upload-Offset'] = new_offset;
68+
69+
if (new_offset === parseInt(upload_length, 10)) {
70+
this.emit(EVENTS.EVENT_UPLOAD_COMPLETE, { file: new File(file_id, file.upload_length, file.upload_defer_length, file.upload_metadata) });
71+
}
6872
}
6973

7074
return this.write(res, 201, { Location: url, ...optional_headers });

‎test/Test-PostHandler.js

+36
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,42 @@ describe('PostHandler', () => {
144144
req.headers = { 'upload-length': 1000, host: 'localhost:3000' };
145145
handler.send(req, res);
146146
})
147+
148+
it(`must fire the ${EVENTS.EVENT_UPLOAD_COMPLETE} event when upload is complete with single request`, (done) => {
149+
const fake_store = sinon.createStubInstance(DataStore);
150+
151+
const upload_length = 1000;
152+
153+
fake_store.create.resolvesArg(0);
154+
fake_store.write.resolves(upload_length);
155+
156+
const handler = new PostHandler(fake_store, { path: '/test/output' });
157+
handler.on(EVENTS.EVENT_UPLOAD_COMPLETE, (obj) => {
158+
done();
159+
});
160+
161+
req.headers = { 'upload-length': `${upload_length}`, host: 'localhost:3000', 'content-type': 'application/offset+octet-stream' };
162+
handler.send(req, res);
163+
})
164+
165+
it(`must not fire the ${EVENTS.EVENT_UPLOAD_COMPLETE} event when upload-length is defered`, (done) => {
166+
const fake_store = sinon.createStubInstance(DataStore);
167+
168+
const upload_length = 1000;
169+
170+
fake_store.create.resolvesArg(0);
171+
fake_store.write.resolves(upload_length);
172+
173+
const handler = new PostHandler(fake_store, { path: '/test/output' });
174+
handler.on(EVENTS.EVENT_UPLOAD_COMPLETE, (obj) => {
175+
done(new Error());
176+
});
177+
178+
req.headers = { 'upload-defer-length': '1', host: 'localhost:3000', 'content-type': 'application/offset+octet-stream' };
179+
handler.send(req, res)
180+
.then(() => done())
181+
.catch(done);
182+
})
147183
});
148184
});
149185
});

0 commit comments

Comments
 (0)
Please sign in to comment.