-
Notifications
You must be signed in to change notification settings - Fork 521
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
Deduplicate file names by adding a suffix on the file name #708
base: master
Are you sure you want to change the base?
Deduplicate file names by adding a suffix on the file name #708
Conversation
What about iOS? |
fea2165
to
7cf781a
Compare
iOS has a different model for the file system, where we do not share files between applications, without using the Share option. Besides that, the lines 948 to 950 in FlutterDownloaderPlugin.m, highlighted below remove the file before downloading it. In that way, duplicate file names does not appear to be an issue for iOS. if ([fileManager fileExistsAtPath:[destinationURL path]]) {
[fileManager removeItemAtURL:destinationURL error:nil];
} So, my question is, should I change the above lines to replicate the Android behaviour? If you want to change this, I can do it on this PR, or as a separate PR. |
Ah, OK, thanks for explaining :)
Overall, what do you think @jenseralmeida? |
I think we should change the iOS behavior and deduplicate the file name also. I'll work on the objective-C and push an update anytime next week.
I do agree also to add the options as a long-term solution, as we can have a fix for an issue ASAP, and a better plan to manage the files on a future version.
Together with the long-term solution, I think that a new SourceFileName or OriginalFileName property should be added to the DownloadTask class, allowing better control of what to do, by the plugin users.
Best regards,
Jenser Almeida
…________________________________
From: Bartek Pacia ***@***.***>
Sent: Friday, August 19, 2022 12:44:46 PM
To: fluttercommunity/flutter_downloader ***@***.***>
Cc: Jenser Almeida ***@***.***>; Mention ***@***.***>
Subject: Re: [fluttercommunity/flutter_downloader] Deduplicate files names by adding a (#dedup-name) suffix on the file name (PR #708)
Ah, OK, thanks for explaining :)
1. What happens currently on Android when file name already exists?
2. Regarding the issue – I think that the most important thing is to have the same behavior across all platforms so that if a file with the same name on iOS exists, it is deleted, then Android should work the same way. We risk overwriting some user files, but I don't see a better solution as of now.
Alternatively, we could other way around and deduplicate file names on iOS as well – then I'd ask you to write some Objective-C to implement this on iOS.
1. In the longer term, I think we could provide some mechanism like FileSaveStrategy, which would be an enum with 3 states: fail, duplicate, or overwrite.
Overall, what do you think @jenseralmeida<https://nam12.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fjenseralmeida&data=05%7C01%7C%7C168274d2a94043f19cba08da821b4592%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C637965350885605722%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=Ox3xRhXhjL0OlB4cSnstlzvpGna4Vx0qBu5PACr467U%3D&reserved=0>?
—
Reply to this email directly, view it on GitHub<https://nam12.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Ffluttercommunity%2Fflutter_downloader%2Fpull%2F708%23issuecomment-1221038592&data=05%7C01%7C%7C168274d2a94043f19cba08da821b4592%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C637965350885605722%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=wrup1BSIJr6pIjnnAJA1%2BmGpDPz3zyaiFR5UVY87BEE%3D&reserved=0>, or unsubscribe<https://nam12.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fnotifications%2Funsubscribe-auth%2FAAFBE2ZFB2PGNLZKP2FVSTDVZ7P25ANCNFSM567BEH7Q&data=05%7C01%7C%7C168274d2a94043f19cba08da821b4592%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C637965350885605722%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=7BtbNm1vkSfRLMusDkhZlfydQ7Lj8JwVkoMAzPR24Cw%3D&reserved=0>.
You are receiving this because you were mentioned.Message ID: ***@***.***>
|
Sorry, for the first question, about what happens to Android when the file name already exists, the answer is, the download fails, without any reasonable feedback.
Please, look into the issue #620 for the details, and the reason for this PR is to fix this issue, which in my case, is a blocker to promote an application to production.
Best regards,
Jenser Almeida
…________________________________
From: Jenser Almeida ***@***.***>
Sent: Friday, August 19, 2022 2:02:58 PM
To: fluttercommunity/flutter_downloader ***@***.***>; fluttercommunity/flutter_downloader ***@***.***>
Cc: Mention ***@***.***>
Subject: Re: [fluttercommunity/flutter_downloader] Deduplicate files names by adding a (#dedup-name) suffix on the file name (PR #708)
I think we should change the iOS behavior and deduplicate the file name also. I'll work on the objective-C and push an update anytime next week.
I do agree also to add the options as a long-term solution, as we can have a fix for an issue ASAP, and a better plan to manage the files on a future version.
Together with the long-term solution, I think that a new SourceFileName or OriginalFileName property should be added to the DownloadTask class, allowing better control of what to do, by the plugin users.
Best regards,
Jenser Almeida
________________________________
From: Bartek Pacia ***@***.***>
Sent: Friday, August 19, 2022 12:44:46 PM
To: fluttercommunity/flutter_downloader ***@***.***>
Cc: Jenser Almeida ***@***.***>; Mention ***@***.***>
Subject: Re: [fluttercommunity/flutter_downloader] Deduplicate files names by adding a (#dedup-name) suffix on the file name (PR #708)
Ah, OK, thanks for explaining :)
1. What happens currently on Android when file name already exists?
2. Regarding the issue – I think that the most important thing is to have the same behavior across all platforms so that if a file with the same name on iOS exists, it is deleted, then Android should work the same way. We risk overwriting some user files, but I don't see a better solution as of now.
Alternatively, we could other way around and deduplicate file names on iOS as well – then I'd ask you to write some Objective-C to implement this on iOS.
1. In the longer term, I think we could provide some mechanism like FileSaveStrategy, which would be an enum with 3 states: fail, duplicate, or overwrite.
Overall, what do you think @jenseralmeida<https://nam12.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fjenseralmeida&data=05%7C01%7C%7C168274d2a94043f19cba08da821b4592%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C637965350885605722%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=Ox3xRhXhjL0OlB4cSnstlzvpGna4Vx0qBu5PACr467U%3D&reserved=0>?
—
Reply to this email directly, view it on GitHub<https://nam12.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Ffluttercommunity%2Fflutter_downloader%2Fpull%2F708%23issuecomment-1221038592&data=05%7C01%7C%7C168274d2a94043f19cba08da821b4592%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C637965350885605722%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=wrup1BSIJr6pIjnnAJA1%2BmGpDPz3zyaiFR5UVY87BEE%3D&reserved=0>, or unsubscribe<https://nam12.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fnotifications%2Funsubscribe-auth%2FAAFBE2ZFB2PGNLZKP2FVSTDVZ7P25ANCNFSM567BEH7Q&data=05%7C01%7C%7C168274d2a94043f19cba08da821b4592%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C637965350885605722%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=7BtbNm1vkSfRLMusDkhZlfydQ7Lj8JwVkoMAzPR24Cw%3D&reserved=0>.
You are receiving this because you were mentioned.Message ID: ***@***.***>
|
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); | ||
} |
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'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.
@jenseralmeida Okay, so I'm waiting for you to add deduplication of file name for iOS as well, and then I'll merge this because we'll have consistent behavior across iOS and Android. Also, please take a look at my comment :) |
Also:
I think this is a great idea. I needed this feature in my private app which uses this plugin (in the end, I think I found some workaround or something – don't remember exactly). If you have time and are willing to contribute this feature as well, I'd be more than happy to review it and (hopefully) merge :) |
Why is this done not on dart side, if you want this to be on both ios/android? |
@bartekpacia I've made simple function and written tests for it. Why you think that big changes is bad in this case? It doesn't break the api as I understand.
|
@IlyaMax I'm only maintaining this package and would prefer not to break any dependents. Just trying to stay on the safe side. But now I see that indeed, we can duplicate file names on the Dart side. After all, this is @jenseralmeida's PR, so it's up to him to implement this functionality. You might want to create a PR to his fork with your Dart changes. |
Hi @IlyaMax, I can take a look into it, and as I did not write it on Objective-C yet, and @bartekpacia feels that it does fit as a safe option then I will move it out from Java to Dart and apply it to both platforms. On the timeframe side, I am stuck with other things, so I am not sure when I will work on this. If @IlyaMax wants to open another PR with his proposed changes, he can do it. When I find time to work on this, I will check if this is still a pending issue and complete its work. |
Hi, any news about this? |
@lugael Hi, unfortunately, no news. If you're willing to pick this up, then here's a rough TODO list:
|
3ce851e
to
cda1324
Compare
Fixes #620