refactor: Update download logic to use parallel fetching for improved performance
This commit is contained in:
@@ -68,33 +68,20 @@ export default function DownloadPage() {
|
||||
const metadata = await metaRes.json();
|
||||
setFilename(metadata.filename);
|
||||
|
||||
// 2. Download all parts with a concurrency limit
|
||||
// 2. Download all parts in parallel
|
||||
setDownloadState('downloading');
|
||||
const CONCURRENCY_LIMIT = 5;
|
||||
const queue = [...metadata.parts];
|
||||
const encryptedParts = new Array(metadata.num_parts);
|
||||
let downloadedCount = 0;
|
||||
|
||||
const downloadPart = async (part: any) => {
|
||||
const downloadPromises = metadata.parts.map(async (part: any) => {
|
||||
const response = await fetchWithRetry(`/api/download-part/${file_id}/${part.part_index}`);
|
||||
const buffer = await response.arrayBuffer();
|
||||
encryptedParts[part.part_index] = { index: part.part_index, data: buffer };
|
||||
downloadedCount++;
|
||||
setProgress(Math.round((downloadedCount / metadata.num_parts) * 50));
|
||||
};
|
||||
});
|
||||
|
||||
const active = [];
|
||||
while (queue.length > 0 || active.length > 0) {
|
||||
while (active.length < CONCURRENCY_LIMIT && queue.length > 0) {
|
||||
const task = queue.shift()!;
|
||||
const promise = downloadPart(task).finally(() => {
|
||||
const index = active.indexOf(promise);
|
||||
if (index > -1) active.splice(index, 1);
|
||||
});
|
||||
active.push(promise);
|
||||
}
|
||||
await Promise.race(active);
|
||||
}
|
||||
await Promise.all(downloadPromises);
|
||||
|
||||
// 3. Import key and decrypt parts
|
||||
setDownloadState('decrypting');
|
||||
|
||||
Reference in New Issue
Block a user