Skip to content

Commit

Permalink
Add correct type, remove progress tracking
Browse files Browse the repository at this point in the history
  • Loading branch information
MattWong-ca committed Dec 8, 2024
1 parent 274bc37 commit 85a9328
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 37 deletions.
2 changes: 1 addition & 1 deletion public/locales/en/files.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
43 changes: 7 additions & 36 deletions src/bundles/files/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
}),

/**
Expand Down
2 changes: 2 additions & 0 deletions src/bundles/files/consts.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'} */
Expand Down
2 changes: 2 additions & 0 deletions src/bundles/files/protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand All @@ -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>
Expand Down

0 comments on commit 85a9328

Please sign in to comment.