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

fix(filesystem): Allow content:// paths to be used in Filesystem.copy() #1301

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

BARNZ
Copy link

@BARNZ BARNZ commented Nov 25, 2022

This is my attempt to fix ionic-team/capacitor#6114

Submitting a content:// path to Filesystem.copy such as one provided by @capawesome/capacitor-file-picker plugin would previously result in the application crashing despite the Capacitor filesystem docs stating the following:

Additionally, the Filesystem API supports using full file:// paths, or reading content:// files on Android. Simply leave out the directory param to use a full file path.

This merge requests adds some additional handling when copying from a content:// path:

  • Create a temp file
  • Resolve and read the content stream to this temp file
  • Use this temp file as the source object (so all existing behavior works as usual)
  • Clean up this temp file afterwards once the file has been copied

The following example will now work correctly from application code:

Filesystem.copy({
    from: 'content://path/to/some/photo.jpg',
    to: 'Media/myphoto.jpg',
    toDirectory: Directory.External
})

The main reason for doing this is to avoid memory issues when the user picks large files. Native file copying can be used instead of reading the file as base64 over the bridge and writing back to a new path manually.

@abreu1234
Copy link

me to need this correction

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

bug: Filesystem.copy without specifying a source directory results in a crash
2 participants