downloadFile fails on IOS if the path already exists
Describe the bug I can't download a file and replace the existing one
couldn’t be moved to “Caches” because an item with the same name already exists.
To Reproduce use downloadFile twice on an ios device
Expected behavior We should be able to override the existing file
Smartphone (please complete the following information):
- OS: IOS
I faced the same issue so as a workaround I've added Date.now() before the fileName:
const ret = await Http.downloadFile({ url: url, filePath: Date.now().toString().concat('-', file.fileName), fileDirectory: FilesystemDirectory.Documents });
I think the default behavior should not overwrite existing files, but I wouldn't be opposed to adding an option to HttpDownloadFileOptions so that you can overwrite files
I am facing the problem, that the file already exists, which is ok - but it seems it is happening with a temp file name.
File Dest file:///Users/nhh/Library/Developer/CoreSimulator/Devices/9EDFDA33-B1E3-4536-9D9D-200C64EF05A1/data/Containers/Data/Application/4CC14443-7988-45D7-A840-3E63EF3B2CB8/Documents/my-file-name.png
ERROR MESSAGE: {"code":"DOWNLOAD","message":"Unable to download file","errorMessage":"“CFNetworkDownload_rBTFUb.tmp” couldn’t be moved to “Documents” because an item with the same name already exists."}
⚡️ [error] - {"code":"DOWNLOAD","message":"Unable to download file","errorMessage":"“CFNetworkDownload_rBTFUb.tmp” couldn’t be moved to “Documents” because an item with the same name already exists."}
I am also explicitely handle the "it already exists" case, but it seems not to work initially.
const fileName = "Defined elsewhere";
try {
// stat throws, so we need to try catch
await Filesystem.stat({
path: fileName,
directory: FilesystemDirectory.Data,
});
const uri = await Filesystem.getUri({
path: fileName,
directory: FilesystemDirectory.Data,
});
return Capacitor.convertFileSrc(uri.uri);
} catch (e) {
// File could not be found, going on...
}
While I am writing this, I noticed, there could be two images in my list, which are the same, which initially don't exist and therefore being tried to download.... twice.
Thank you all for being my rubber-duck. :)
EDIT: I am thinking about a solution, unfortunately without the override option, I need to manually deduplicate my list of images which initially dont exist. This is hard because I am not in control of "load a list" I am either doing, it on the fly.
A workaround would be to append a Date.now() to the file name, but that completely destroys the purpose of saving images on the disk (imo). It also pollutes my clients phones with duplicates.
Do you guys have any idea?
Quick fix
try {
await Filesystem.stat({
directory: options.fileDirectory,
path: options.filePath,
})
await Filesystem.deleteFile({
directory: options.fileDirectory,
path: options.filePath,
})
} catch {
// noop
}
I hope this will be fixed with HTTP included in window.fetch...