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

fix: print useful error on write fail #1843

Merged
merged 2 commits into from
Jun 17, 2021
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: 1 addition & 1 deletion assets/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@
},
"couldNotSaveDialog": {
"title": "Could not write to disk",
"message": "There was an error writing to the disk. Please try again."
"message": "There was an error writing to \"{ dir }\": \"{ error }\". Please try again or inspect logs for more details."
},
"launchAtLoginNotSupported": {
"title": "Error",
Expand Down
3 changes: 2 additions & 1 deletion src/dialogs/prompt/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ module.exports = async function showPrompt (options) {
? pallette.dark.background
: pallette.default.background,
webPreferences: {
nodeIntegration: true
nodeIntegration: true,
contextIsolation: false
},
...options.window
})
Expand Down
28 changes: 23 additions & 5 deletions src/download-cid.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { join } = require('path')
const { join, dirname } = require('path')
const fs = require('fs-extra')
const i18n = require('i18next')
const isIPFS = require('is-ipfs')
Expand All @@ -18,8 +18,25 @@ const SHORTCUT = IS_MAC
: 'CommandOrControl+Alt+D'

async function saveFile (dir, file) {
const location = join(dir, file.path)
await fs.outputFile(location, file.content)
const destination = join(dir, file.path)
// ensure files are saved within the dir picked by the user
if (!destination.startsWith(dir)) {
throw new Error(`unable to create '${file.path}' outside of '${dir}'`)
}
lidel marked this conversation as resolved.
Show resolved Hide resolved
// abort if destination already exists (safer default than overwriting user data)
if (fs.existsSync(destination)) {
throw new Error(`unable to create '${file.path}' as it already exists at '${destination}'`)
}
// abort if symlinks in the target dir point outside of it
const subDir = dirname(destination)
if (fs.existsSync(subDir)) {
const realRootDir = await fs.realpath(dir)
const realSubDir = await fs.realpath(subDir)
if (!realSubDir.startsWith(realRootDir)) {
throw new Error(`unable to create subdir '${realSubDir}' outside of '${realRootDir}'`)
}
}
await fs.outputFile(destination, file.content)
}

async function get (ipfs, cid) {
Expand Down Expand Up @@ -129,11 +146,12 @@ async function downloadCid (ctx) {
shell.showItemInFolder(join(dir, files[0].path))
}
} catch (err) {
logger.error(`[cid download] ${err.toString()}`)
const errMsg = err.toString()
logger.error(`[cid download] ${errMsg}`)

showDialog({
title: i18n.t('couldNotSaveDialog.title'),
message: i18n.t('couldNotSaveDialog.message'),
message: i18n.t('couldNotSaveDialog.message', { dir, error: errMsg }),
buttons: [i18n.t('close')]
})
}
Expand Down