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 5, 2017
1 parent db1e1bc commit 5d2ed55
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion 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 @@ -520,14 +521,27 @@ function registerForDownloadListener (session) {
}

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 }))
let savePath = ((process.env.SPECTRON || (!getSetting(settings.DOWNLOAD_ALWAYS_ASK) && !item.promptForSaveLocation())) ? defaultPath : dialog.showSaveDialog(win, { defaultPath }))

// User cancelled out of save dialog prompt
if (!savePath) {
event.preventDefault()
return
}

let willNotOverwrite = false
let matchedFilenames = 0
let querySavePath
while (!willNotOverwrite) {
querySavePath = (matchedFilenames ? savePath.substring(0, savePath.lastIndexOf('.') >= 0 ? savePath.lastIndexOf('.') : savePath.length) + ` (${matchedFilenames})` + savePath.substring(savePath.lastIndexOf('.') >= 0 ? savePath.lastIndexOf('.') : savePath.length) : savePath)
if (!fs.existsSync(querySavePath)) {
willNotOverwrite = true
savePath = querySavePath
} else {
matchedFilenames++
}
}

item.setSavePath(savePath)
appActions.changeSetting(settings.DEFAULT_DOWNLOAD_SAVE_PATH, path.dirname(savePath))

Expand Down

0 comments on commit 5d2ed55

Please sign in to comment.