Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: simplify file adding #769

Merged
merged 3 commits into from
Aug 29, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 19 additions & 24 deletions src/bundles/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,38 +228,33 @@ export default (opts = {}) => {
doFilesWrite: make(actions.WRITE, async (ipfs, root, rawFiles, id, { dispatch }) => {
const { streams, totalSize } = await filesToStreams(rawFiles)

const updateProgress = (progress) => {
dispatch({ type: 'FILES_WRITE_UPDATED', payload: { id: id, progress } })
const updateProgress = (sent) => {
dispatch({ type: 'FILES_WRITE_UPDATED', payload: { id: id, progress: sent / totalSize * 100 } })
}

updateProgress(0)

let sent = 0
const res = await ipfs.files.add(streams, {
pin: false,
wrapWithDirectory: true,
progress: updateProgress
})

for (const file of streams) {
const dir = join(root, dirname(file.name))
await ipfs.files.mkdir(dir, { parents: true })
let alreadySent = 0

const res = await ipfs.add(file.content, {
pin: false,
// eslint-disable-next-line
progress: (bytes) => {
sent = sent + bytes - alreadySent
alreadySent = bytes
updateProgress(sent / totalSize * 100)
}
})

const src = `/ipfs/${res[0].hash}`
const dst = join(root, file.name)
await ipfs.files.cp([src, dst])
if (res.length !== streams.length + 1) {
// See https://github.com/ipfs/js-ipfs-api/issues/797
throw new Error('Unable to finish upload: IPFS API returned a partial response. Try adding a smaller tree.')
}

sent = sent - alreadySent + file.size
updateProgress(sent / totalSize * 100)
for (const { path, hash } of res) {
// Only go for direct children
if (path.indexOf('/') === -1 && path !== '') {
const src = `/ipfs/${hash}`
const dst = join(root, path)
await ipfs.files.cp([src, dst])
}
}

updateProgress(100)
updateProgress(totalSize)
}),

doFilesDelete: make(actions.DELETE, (ipfs, files) => {
Expand Down
8 changes: 7 additions & 1 deletion src/files/files-list/FilesList.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,13 @@ class FileList extends React.Component {

get selectedMenu () {
const unselectAll = () => this.toggleAll(false)
const size = this.selectedFiles.reduce((a, b) => a + (b.size || b.cumulativeSize), 0)
const size = this.selectedFiles.reduce((a, b) => {
if (b.cumulativeSize) {
return a + b.cumulativeSize
}

return a + b.size
}, 0)
const show = this.state.selected.length !== 0

return (
Expand Down
6 changes: 3 additions & 3 deletions src/lib/dnd-backend.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ async function scanFiles (item, root = '') {
const file = await getFileFromEntry(item)

return [{
name: join(root, file.webkitRelativePath || file.name),
path: join(root, file.webkitRelativePath || file.name),
content: fileReader(file),
size: file.size
}]
Expand All @@ -51,13 +51,13 @@ async function scan (files) {
if (file.getAsEntry || file.webkitGetAsEntry) {
entries.push({
entry: getAsEntry(file),
name: file.name
path: file.name
})
} else {
totalSize += file.size

streams.push({
name: file.webkitRelativePath || file.name,
path: file.webkitRelativePath || file.name,
content: fileReader(file),
size: file.size
})
Expand Down
2 changes: 1 addition & 1 deletion src/lib/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export async function filesToStreams (files) {
}

streams.push({
name: file.webkitRelativePath || file.name,
path: file.webkitRelativePath || file.name,
content: stream,
size: file.size
})
Expand Down