react-native-documents / document-picker

Document Picker for React Native
https://react-native-documents.github.io/
MIT License
1.35k stars 437 forks source link

Fix: `fileCopyUri` should include the URI schema #527

Closed kidroca closed 2 years ago

kidroca commented 2 years ago

Summary

Make the fileCopyUri be a URI string and include the schema prefix (usually file:/)

A URI should begin with the schema prefix In iOS fileCopyUri already works that way Missing the schema prefix causes a problem when you try to use fileCopyUri with fetch to upload a file

Why .toURI().toString()

This was the only method I've found that returned the schema as part of the string The same thing is used in RN image picker here

Difference between file:/ and file:///

Since on iOS fileCopyUri is returned with a file:/// prefix - you might be wondering if this is a problem. According to wikipedia file:/ and file:/// are the same thing: https://en.wikipedia.org/wiki/File_URI_scheme#How_many_slashes.3F

Test Plan

I've manually tested the changes in the example app and in an external app

Before (no file:/ prefix for fileCopyUri)

RN_Document_Picker_Before

 LOG  [
  {
    "size": 3028,
    "fileCopyUri": "/data/user/0/com.example.reactnativedocumentpicker/cache/7246f3d1-815b-45fa-86ce-a420da059da4/sample.pdf",
    "name": "sample.pdf",
    "type": "application/pdf",
    "uri": "content://com.android.providers.downloads.documents/document/18"
  }
]

After

RN_Document_Picker_After

 LOG  [
  {
    "size": 3028,
    "fileCopyUri": "file:/data/user/0/com.example.reactnativedocumentpicker/cache/d87a83fe-e393-40f2-9f5f-f3ebe2641974/sample.pdf",
    "name": "sample.pdf",
    "type": "application/pdf",
    "uri": "content://com.android.providers.downloads.documents/document/18"
  }
]

Sample video (after) in our app:

https://user-images.githubusercontent.com/12156624/150010016-9e50f735-e0ae-40d6-8327-db95981e1f29.mp4

Sample for the same in iOS

No iOS change, but just showing how it already works there: RN_Document_Picker_iOS

What's required for testing (prerequisites)?

This can be manually tested in the example app

What are the steps to reproduce (after prerequisites)?

  1. Use the example app on Android
  2. Use the "Open Picker For Single File Selection" button (Or any button that passes a copyTo Document Picker Option)
  3. Select a file
  4. See the displayed fileCopyUri - it should contain the URI schema in the beginning e.g. file:/

Compatibility

OS Implemented
iOS
Android

Checklist

vonovak commented 2 years ago

thank you for the PR and the comprehensive description! :)