Skip to content
Snippets Groups Projects
Unverified Commit f8809df5 authored by Joel Jeremy Marquez's avatar Joel Jeremy Marquez Committed by GitHub
Browse files

Parallel cloud file download (#2700)

* Fetch cloud file and file info in parallel to speed downloads up

* Release notes

* Rename release notes
parent 04bc0c3c
No related branches found
No related tags found
No related merge requests found
......@@ -377,48 +377,51 @@ export async function listRemoteFiles(): Promise<RemoteFile[] | null> {
export async function download(fileId) {
const userToken = await asyncStorage.getItem('user-token');
let buffer;
try {
buffer = await fetch(getServer().SYNC_SERVER + '/download-user-file', {
headers: {
'X-ACTUAL-TOKEN': userToken,
'X-ACTUAL-FILE-ID': fileId,
},
const syncServer = getServer().SYNC_SERVER;
const userFileFetch = fetch(`${syncServer}/download-user-file`, {
headers: {
'X-ACTUAL-TOKEN': userToken,
'X-ACTUAL-FILE-ID': fileId,
},
})
.then(checkHTTPStatus)
.then(res => {
if (res.arrayBuffer) {
return res.arrayBuffer().then(ab => Buffer.from(ab));
}
return res.buffer();
})
.then(checkHTTPStatus)
.then(res => {
if (res.arrayBuffer) {
return res.arrayBuffer().then(ab => Buffer.from(ab));
}
return res.buffer();
});
} catch (err) {
console.log('Download failure', err);
throw FileDownloadError('download-failure');
}
let res;
try {
res = await fetchJSON(getServer().SYNC_SERVER + '/get-user-file-info', {
headers: {
'X-ACTUAL-TOKEN': userToken,
'X-ACTUAL-FILE-ID': fileId,
},
.catch(err => {
console.log('Download failure', err);
throw FileDownloadError('download-failure');
});
} catch (err) {
const userFileInfoFetch = fetchJSON(`${syncServer}/get-user-file-info`, {
headers: {
'X-ACTUAL-TOKEN': userToken,
'X-ACTUAL-FILE-ID': fileId,
},
}).catch(err => {
console.log('Error fetching file info', err);
throw FileDownloadError('internal', { fileId });
}
});
if (res.status !== 'ok') {
const [userFileInfoRes, userFileRes] = await Promise.all([
userFileInfoFetch,
userFileFetch,
]);
if (userFileInfoRes.status !== 'ok') {
console.log(
'Could not download file from the server. Are you sure you have the right file ID?',
res,
userFileInfoRes,
);
throw FileDownloadError('internal', { fileId });
}
const fileData = res.data;
const fileData = userFileInfoRes.data;
let buffer = userFileRes;
// The download process checks if the server gave us decrypt
// information. It is assumed that this key has already been loaded
......
---
category: Enhancements
authors: [joel-jeremy]
---
Fetch cloud file and file info in parallel to speed downloads up
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment