-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Improve algorithm to generate a filename from an image download #896
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
Merged
CDRussell
merged 10 commits into
develop
from
feature/craig/improve_download_filename_guessing
Aug 10, 2020
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
92f4548
Improve algorithm used to guess filenames for downloaded files
CDRussell f4a1927
Tidy extraction logic and add additional test cases
CDRussell 00ec84b
Break evaluation earlier and add new test
CDRussell 943f91d
Return .bin if no other filetype available
CDRussell 4d73cc8
Remove unused constructor param
CDRussell 6cb5bda
Add additional test
CDRussell d05b32f
Removed confusing extension function from pendingDownload
CDRussell 20c239a
Merge branch 'develop' of github.com:duckduckgo/Android into feature/…
CDRussell e6f77ae
Remove test that can be OS-dependent
CDRussell f16a358
Merge branch 'develop' of github.com:duckduckgo/Android into feature/…
CDRussell File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
152 changes: 152 additions & 0 deletions
152
...c/androidTest/java/com/duckduckgo/app/browser/downloader/UriUtilsFilenameExtractorTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,152 @@ | ||
| /* | ||
| * Copyright (c) 2020 DuckDuckGo | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.duckduckgo.app.browser.downloader | ||
|
|
||
| import org.junit.Assert.assertEquals | ||
| import org.junit.Test | ||
|
|
||
| class UriUtilsFilenameExtractorTest { | ||
|
|
||
| private val testee: FilenameExtractor = FilenameExtractor() | ||
|
|
||
| @Test | ||
| fun whenUrlEndsWithFilenameAsJpgNoMimeOrContentDispositionThenFilenameShouldBeExtracted() { | ||
| val url = "https://example.com/realFilename.jpg" | ||
| val mimeType: String? = null | ||
| val contentDisposition: String? = null | ||
| val extracted = testee.extract(buildPendingDownload(url, contentDisposition, mimeType)) | ||
| assertEquals("realFilename.jpg", extracted) | ||
| } | ||
|
|
||
| @Test | ||
| fun whenUrlContainsFilenameButContainsAdditionalPathSegmentsThenFilenameShouldBeExtracted() { | ||
| val url = "https://foo.example.com/path/images/b/b1/realFilename.jpg/other/stuff" | ||
| val mimeType: String? = null | ||
| val contentDisposition: String? = null | ||
| val extracted = testee.extract(buildPendingDownload(url, contentDisposition, mimeType)) | ||
| assertEquals("realFilename.jpg", extracted) | ||
| } | ||
|
|
||
| @Test | ||
| fun whenUrlContainsFilenameButContainsAdditionalPathSegmentsAndQueryParamsThenFilenameShouldBeExtracted() { | ||
| val url = "https://foo.example.com/path/images/b/b1/realFilename.jpg/other/stuff?cb=123" | ||
| val mimeType: String? = null | ||
| val contentDisposition: String? = null | ||
| val extracted = testee.extract(buildPendingDownload(url, contentDisposition, mimeType)) | ||
| assertEquals("realFilename.jpg", extracted) | ||
| } | ||
|
|
||
| @Test | ||
| fun whenUrlContainsFilenameButContainsAdditionalPathSegmentsAndQueryParamsWhichLookLikeAFilenameThenFilenameShouldBeExtracted() { | ||
| val url = "https://foo.example.com/path/images/b/b1/realFilename.jpg/other/stuff?cb=123.com" | ||
| val mimeType: String? = null | ||
| val contentDisposition: String? = null | ||
| val extracted = testee.extract(buildPendingDownload(url, contentDisposition, mimeType)) | ||
| assertEquals("realFilename.jpg", extracted) | ||
| } | ||
|
|
||
| @Test | ||
| fun whenUrlContainsFilenameButContentDispositionSaysOtherwiseThenExtractFromContentDisposition() { | ||
| val url = "https://example.com/filename.jpg" | ||
| val mimeType: String? = null | ||
| val contentDisposition: String? = "Content-Disposition: attachment; filename=fromDisposition.jpg" | ||
| val extracted = testee.extract(buildPendingDownload(url, contentDisposition, mimeType)) | ||
| assertEquals("fromDisposition.jpg", extracted) | ||
| } | ||
|
|
||
| @Test | ||
| fun whenFilenameEndsInBinThenThatIsExtracted() { | ||
| val url = "https://example.com/realFilename.bin" | ||
| val mimeType: String? = null | ||
| val contentDisposition: String? = null | ||
| val extracted = testee.extract(buildPendingDownload(url, contentDisposition, mimeType)) | ||
| assertEquals("realFilename.bin", extracted) | ||
| } | ||
|
|
||
| @Test | ||
| fun whenUrlContainsNoFileNameButLotsOfPathsSegmentsThenFirstSegmentNameIsUsed() { | ||
| val url = "https://example.com/foo/bar/files" | ||
| val mimeType: String? = null | ||
| val contentDisposition: String? = null | ||
| val extracted = testee.extract(buildPendingDownload(url, contentDisposition, mimeType)) | ||
| assertEquals("foo.bin", extracted) | ||
| } | ||
|
|
||
| @Test | ||
| fun whenFilenameEndsInBinWithASlashThenThatIsExtracted() { | ||
| val url = "https://example.com/realFilename.bin/" | ||
| val mimeType: String? = null | ||
| val contentDisposition: String? = null | ||
| val extracted = testee.extract(buildPendingDownload(url, contentDisposition, mimeType)) | ||
| assertEquals("realFilename.bin", extracted) | ||
| } | ||
|
|
||
| @Test | ||
| fun whenFilenameContainsBinThenThatIsExtracted() { | ||
| val url = "https://example.com/realFilename.bin/foo/bar" | ||
| val mimeType: String? = null | ||
| val contentDisposition: String? = null | ||
| val extracted = testee.extract(buildPendingDownload(url, contentDisposition, mimeType)) | ||
| assertEquals("realFilename.bin", extracted) | ||
| } | ||
|
|
||
| @Test | ||
| fun whenUrlIsEmptyStringAndNoOtherDataProvidedThenDefaultNameAndBinFiletypeReturned() { | ||
| val url = "" | ||
| val mimeType: String? = null | ||
| val contentDisposition: String? = null | ||
| val extracted = testee.extract(buildPendingDownload(url, contentDisposition, mimeType)) | ||
| assertEquals("downloadfile.bin", extracted) | ||
| } | ||
|
|
||
| @Test | ||
| fun whenUrlIsEmptyStringAndMimeTypeProvidedThenDefaultNameAndFiletypeFromMimeReturned() { | ||
| val url = "" | ||
| val mimeType: String? = "image/jpeg" | ||
| val contentDisposition: String? = null | ||
| val extracted = testee.extract(buildPendingDownload(url, contentDisposition, mimeType)) | ||
| assertEquals("downloadfile.jpg", extracted) | ||
| } | ||
|
|
||
| @Test | ||
| fun whenUrlIsEmptyStringAndContentDispositionProvidedThenExtractFromContentDisposition() { | ||
| val url = "" | ||
| val mimeType: String? = null | ||
| val contentDisposition: String? = "Content-Disposition: attachment; filename=fromDisposition.jpg" | ||
| val extracted = testee.extract(buildPendingDownload(url, contentDisposition, mimeType)) | ||
| assertEquals("fromDisposition.jpg", extracted) | ||
| } | ||
|
|
||
| @Test | ||
| fun whenNoFilenameAndNoPathSegmentsThenDomainNameReturned() { | ||
| val url = "http://example.com" | ||
| val mimeType: String? = null | ||
| val contentDisposition: String? = null | ||
| val extracted = testee.extract(buildPendingDownload(url, contentDisposition, mimeType)) | ||
| assertEquals("example.com", extracted) | ||
| } | ||
|
|
||
| private fun buildPendingDownload(url: String, contentDisposition: String?, mimeType: String?): FileDownloader.PendingFileDownload { | ||
| return FileDownloader.PendingFileDownload( | ||
| url = url, | ||
| contentDisposition = contentDisposition, | ||
| mimeType = mimeType, | ||
| subfolder = "aFolder", | ||
| userAgent = "aUserAgent" | ||
| ) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
app/src/main/java/com/duckduckgo/app/browser/downloader/FilenameExtractor.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| /* | ||
| * Copyright (c) 2020 DuckDuckGo | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.duckduckgo.app.browser.downloader | ||
|
|
||
| import android.webkit.URLUtil | ||
| import androidx.core.net.toUri | ||
| import com.duckduckgo.app.browser.downloader.FileDownloader.PendingFileDownload | ||
| import com.duckduckgo.app.browser.downloader.FilenameExtractor.GuessQuality.NotGoodEnough | ||
| import com.duckduckgo.app.browser.downloader.FilenameExtractor.GuessQuality.TriedAllOptions | ||
| import timber.log.Timber | ||
| import javax.inject.Inject | ||
|
|
||
| class FilenameExtractor @Inject constructor() { | ||
|
|
||
| fun extract(pendingDownload: PendingFileDownload): String { | ||
| val url = pendingDownload.url | ||
| val mimeType = pendingDownload.mimeType | ||
| val contentDisposition = pendingDownload.contentDisposition | ||
|
|
||
| val firstGuess = guessFilename(url, contentDisposition, mimeType) | ||
| val guesses = Guesses(bestGuess = null, latestGuess = firstGuess) | ||
| val baseUrl = url.toUri().host ?: "" | ||
| var pathSegments = pathSegments(url) | ||
|
|
||
| while (evaluateGuessQuality(guesses, pathSegments) != TriedAllOptions) { | ||
| pathSegments = pathSegments.dropLast(1) | ||
| guesses.latestGuess = guessFilename(baseUrl + "/" + pathSegments.rebuildUrl(), contentDisposition, mimeType) | ||
| } | ||
|
|
||
| return bestGuess(guesses) | ||
| } | ||
|
|
||
| private fun evaluateGuessQuality(guesses: Guesses, pathSegments: List<String>): GuessQuality { | ||
| val latestGuess = guesses.latestGuess | ||
|
|
||
| // if it contains a '.' then it's a good chance of a filetype and we can update our best guess | ||
| if (latestGuess.contains(".")) { | ||
| guesses.bestGuess = latestGuess | ||
| } | ||
|
|
||
| if (pathSegments.size < 2) return TriedAllOptions | ||
|
|
||
| return NotGoodEnough | ||
| } | ||
|
|
||
| private fun guessFilename(url: String, contentDisposition: String?, mimeType: String?): String { | ||
| val tidiedUrl = url.removeSuffix("/") | ||
| var guessedFilename = URLUtil.guessFileName(tidiedUrl, contentDisposition, mimeType) | ||
|
|
||
| // we only want to keep the default .bin filetype on the guess if the URL actually has that too | ||
| if (guessedFilename.endsWith(DEFAULT_FILE_TYPE) && !tidiedUrl.endsWith(DEFAULT_FILE_TYPE)) { | ||
| guessedFilename = guessedFilename.removeSuffix(DEFAULT_FILE_TYPE) | ||
| } | ||
|
|
||
| Timber.v("From URL [$url], guessed [$guessedFilename]") | ||
| return guessedFilename | ||
| } | ||
|
|
||
| private fun bestGuess(guesses: Guesses): String { | ||
| val guess = guesses.bestGuess ?: guesses.latestGuess | ||
| if (!guess.contains(".")) { | ||
| return guess + DEFAULT_FILE_TYPE | ||
| } | ||
| return guess | ||
| } | ||
|
|
||
| private fun pathSegments(url: String): List<String> { | ||
| return url.toUri().pathSegments ?: emptyList() | ||
| } | ||
|
|
||
| private fun List<String>.rebuildUrl(): String { | ||
| return joinToString(separator = "/") | ||
| } | ||
|
|
||
| companion object { | ||
| private const val DEFAULT_FILE_TYPE = ".bin" | ||
| } | ||
|
|
||
| sealed class GuessQuality { | ||
| object NotGoodEnough : GuessQuality() | ||
| object TriedAllOptions : GuessQuality() | ||
| } | ||
|
|
||
| data class Guesses(var latestGuess: String, var bestGuess: String? = null) | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
urlis empty,mimeTypeandcontentDispositionare not, which one of them as prevalence over the other?urlis not empty and contains a filename, butmimetypesays another thing. which one will be returned?Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
URLUtil.guessFileNamewhich 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 behaviormimetypebeing preferred, but again, deferring that logic toURLUtil.guessFileName. so for this scenario, the behavior is unchanged from the current behavior.