Skip to content
This repository has been archived by the owner on Dec 11, 2019. It is now read-only.

Don't overwrite existing files when DOWNLOAD_ALWAYS_ASK is false #9867

Merged
merged 1 commit into from
Aug 14, 2017

Conversation

timborden
Copy link
Contributor

@timborden timborden commented Jul 5, 2017

Fixes #7764

Submitter Checklist:

  • Submitted a ticket for my issue if one did not already exist.
  • Used Github auto-closing keywords in the commit message.
  • Added/updated tests for this change (for new code or code which already has tests).
  • Ran git rebase -i to squash commits (if needed).
  • Tagged reviewers and labelled the pull request as needed.

Test Plan:

  • Tested that the file was downloaded to the location specified in the settings
  • Tested that a second downloaded file would not replace an existing file of the same name

Reviewer Checklist:

Tests

  • Adequate test coverage exists to prevent regressions
  • Tests should be independent and work correctly when run individually or as a suite ref
  • New files have MPL2 license header

app/filtering.js Outdated
@@ -520,14 +521,29 @@ 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 }))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there should be a single if statement here that wraps all of this, but can you switch !getSetting(settings.DOWNLOAD_ALWAYS_ASK) && !item.promptForSaveLocation() to getSetting(settings.DOWNLOAD_ALWAYS_ASK) || item.promptForSaveLocation()? The former is confusing

app/filtering.js Outdated

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

if (!getSetting(settings.DOWNLOAD_ALWAYS_ASK) && !item.promptForSaveLocation()) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please merge into else for if above

app/filtering.js Outdated

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

if (!getSetting(settings.DOWNLOAD_ALWAYS_ASK) && !item.promptForSaveLocation()) {
let willNotOverwrite = false
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

double negative checks are also hard to understand !willNotOverwrite -> willOverwrite

app/filtering.js Outdated
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)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this will break things like .tar.gz. Maybe get the first index?

@timborden timborden force-pushed the feature/download-filename-exists branch from 11299f7 to 8a64255 Compare July 6, 2017 10:06
@timborden
Copy link
Contributor Author

Thanks for the feedback @bridiver

I've refactored the code based on your suggestions.

I also dug into the extension name edge cases (like tar.gz). As a result I switched from the substring() approach to using path.extname(). Sadly, it didn't solve the issue, but the code is shorter and more readable.

I did some searching for possible solutions. One thought was to use the MIME:

> var mime = require('mime-types')
undefined
> mime.lookup('package.json')
'application/json'
> mime.extension(mime.lookup('package.json'))
'json'

....sadly, it didn't work for tar.gz files

> mime.lookup('v0.17.13dev.tar.gz')
false

It seems that the most common approach is to split on the last "."...even the OS X save prompt defaults to using the last ".":
screen shot 2017-07-06 at 11 01 42

Short of rolling with a whitelist of file extensions, which feels like overkill....it looks like this may be the best option.

@timborden timborden force-pushed the feature/download-filename-exists branch from 8a64255 to 5b7a0b3 Compare July 11, 2017 14:02
@timborden
Copy link
Contributor Author

timborden commented Jul 11, 2017

Just added tests....let me know if you think it needs more coverage.

@luixxiul
Copy link
Contributor

luixxiul commented Aug 5, 2017

@timborden would you please rebase on the latest master? thanks!

@luixxiul luixxiul modified the milestones: 0.21.x (Nightly Channel), 0.20.x (Developer Channel) Aug 7, 2017
@timborden timborden force-pushed the feature/download-filename-exists branch from 5b7a0b3 to 3cba178 Compare August 9, 2017 15:47
@timborden
Copy link
Contributor Author

timborden commented Aug 9, 2017

Hey @luixxiul ...sorry for the delay (just changed my GitHub settings so I don't miss these notifications)

I just rebased....FYI

@luixxiul
Copy link
Contributor

luixxiul commented Aug 9, 2017

@timborden thanks! I'd like to see you keep up the nice work :-)

@codecov-io
Copy link

codecov-io commented Aug 9, 2017

Codecov Report

Merging #9867 into master will decrease coverage by 0.03%.
The diff coverage is 6.66%.

@@            Coverage Diff             @@
##           master    #9867      +/-   ##
==========================================
- Coverage   53.67%   53.63%   -0.04%     
==========================================
  Files         238      238              
  Lines       21049    21063      +14     
  Branches     3256     3258       +2     
==========================================
+ Hits        11297    11298       +1     
- Misses       9752     9765      +13
Flag Coverage Δ
#unittest 53.63% <6.66%> (-0.04%) ⬇️
Impacted Files Coverage Δ
app/filtering.js 18.07% <6.66%> (-0.27%) ⬇️

@timborden timborden changed the title Don't overwrite existing files when DOWNLOAD_ALWAYS_ASK is false (fix… Don't overwrite existing files when DOWNLOAD_ALWAYS_ASK is false Aug 9, 2017
Copy link
Member

@bsclifton bsclifton left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code works great 😄 👍 Good job on this one (including the tests too!)

@bsclifton
Copy link
Member

@bridiver did you want to re-review before this gets merged?

@bsclifton bsclifton merged commit 2f9ef72 into brave:master Aug 14, 2017
@bsclifton
Copy link
Member

Merged 😄 👌

@timborden
Copy link
Contributor Author

Awesome....thanks @bsclifton, @bridiver and @luixxiul

@bbondy bbondy modified the milestones: 0.21.x (Developer Channel), 0.20.x (Beta Channel) Oct 25, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants