flutter_downloader
flutter_downloader copied to clipboard
download error if file exists
/**
* Create a file using java.io API
*/
private File createFileInAppSpecificDir(String filename, String savedDir) {
File newFile = new File(savedDir, filename);
try {
boolean rs = newFile.createNewFile();
if (rs) {
return newFile;
} else {
logError("It looks like you are trying to save file in public storage but not setting 'saveInPublicStorage' to 'true'");
}
} catch (IOException e) {
e.printStackTrace();
logError("Create a file using java.io API failed ");
}
return null;
}
should add this
if (newFile.exists()) {
return newFile;
}
This error is also happening on Android when the file is in the directory but I'm trying to download it using the downloader.
@hnvn do you think there's a better solution for this? I think it's better to append a duplication number to the file name ex(photo.png, photo(1).png)
since different files can have the same file name and rewriting or retrieving the file with the same name isn't the best solution IMO.
I can work on a PR for this fix, I just need your input. :)
This error is also happening on Android when the file is in the directory but I'm trying to download it using the downloader.
@hnvn do you think there's a better solution for this? I think it's better to append a duplication number to the file name
ex(photo.png, photo(1).png)
since different files can have the same file name and rewriting or retrieving the file with the same name isn't the best solution IMO. I can work on a PR for this fix, I just need your input. :)
Same problem here, sound nice if you fix this. I agree too, about append a duplication number when the filename is the same
Create unique locale file with the remote file name: gist
import 'dart:io';
import 'package:path/path.dart' as path;
class FileUtil {
static File getUniqueFile(String folderName, final String? fileName) {
int num = 1;
String destFileName =
fileName ?? '${DateTime.now().millisecondsSinceEpoch}';
String extension = path.extension(destFileName);
String baseName = path.basenameWithoutExtension(destFileName);
File file = File(folderName + path.separator + destFileName);
while (file.existsSync()) {
destFileName = '$baseName (${num++})$extension';
file = File(folderName + path.separator + destFileName);
}
return file;
}
static Future<File> createUniqueFile(
String folderName, final String? name) async {
File uniqueFile = FileUtil.getUniqueFile(folderName, name);
if (!uniqueFile.existsSync()) {
await uniqueFile.create();
}
return uniqueFile;
}
}
any updates on this ? facing the same issue it fails alternately if file exists.
Edit: Thanks @FaKenKoala for your answer, it really helped me ❤. But I think these cases should be handled by plugin.
It happens when the task status for the file in the Downloader DB is not 'complete,' but you have a fully downloaded file in the local directory. The problem is that the plugin sees the fully downloaded file as a partially downloaded one. If the same file exists, the plugin resumes downloading and sends the size of the byes to the server, which causes HTTP status 416 Range Not Satisfiable.
We could reduce the size of downloadedBytes by a small amount and resume downloading in the setupPartialDownloadedDataHeader method.
Same problem form me
@Faaatman Would be great to see your PR fixing this :)
This is worked on in #708 :)
you all have this open failed: EACCES (Permission denied) error? I'm not sure that I have the same issue as you but it seems like that