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

Support file:// and library content:// files #49

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

Conversation

jrichardlai
Copy link

Not sure if that's the best way, but it allows reading file from library or filesystem.

@johanneslumpe
Copy link
Collaborator

@jrichardlai Thanks! I'm not an Android-person but I guess @cjdell has an opinion on this :)

@johanneslumpe
Copy link
Collaborator

@jrichardlai Are you using this in production? I'm inclined to merge this as @cjdell isn't responding. But I cannot test it on my own right now, so I want to make sure that you are positive that this works as expected. Is there anything you think we should add to the readme/docs for this?

@jrichardlai
Copy link
Author

@johanneslumpe I'm using it in production didn't see any problems so far, but this feature is not used a lot. Also I added this change because it makes sense for us ( we use it when we want to retry an upload that fails, which can be from either a image from the library or a new photo ) but I don't know if it fits in this library if react-native-file-system is really about only the file system.

@jrichardlai
Copy link
Author

And yeah a second opinion would be better :)

@mcorb
Copy link
Contributor

mcorb commented Mar 19, 2016

Accepting a subset or URLs using existing entry points is confusing and could introduce correctness issues because this API is specified as accepting file paths.

(e.g. why don't the other functions in the API accept URLs, only this one? Why are only some kinds of URLs accepted? What if I really want to write a file that looks like a URL in some custom Android distribution, but this code prevents me from doing so?)

@jrichardlai
Copy link
Author

@mcorb word. Maybe I should make another method for that readFileFromURL? Since this library is already handling download of urls, it still makes sense to be part of it.

@grokbot
Copy link

grokbot commented May 31, 2016

I'm attempting to open pdfs downloaded by using Linking.openUrl and it fails to open if I use a file://path_to_file

Would this PR make that function? I'll check it out, but if you can confirm that would be great.

Edit: I suppose my thought that it was some kind of permissions issue was not correct. For anyone else looking for reason why files cannot be opened, this PR didn't solve not being able to open local files downloaded with RNFS via Linking. Everyone else, sorry for cluttering!
Bonus Edit: turns out using ExternalDirectoryPath as the target directory worked for using Linking.openUrl

@bnns
Copy link

bnns commented Jun 8, 2016

@grokbot Thanks for posting your finding about ExternalDirectoryPath - it just saved me a lot of time!

@Froelund
Copy link

This works for me. Thanks @jrichardlai
In my scenario I tried to read the output file from react-native-camera. It worked on ios, but failed on Android. This fixed the issue 👍

@matt-oakes
Copy link

This certainly fixes and issue with Android, but I agree that it should be implement on all methods so the support is consistent.

@jjzazuet
Copy link

Hi. Just pinging to check if any decision has been made on this PR. Thanks!

@@ -75,15 +123,13 @@ public void writeFile(String filepath, String base64Content, ReadableMap options
@ReactMethod
public void readFile(String filepath, Callback callback) {
try {
File file = new File(filepath);

if (!file.exists()) throw new Exception("File does not exist");
Copy link
Owner

@itinance itinance Jul 18, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add please again the File-Exists-check and throw an Exception, if the file does not exists? To not throw an exception would be compatibility break for all those that rely on this check in the thrown exception (instead of calling .exists() by themself in JS-layer)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would also recommend not adding file:// without checking if the filepath already starts with file:// to avoid double file://file://

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It actually doesn't start with file:// since the uri scheme is null

@itinance
Copy link
Owner

Thanks for reaching out. Just one thing. Please consider my comment

@TomMahle
Copy link

TomMahle commented Jun 7, 2018

@grokbot Could you elaborate on this? I'm using:
Linking.openURL('file://' + RNFS.ExternalDirectoryPath + '/' + fileName)
but the file is still not being opened even though Linking.canOpenURL is giving back true and PermissionsAndroid.PERMISSIONS.READ_EXTERNAL_STORAGE is giving granted. Any ideas what I might still be missing?

[Edit]: It looks like the issue I was running into was the "...exposed beyond app through Intent.getData()" issue. I haven't found a way to get around this using Linking, but am awaiting this PR to get fixed before using on react-native-fetch-blob to get merged in in order to use it: joltup/rn-fetch-blob#58

@grokbot
Copy link

grokbot commented Jun 7, 2018

@TomMahle in my case when I was working with this, I had previously downloaded the file and stored it in the default directory through the app, where I did run into some read problems when using Linking.openURL(). Changing the directory to ExternalDirectoryPath had cleared the initial problem of throwing errors of not having permission to open that file. If you're having permission issues, I think the app has to have same user as the target directory or more open permissions (which I think ExternalDirectoryPath is the latter)

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.