MCPcopy
hub / github.com/TransformerOptimus/SuperAGI / downloadAllFiles

Function downloadAllFiles

gui/utils/utils.js:158–204  ·  view source on GitHub ↗
(files, run_name)

Source from the content-addressed store, hash-verified

156};
157
158export const downloadAllFiles = (files, run_name) => {
159 const zip = new JSZip();
160 const promises = [];
161 const fileNamesCount = {};
162
163 files.forEach((file, index) => {
164 fileNamesCount[file.name]
165 ? fileNamesCount[file.name]++
166 : (fileNamesCount[file.name] = 1);
167
168 let modifiedFileName = file.name;
169 if (fileNamesCount[file.name] > 1) {
170 const fileExtensionIndex = file.name.lastIndexOf(".");
171 const name = file.name.substring(0, fileExtensionIndex);
172 const extension = file.name.substring(fileExtensionIndex + 1);
173 modifiedFileName = `${name} (${fileNamesCount[file.name] - 1}).${extension}`;
174 }
175
176 const promise = downloadFile(file.id)
177 .then((blob) => {
178 const fileBlob = new Blob([blob], {type: file.type});
179 zip.file(modifiedFileName, fileBlob);
180 })
181 .catch((error) => {
182 console.error("Error downloading file:", error);
183 });
184
185 promises.push(promise);
186 });
187
188 Promise.all(promises)
189 .then(() => {
190 zip.generateAsync({type: "blob"})
191 .then((content) => {
192 const now = new Date();
193 const timestamp = `${now.getFullYear()}-${("0" + (now.getMonth() + 1)).slice(-2)}-${("0" + now.getDate()).slice(-2)}_${now.getHours()}:${now.getMinutes()}:${now.getSeconds()}`.replace(/:/g, '-');
194 const zipFilename = `${run_name}_${timestamp}.zip`;
195 const downloadLink = document.createElement("a");
196 downloadLink.href = URL.createObjectURL(content);
197 downloadLink.download = zipFilename;
198 downloadLink.click();
199 })
200 .catch((error) => {
201 console.error("Error generating zip:", error);
202 });
203 });
204};
205
206export const refreshUrl = () => {
207 if (typeof window === 'undefined') {

Callers 1

downloadRunFilesFunction · 0.90

Calls 2

downloadFileFunction · 0.85
errorMethod · 0.80

Tested by

no test coverage detected