Skip to content

fix: upload directory with subdirectories [WIP] #624

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

Merged
merged 2 commits into from
Mar 24, 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
2 changes: 2 additions & 0 deletions src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import KeyValueStore from './utils/key-value-store'

const debug = dbgger('desktop')

process.setFdLimit(2048)

function logo (color) {
const p = path.resolve(path.join(__dirname, 'img'))

Expand Down
55 changes: 20 additions & 35 deletions src/controls/utils/upload-files.js
Original file line number Diff line number Diff line change
@@ -1,56 +1,41 @@
import path from 'path'
import fs from 'fs'

function join (...parts) {
const replace = new RegExp('/{1,}', 'g')
return parts.join('/').replace(replace, '/')
}

function clean (files, root) {
const res = []

files.forEach((file) => {
const stat = fs.lstatSync(file)
const dst = join(root, path.basename(file))

if (stat.isDirectory()) {
const files = clean(fs.readdirSync(file).map(f => path.join(file, f)), dst)
res.push({dir: true, dst: dst}, ...files)
} else {
res.push({
dst: dst,
src: file
})
}
})

return res
let adding = 0

async function add (files, root, ipfs) {
for (const file of files) {
const res = await ipfs().add([file], {recursive: true, wrap: true})
const f = res[res.length - 1]
const src = `/ipfs/${f.hash}`
const dst = join(root, path.basename(f.path))

await ipfs().files.cp([src, dst])
}
}

export default function uploadFiles (opts) {
let {ipfs, debug, send} = opts
let adding = 0

const sendAdding = () => { send('adding', adding > 0) }
const inc = () => { adding++; sendAdding() }
const dec = () => { adding--; sendAdding() }

const anyway = () => {
dec()
send('files-updated')
}

return (event, files, root = '/') => {
debug('Uploading files', {files})
files = clean(files, root)

inc()
Promise.all(files.map(file => {
if (file.dir) {
return ipfs().files.mkdir(file.dst)
}

return ipfs().files.write(file.dst, file.src, {create: true})
})).then(() => {
dec()
send('files-updated')
}).catch((e) => {
debug(e.stack)
})

add(files, root, ipfs)
.then(anyway)
.catch((e) => { anyway(); debug(e.stack) })
}
}
2 changes: 1 addition & 1 deletion src/panes/Files.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const fileTarget = {
filesArray.push(files[i].path)
}

ipcRenderer.send('drop-files', filesArray, component.state.root)
ipcRenderer.send('drop-files', filesArray, component.props.root)
}
}

Expand Down