From 85a9328b01a8741b851419710ce00ab852218f92 Mon Sep 17 00:00:00 2001 From: MattWong-ca Date: Sun, 8 Dec 2024 15:45:06 -0500 Subject: [PATCH] Add correct type, remove progress tracking --- public/locales/en/files.json | 2 +- src/bundles/files/actions.js | 43 ++++++----------------------------- src/bundles/files/consts.js | 2 ++ src/bundles/files/protocol.ts | 2 ++ 4 files changed, 12 insertions(+), 37 deletions(-) diff --git a/public/locales/en/files.json b/public/locales/en/files.json index 2d849ebe3..20db42bf5 100644 --- a/public/locales/en/files.json +++ b/public/locales/en/files.json @@ -64,7 +64,7 @@ "title": "Bulk import with text file", "description": "Upload a text file with a list of CIDs (names are optional). Example:", "select": "Select file", - "selectedFile": "Selected file:", + "selectedFile": "Selected file", "invalidCids": "*Invalid CID(s) found", "failedToReadFile": "*Failed to read file contents" }, diff --git a/src/bundles/files/actions.js b/src/bundles/files/actions.js index 43f9a7d95..ab7a0d770 100644 --- a/src/bundles/files/actions.js +++ b/src/bundles/files/actions.js @@ -399,34 +399,19 @@ const actions = () => ({ }), /** - * Reads a CSV file containing CIDs and adds each one to IPFS at the given root path. - * @param {FileStream[]} source - The CSV file containing CIDs + * Reads a text file containing CIDs and adds each one to IPFS at the given root path. + * @param {FileStream[]} source - The text file containing CIDs * @param {string} root - Destination directory in IPFS */ - doFilesBulkCidImport: (source, root) => spawn(ACTIONS.ADD_BY_PATH, async function * (ipfs, { store }) { + doFilesBulkCidImport: (source, root) => perform(ACTIONS.BULK_CID_IMPORT, async function (ipfs, { store }) { ensureMFS(store) - // Ensure source is properly passed - if (!source) { - throw new Error('Source is required') + if (!source?.[0]?.content) { + console.error('Invalid file format provided to doFilesBulkCidImport') + return } - // If source is passed as first argument of spawn callback - const actualSource = Array.isArray(source) ? source : arguments[0] - console.log('Actual source:', actualSource) - - if (!Array.isArray(actualSource)) { - throw new Error('Source must be an array') - } - - const fileStream = actualSource[0] - console.log('fileStream:', fileStream) - - if (!fileStream || !fileStream.content) { - throw new Error('Invalid file format') - } - - const file = fileStream + const file = source[0] const content = await new Response(file.content).text() const lines = content.split('\n').map(line => line.trim()).filter(Boolean) @@ -445,33 +430,19 @@ const actions = () => ({ } }) - /** @type {Array<{ path: string, cid: string }>} */ - const entries = [] - let progress = 0 - const totalCids = cidObjects.length - - yield { entries, progress: 0 } - for (const { cid, name } of cidObjects) { try { const src = `/ipfs/${cid}` const dst = realMfsPath(join(root || '/files', name || cid)) await ipfs.files.cp(src, dst) - - entries.push({ path: dst, cid }) - progress = (entries.length / totalCids) * 100 - - yield { entries, progress } } catch (err) { console.error(`Failed to add CID ${cid}:`, err) // Continue with next CID even if one fails } } - yield { entries, progress: 100 } await store.doFilesFetch() - return entries }), /** diff --git a/src/bundles/files/consts.js b/src/bundles/files/consts.js index b5fdac0dc..67383694a 100644 --- a/src/bundles/files/consts.js +++ b/src/bundles/files/consts.js @@ -23,6 +23,8 @@ export const ACTIONS = { SHARE_LINK: ('FILES_SHARE_LINK'), /** @type {'FILES_ADDBYPATH'} */ ADD_BY_PATH: ('FILES_ADDBYPATH'), + /** @type {'FILES_BULK_CID_IMPORT'} */ + BULK_CID_IMPORT: ('FILES_BULK_CID_IMPORT'), /** @type {'FILES_PIN_ADD'} */ PIN_ADD: ('FILES_PIN_ADD'), /** @type {'FILES_PIN_REMOVE'} */ diff --git a/src/bundles/files/protocol.ts b/src/bundles/files/protocol.ts index c6ca742e2..60076a255 100644 --- a/src/bundles/files/protocol.ts +++ b/src/bundles/files/protocol.ts @@ -63,6 +63,7 @@ export type Message = | Move | Write | AddByPath + | BulkCidImport | DownloadLink | Perform<'FILES_SHARE_LINK', Error, string, void> | Perform<'FILES_COPY', Error, void, void> @@ -76,6 +77,7 @@ export type MakeDir = Perform<'FILES_MAKEDIR', Error, void, void> export type WriteProgress = { paths: string[], progress: number } export type Write = Spawn<'FILES_WRITE', WriteProgress, Error, void, void> export type AddByPath = Perform<'FILES_ADDBYPATH', Error, void, void> +export type BulkCidImport = Perform<'FILES_BULK_CID_IMPORT', Error, void, void> export type Move = Perform<'FILES_MOVE', Error, void, void> export type Delete = Perform<'FILES_DELETE', Error, void, void> export type DownloadLink = Perform<'FILES_DOWNLOADLINK', Error, FileDownload, void>