From 4fdea7db6661282749ea41f6de7eb1ce33177b10 Mon Sep 17 00:00:00 2001 From: 4nshuman Date: Sun, 20 Oct 2024 00:06:30 +0530 Subject: [PATCH] offloaded write operations to Upload module --- src/gui/src/helpers.js | 56 ++++++++++++++++++++---------------------- 1 file changed, 26 insertions(+), 30 deletions(-) diff --git a/src/gui/src/helpers.js b/src/gui/src/helpers.js index 5bc34b82..dbbd5a11 100644 --- a/src/gui/src/helpers.js +++ b/src/gui/src/helpers.js @@ -2203,39 +2203,35 @@ window.unzipItem = async function(itemPath) { }, Math.max(0, window.copy_progress_hide_delay - (Date.now() - start_ts))); } else { const rootdir = await puter.fs.mkdir(path.dirname(filePath) + '/' + path.basename(filePath, '.zip'), { dedupeName: true }); - let queuedFileWrites = [] let perItemProgress = window.zippingProgressConfig.WRITING / Object.keys(unzipped).length; + let queuedFileWrites = [] Object.keys(unzipped).forEach(fileItem => { - let fileWriteProcess = new Promise(async (resolve, reject) => { - try { - let dirLevel, fileName; - let fileData = new Blob([new Uint8Array(unzipped[fileItem], unzipped[fileItem].byteOffset, unzipped[fileItem].length)]); - if (fileItem.includes("/")) { - [dirLevel, fileName] = [fileItem.slice(0, fileItem.lastIndexOf("/")), fileItem.slice(fileItem.lastIndexOf("/") + 1)] - await puter.fs.mkdir(rootdir.path + '/' + dirLevel, { createMissingParents: true }); - } else { - fileName = fileItem; - } - progwin?.set_status(i18n('writing', fileItem)); - fileName != "" && await puter.fs.write(rootdir.path + '/' + fileItem, fileData); - currentProgress += perItemProgress; - progwin?.set_progress(currentProgress.toPrecision(2)); - resolve(); - } catch (e) { - UIAlert(e.message); - reject(); - } - }) - queuedFileWrites.push(fileWriteProcess); - }); - Promise.all(queuedFileWrites).then(() => { - progwin?.set_progress(window.zippingProgressConfig.TOTAL.toPrecision(2)); - // close progress window - clearTimeout(progwin_timeout); - setTimeout(() => { - progwin?.close(); - }, Math.max(0, window.unzip_progress_hide_delay - (Date.now() - start_ts))); + try { + let fileData = new Blob([new Uint8Array(unzipped[fileItem], unzipped[fileItem].byteOffset, unzipped[fileItem].length)]); + progwin?.set_status(i18n('writing', fileItem)); + queuedFileWrites.push(new File([fileData], fileItem)) + currentProgress += perItemProgress; + progwin?.set_progress(currentProgress.toPrecision(2)); + } catch (e) { + UIAlert(e.message); + } }); + queuedFileWrites.length && puter.fs.upload( + // what to upload + queuedFileWrites, + // where to upload + rootdir.path + '/', + // options + { + createFileParent: true + } + ); + progwin?.set_progress(window.zippingProgressConfig.TOTAL.toPrecision(2)); + // close progress window + clearTimeout(progwin_timeout); + setTimeout(() => { + progwin?.close(); + }, Math.max(0, window.unzip_progress_hide_delay - (Date.now() - start_ts))); } }); }