Skip to content
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

downloadFile directory default value is incorrect #1717

Open
jcesarmobile opened this issue Aug 3, 2023 · 4 comments
Open

downloadFile directory default value is incorrect #1717

jcesarmobile opened this issue Aug 3, 2023 · 4 comments
Labels
plugin: filesystem type: bug A confirmed bug report

Comments

@jcesarmobile
Copy link
Member

Bug Report

Plugin(s)

filesystem

Capacitor Version

5

Platform(s)

all

Current Behavior

directory is documented as

  /**
   * The directory to write the file to.
   * If this option is used, filePath can be a relative path rather than absolute.
   * The default is the `DATA` directory.
   *
   * @since 5.1.0
   */
  directory?: Directory;

but on iOS the default is DOCUMENTS.
on Android the default is not one of the known directories, but Environment.DIRECTORY_DOWNLOADS.
on web the file is not saved into any directory of the Filesystem, but it downloads the actual file

Expected Behavior

The docs should be updated to document the actual behavior, or the code should be adjusted to match the documented behavior.

Code Reproduction

Other Technical Details

Additional Context

@ionitron-bot ionitron-bot bot added the triage label Aug 3, 2023
@jcesarmobile jcesarmobile added plugin: filesystem type: bug A confirmed bug report and removed triage labels Aug 3, 2023
@ionitron-bot
Copy link

ionitron-bot bot commented Aug 3, 2023

This issue has been labeled as type: bug. This label is added to issues that that have been reproduced and are being tracked in our internal issue tracker.

@matleh
Copy link

matleh commented Nov 23, 2023

With the current default on Android, it is not possible to use an absolute path, which is a serious limitation.

That is because to use an absolute path, directory must be null but if directory: null is passed in the options, the default of Environment.DIRECTORY_DOWNLOADS is used.

@matleh
Copy link

matleh commented Nov 23, 2023

I use an ugly work-around:

async function downloadFile({ url, path, directory }) {
  const postfix = Math.random().toString(36).substring(2);
  let tmpPath;
  if (directory === null) {
    // work-around for Capacitor bug - https://github.com/ionic-team/capacitor-plugins/issues/1717
    try {
      const { path: tmpPath } =  await Filesystem.downloadFile({
        url,
        path: `tmp-${postfix}`,
        directory: Directory.Cache,
      });
      return await Filesystem.copy({ from: tmpPath, to: path });
    } finally {
      if (tmpPath) {
        await Filesystem.deleteFile({ path: tmpPath })
      }
    }
  }
  return await Filesystem.downloadFile({ url, path, directory });
}

@jpike88

This comment was marked as abuse.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
plugin: filesystem type: bug A confirmed bug report
Projects
None yet
Development

No branches or pull requests

3 participants