http icon indicating copy to clipboard operation
http copied to clipboard

downloadFile fails on IOS if the path already exists

Open corentin-gautier opened this issue 5 years ago • 4 comments

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

corentin-gautier avatar Sep 14 '20 11:09 corentin-gautier

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 });

Nazirovich avatar Oct 09 '20 05:10 Nazirovich

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

thomasvidas avatar Jan 19 '21 16:01 thomasvidas

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?

nhh avatar Jul 22 '21 05:07 nhh

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...

muuvmuuv avatar Jul 21 '22 08:07 muuvmuuv