Skip to content
This repository has been archived by the owner on Jul 7, 2024. It is now read-only.
/ Orion Public archive

Commit

Permalink
Merge pull request #107 from Siderus/bugfix/122-save-files-sequentially
Browse files Browse the repository at this point in the history
Make save files sequential
  • Loading branch information
kernelwhisperer authored May 14, 2018
2 parents a302438 + eab5c80 commit 2b69a17
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 23 deletions.
6 changes: 5 additions & 1 deletion app/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,11 @@ export function saveFileToPath (hash, dest) {

// First make all the directories
if (file.type === 'dir' || !file.content) {
mkdirSync(finalDest)
try {
mkdirSync(finalDest)
} catch (err) {
if (err.code !== 'EEXIST') throw err
}
} else {
// Pipe the file content into an actual write stream
const writeStream = createWriteStream(finalDest)
Expand Down
39 changes: 17 additions & 22 deletions app/windows/Storage/Components/Header.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,31 +67,26 @@ class Header extends React.Component {
if (this.props.storageStore.selected.length === 0) return

const selected = this.props.storageStore.selected
const opts = { title: 'Where should I save?' }

let promises
// More than one object/element
if (selected.length > 1) {
opts.properties = ['openDirectory', 'createDirectory']
opts.buttonLabel = 'Save everything here'
const filePaths = remote.dialog.showOpenDialog(remote.app.mainWindow, opts)
if (!filePaths) return

const destination = filePaths[0]
promises = selected.map(element => saveFileToPath(element.hash, destination))
} else {
// selected.length === 1
opts.properties = ['openDirectory']
opts.buttonLabel = 'Save here'
const filePaths = remote.dialog.showOpenDialog(remote.app.mainWindow, opts)
if (!filePaths) return

const destination = filePaths[0]
promises = [saveFileToPath(selected[0].hash, destination)]
const opts = {
title: 'Where should I save?',
properties: ['openDirectory', 'createDirectory'],
buttonLabel: 'Save here'
}
const filePaths = remote.dialog.showOpenDialog(remote.app.mainWindow, opts)
if (!filePaths) return

const destination = filePaths[0]
let sequentialChain = Promise.resolve()

// we call saveFileToPath only after the previous call finished (in .then)
selected.forEach(element => {
sequentialChain = sequentialChain.then((res) => {
return saveFileToPath(element.hash, destination)
})
})

this.setState({ isSavingOnDisk: true })
Promise.all(promises)
sequentialChain
.then(result => this.setState({ isSavingOnDisk: false }))
.catch(err => {
remote.dialog.showErrorBox('Saving to disk has failed', err.message)
Expand Down

0 comments on commit 2b69a17

Please sign in to comment.