Skip to content

Deduplicate file names by adding a suffix on the file name #708

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"java.configuration.updateBuildConfiguration": "interactive"
}
1 change: 1 addition & 0 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ apply plugin: 'com.android.library'

android {
compileSdkVersion 31
namespace 'vn.hunghd.flutterdownloader'

defaultConfig {
minSdkVersion 19
Expand Down
3 changes: 1 addition & 2 deletions android/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="vn.hunghd.flutterdownloader">
<manifest xmlns:android="http://schemas.android.com/apk/res/android">

<!-- See https://developer.android.com/training/package-visibility -->
<queries>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,10 @@ private void downloadFile(Context context, String fileURL, String savedDir, Stri
outputStream = context.getContentResolver().openOutputStream(uri, "w");
} else {
File file = createFileInAppSpecificDir(filename, savedDir);
if (file.getName() != filename) {
filename = file.getName();
taskDao.updateTask(getId().toString(), filename, contentType);
}
savedFilePath = file.getPath();
outputStream = new FileOutputStream(file, false);
}
Expand Down Expand Up @@ -499,6 +503,28 @@ private void downloadFile(Context context, String fileURL, String savedDir, Stri
private File createFileInAppSpecificDir(String filename, String savedDir) {
File newFile = new File(savedDir, filename);
try {
int deduplicationFileNumber = 0;
while (newFile.exists()) {
deduplicationFileNumber++;
int fileNameExtensionIndex = filename.lastIndexOf(".");
String fileNameWithoutExtension;
String fileExtension;
if (fileNameExtensionIndex != -1) {
fileNameWithoutExtension = filename.substring(0, fileNameExtensionIndex);
if (fileNameExtensionIndex + 1 < filename.length()) {
fileExtension = "." + filename.substring(fileNameExtensionIndex + 1);
} else if (fileNameExtensionIndex + 1 == filename.length()) {
fileExtension = ".";
} else {
fileExtension = "";
}
} else {
fileNameWithoutExtension = filename;
fileExtension = "";
}
newFile = new File(savedDir,
fileNameWithoutExtension + "(" + deduplicationFileNumber + ")" + fileExtension);
}
Comment on lines +506 to +527
Copy link
Collaborator

@bartekpacia bartekpacia Aug 19, 2022

Choose a reason for hiding this comment

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

I'd like to have this logic extracted to some method and unit-tested.

I know, there aren't any tests yet and the overall quality of the Java code in the plugin is not very good, but at least we can try to mitigate the problem :)

PS If we had Android side in Kotlin, we could simplify this code (see link). I'm thinking about converting to Kotlin soon.

boolean rs = newFile.createNewFile();
if (rs) {
return newFile;
Expand Down