uses fetch.body
This commit is contained in:
parent
5f79a9fb6d
commit
38ef52d3ba
3 changed files with 192 additions and 24 deletions
63
app/api.js
63
app/api.js
|
@ -197,6 +197,69 @@ export function uploadWs(encrypted, info, metadata, verifierB64, onprogress) {
|
|||
};
|
||||
}
|
||||
|
||||
////////////////////////
|
||||
|
||||
async function downloadS(id, keychain, onprogress, signal) {
|
||||
const auth = await keychain.authHeader();
|
||||
try {
|
||||
const response = await fetch(`/api/download/${id}`, {
|
||||
signal: signal ,
|
||||
method: 'GET',
|
||||
headers: {'Authorization': auth}
|
||||
});
|
||||
|
||||
if (response.status !== 200) {
|
||||
throw new Error(response.status);
|
||||
}
|
||||
|
||||
const authHeader = response.headers.get('WWW-Authenticate');
|
||||
if (authHeader) {
|
||||
keychain.nonce = parseNonce(authHeader);
|
||||
}
|
||||
|
||||
const fileSize = response.headers.get('Content-Length');
|
||||
onprogress([0, fileSize]);
|
||||
|
||||
console.log(response.body);
|
||||
if (response.body) {
|
||||
return response.body;
|
||||
}
|
||||
return response.blob();
|
||||
|
||||
} catch (err) {
|
||||
if (err.name === 'AbortError') {
|
||||
throw new Error('0');
|
||||
} else {
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async function tryDownloadStream(id, keychain, onprogress, signal, tries = 1) {
|
||||
try {
|
||||
const result = await downloadS(id, keychain, onprogress, signal);
|
||||
return result;
|
||||
} catch (e) {
|
||||
if (e.message === '401' && --tries > 0) {
|
||||
return tryDownloadStream(id, keychain, onprogress, signal, tries);
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
export function downloadStream(id, keychain, onprogress) {
|
||||
const controller = new AbortController();
|
||||
function cancel() {
|
||||
controller.abort();
|
||||
}
|
||||
return {
|
||||
cancel,
|
||||
result: tryDownloadStream(id, keychain, onprogress, controller.signal, 2)
|
||||
};
|
||||
}
|
||||
|
||||
//////////////////
|
||||
|
||||
function download(id, keychain, onprogress, canceller) {
|
||||
const xhr = new XMLHttpRequest();
|
||||
canceller.oncancel = function() {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue