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

Make save files sequential #107

Merged
merged 1 commit into from
May 14, 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
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