Skip to content

Commit

Permalink
Get it working with txt file
Browse files Browse the repository at this point in the history
  • Loading branch information
MattWong-ca committed Dec 1, 2024
1 parent 31a6cd0 commit 35e392d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
27 changes: 20 additions & 7 deletions src/bundles/files/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -408,27 +408,40 @@ const actions = () => ({
ensureMFS(store)

if (source.length !== 1) {
throw new Error('Please provide exactly one CSV file')
throw new Error('Please provide exactly one text file')
}

// Read the CSV file content
// Read the text file content
const file = source[0]
const content = await new Response(file.content).text()

// Split content into CIDs (assuming one CID per line, comma-separated)
const cids = content.split(/[\n,]/).map(cid => cid.trim()).filter(Boolean)
const lines = content.split('\n').map(line => line.trim()).filter(Boolean)
// NOTE: need to add finally fetch later
const cidObjects = lines.map((line) => {
let actualCid = line
let name = line
const cidParts = line.split(' ')
if (cidParts.length > 1) {
actualCid = cidParts[0]
name = cidParts.slice(1).join(' ')
}
return {
name,
cid: actualCid
}
})

/** @type {Array<{ path: string, cid: string }>} */
const entries = []
let progress = 0
const totalCids = cids.length
const totalCids = cidObjects.length

yield { entries, progress: 0 }

for (const cid of cids) {
for (const { cid, name } of cidObjects) {
try {
const src = `/ipfs/${cid}`
const dst = realMfsPath(join(root || '/files', cid))
const dst = realMfsPath(join(root || '/files', name || cid))

await ipfs.files.cp(src, dst)

Expand Down
2 changes: 1 addition & 1 deletion src/files/file-input/FileInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ class FileInput extends React.Component {
type='file'
className='dn'
multiple
accept='.csv'
accept='.txt'
ref={el => { this.bulkCidInput = el }}
onChange={this.onBulkCidInputChange(this.bulkCidInput)} />
</div>
Expand Down

0 comments on commit 35e392d

Please sign in to comment.