commit
d906e927ed
|
@ -160,6 +160,7 @@ function download(id, keychain, onprogress, canceller) {
|
||||||
resolve(this.result);
|
resolve(this.result);
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
xhr.addEventListener('progress', function(event) {
|
xhr.addEventListener('progress', function(event) {
|
||||||
if (event.lengthComputable && event.target.status === 200) {
|
if (event.lengthComputable && event.target.status === 200) {
|
||||||
onprogress([event.loaded, event.total]);
|
onprogress([event.loaded, event.total]);
|
||||||
|
@ -168,8 +169,10 @@ function download(id, keychain, onprogress, canceller) {
|
||||||
const auth = await keychain.authHeader();
|
const auth = await keychain.authHeader();
|
||||||
xhr.open('get', `/api/download/${id}`);
|
xhr.open('get', `/api/download/${id}`);
|
||||||
xhr.setRequestHeader('Authorization', auth);
|
xhr.setRequestHeader('Authorization', auth);
|
||||||
|
xhr.setRequestHeader('Connection', 'close');
|
||||||
xhr.responseType = 'blob';
|
xhr.responseType = 'blob';
|
||||||
xhr.send();
|
xhr.send();
|
||||||
|
onprogress([0, 1]);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -14,10 +14,15 @@ module.exports = async function(req, res) {
|
||||||
'WWW-Authenticate': `send-v1 ${req.nonce}`
|
'WWW-Authenticate': `send-v1 ${req.nonce}`
|
||||||
});
|
});
|
||||||
const file_stream = storage.get(id);
|
const file_stream = storage.get(id);
|
||||||
let sentBytes = 0;
|
let cancelled = false;
|
||||||
file_stream.on('data', c => (sentBytes += c.length));
|
|
||||||
file_stream.on('end', async () => {
|
req.on('close', () => {
|
||||||
if (sentBytes < contentLength) {
|
cancelled = true;
|
||||||
|
file_stream.destroy();
|
||||||
|
});
|
||||||
|
|
||||||
|
file_stream.on('close', async () => {
|
||||||
|
if (cancelled) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const dl = meta.dl + 1;
|
const dl = meta.dl + 1;
|
||||||
|
@ -32,6 +37,7 @@ module.exports = async function(req, res) {
|
||||||
log.info('StorageError:', id);
|
log.info('StorageError:', id);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
file_stream.pipe(res);
|
file_stream.pipe(res);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
res.sendStatus(404);
|
res.sendStatus(404);
|
||||||
|
|
|
@ -134,6 +134,27 @@ describe('Upload / Download flow', function() {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('can cancel and not increase download count', async function() {
|
||||||
|
const fs = new FileSender(blob);
|
||||||
|
const file = await fs.upload();
|
||||||
|
const fr = new FileReceiver({
|
||||||
|
secretKey: file.toJSON().secretKey,
|
||||||
|
id: file.id,
|
||||||
|
nonce: file.keychain.nonce,
|
||||||
|
requiresPassword: false
|
||||||
|
});
|
||||||
|
await fr.getMetadata();
|
||||||
|
fr.once('progress', () => fr.cancel());
|
||||||
|
|
||||||
|
try {
|
||||||
|
await fr.download(noSave);
|
||||||
|
assert.fail('not cancelled');
|
||||||
|
} catch (e) {
|
||||||
|
await file.updateDownloadCount();
|
||||||
|
assert.equal(file.dtotal, 0);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
it('can allow multiple downloads', async function() {
|
it('can allow multiple downloads', async function() {
|
||||||
const fs = new FileSender(blob);
|
const fs = new FileSender(blob);
|
||||||
const file = await fs.upload();
|
const file = await fs.upload();
|
||||||
|
|
Loading…
Reference in New Issue