Skip to content

Commit

Permalink
Don't overwrite existing files when DOWNLOAD_ALWAYS_ASK is false (fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
timborden committed Jul 6, 2017
1 parent db1e1bc commit 8a64255
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions app/filtering.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const dialog = electron.dialog
const app = electron.app
const uuid = require('uuid')
const path = require('path')
const fs = require('fs')
const getOrigin = require('../js/state/siteUtil').getOrigin
const {adBlockResourceName} = require('./adBlock')
const {updateElectronDownloadItem} = require('./browser/electronDownloadItem')
Expand Down Expand Up @@ -519,8 +520,22 @@ function registerForDownloadListener (session) {
itemFilename = item.getFilename()
}

const defaultPath = path.join(getSetting(settings.DOWNLOAD_DEFAULT_PATH) || getSetting(settings.DEFAULT_DOWNLOAD_SAVE_PATH) || app.getPath('downloads'), itemFilename)
const savePath = ((process.env.SPECTRON || (!getSetting(settings.DOWNLOAD_ALWAYS_ASK) && !item.promptForSaveLocation())) ? defaultPath : dialog.showSaveDialog(win, { defaultPath }))
const defaultDir = (getSetting(settings.DOWNLOAD_DEFAULT_PATH) || getSetting(settings.DEFAULT_DOWNLOAD_SAVE_PATH) || app.getPath('downloads'))
let savePath
if (process.env.SPECTRON || (!getSetting(settings.DOWNLOAD_ALWAYS_ASK) && !item.promptForSaveLocation())) {
let willOverwrite = true
let matchedFilenames = 0
while (willOverwrite) {
savePath = path.join(defaultDir, (matchedFilenames ? `${itemFilename.replace(new RegExp(`${path.extname(itemFilename)}$`), '')} (${matchedFilenames})${path.extname(itemFilename)}` : itemFilename))
if (!fs.existsSync(savePath)) {
willOverwrite = false
} else {
matchedFilenames++
}
}
} else {
savePath = dialog.showSaveDialog(win, { defaultPath: path.join(defaultDir, itemFilename) })
}

// User cancelled out of save dialog prompt
if (!savePath) {
Expand Down

0 comments on commit 8a64255

Please sign in to comment.