Skip to content

Conversation

@CDRussell
Copy link
Member

@CDRussell CDRussell commented Aug 5, 2020

Task/Issue URL: https://app.asana.com/0/0/1187737416677993/f
Tech Design URL:
CC:

Description:
When trying to download images, our app currently tries to guess what a good filename for the download will be, and often fails to produce something nice; it'll often end in a .*bin file type as its current implementation is somewhat naive in turning a URL into a filename.

This PR improves that situation by walking back the URL path, trying each to make a filename, and then returning the best filename produced. Worst case, this should produce the same result as the old algorithm, but best case it will pick out a much better filename.

Steps to test this PR:

  1. Visit https://vignette3.wikia.nocookie.net/guns/images/b/b1/FHJ84.jpg/revision/latest?cb=20160824141549 and long press to download the image; verify the filename produced is FHJ84.jpg and not latest.bin as it was before
  2. Try other image downloads, and verify they produce a sensible enough filename

Internal references:

Software Engineering Expectations
Technical Design Template

@CDRussell CDRussell requested a review from cmonfortep August 7, 2020 08:40
@CDRussell CDRussell changed the title improve download filename guessing Improve algorithm to generate a filename from an image download Aug 7, 2020
@CDRussell CDRussell marked this pull request as ready for review August 7, 2020 08:40
Copy link
Contributor

@cmonfortep cmonfortep left a comment

Choose a reason for hiding this comment

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

I really liked to review the PR starting with the unit tests you prepared, it made me easier to understand the algorithm. I posted a few questions though.

Also, while testing this I found a difference between current develop (and main) vs this branch.

  1. Go to DDG and search something (e.g: wombat)
  2. Go to image tab
  3. Long press on an image
  4. Develop will download something like downloadfile.bin that can be opened by any app gallery.
  5. This branch will download something like downloadfile (without extension), and file can't be opened.

import org.junit.Assert.assertEquals
import org.junit.Test

class UriUtilsFilenameExtractorTest {
Copy link
Contributor

Choose a reason for hiding this comment

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

I still need to look the real implementation, but after going through the test cases, I have a question: What happens

  1. if url is empty, mimeType and contentDisposition are not, which one of them as prevalence over the other?
  2. if url is not empty and contains a filename, but mimetype says another thing. which one will be returned?

Copy link
Member Author

@CDRussell CDRussell Aug 7, 2020

Choose a reason for hiding this comment

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

  1. we would still fall back on URLUtil.guessFileName which is ultimately deciding on the precedence between them. I think that might even be varying the precedence based on OS version which isn't ideal, but that is unchanged from current behavior
  2. I see mimetype being preferred, but again, deferring that logic to URLUtil.guessFileName. so for this scenario, the behavior is unchanged from the current behavior.

return NotGoodEnough
}

private fun guessFromUrl(url: String, contentDisposition: String?, mimeType: String?): String {
Copy link
Contributor

Choose a reason for hiding this comment

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

That made me think this method was going to use only url: String to guess the fileName, but I think we will also use also the other params. Wanted to check if my assumption is true

Copy link
Member Author

Choose a reason for hiding this comment

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

Good point; changed to guessFilename() which is hopefully clearer

@cmonfortep cmonfortep self-assigned this Aug 7, 2020
@CDRussell CDRussell requested a review from cmonfortep August 7, 2020 16:44
…craig/improve_download_filename_guessing

# Conflicts:
#	app/src/main/java/com/duckduckgo/app/browser/DownloadConfirmationFragment.kt
#	app/src/main/java/com/duckduckgo/app/browser/downloader/FileDownloader.kt
Copy link
Contributor

@cmonfortep cmonfortep left a comment

Choose a reason for hiding this comment

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

LGTM thanks for the changes!

@CDRussell CDRussell merged commit 64485a5 into develop Aug 10, 2020
@CDRussell CDRussell deleted the feature/craig/improve_download_filename_guessing branch August 10, 2020 10:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants