Skip to content

Commit

Permalink
fix(Android): Don't crash while downloading file with % in filename (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
valentinaporch committed May 22, 2023
1 parent 681aac4 commit 81e3aa4
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ import java.net.MalformedURLException
import java.net.URL
import java.util.*

val invalidCharRegex = "[\\\\/%\"]".toRegex()


class RNCWebViewManagerImpl {
companion object {
Expand Down Expand Up @@ -97,7 +99,11 @@ class RNCWebViewManagerImpl {
Log.w(TAG, "Unsupported URI, aborting download", e)
return@DownloadListener
}
val fileName = URLUtil.guessFileName(url, contentDisposition, mimetype)
var fileName = URLUtil.guessFileName(url, contentDisposition, mimetype)

// Sanitize filename by replacing invalid characters with "_"
fileName = fileName.replace(invalidCharRegex, "_")

val downloadMessage = "Downloading $fileName"

//Attempt to add cookie, if it exists
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@

import java.io.File;
import java.io.IOException;
import java.lang.SecurityException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
Expand Down Expand Up @@ -305,7 +306,7 @@ public void downloadFile(String downloadingMessage) {

try {
dm.enqueue(mDownloadRequest);
} catch (IllegalArgumentException e) {
} catch (IllegalArgumentException | SecurityException e) {
Log.w("RNCWebViewModule", "Unsupported URI, aborting download", e);
return;
}
Expand Down

0 comments on commit 81e3aa4

Please sign in to comment.