react-native-file-access icon indicating copy to clipboard operation
react-native-file-access copied to clipboard

[Q] Fetch

Open sayem314 opened this issue 3 years ago • 15 comments

Thanks for the library. I just found it today. I have few questions.

  • Does fetch support background downloads?
  • Is it possible to add pause/resume?
  • Any possibility to add support for download progress?

sayem314 avatar Apr 08 '21 18:04 sayem314

The focus of this library is filesystem actions. For more advanced upload/download control, consider https://github.com/edeckers/react-native-blob-courier, it focuses on network, ignoring filesystem, so would compliment this library well.

I have not used react-native-blob-courier myself (so cannot vouch for actual functionality), but from the documentation, appears to handle the features you are looking for.

alpha0010 avatar Apr 08 '21 21:04 alpha0010

Thank you for your response. I will check out the package you mentioned however I would still suggest adding a background download feature with pause and resume since you are providing a fetch option.

sayem314 avatar Apr 09 '21 06:04 sayem314

Hi @alpha0010, we've recently released https://github.com/georstat/react-native-image-cache, it would be nice to have resumable downloads like: https://github.com/wcandillon/react-native-expo-image-cache/blob/b81b99b01918558c7680595980d76003d507bc6f/src/CacheManager.ts#L29 in order to "improve" it.

We hope/believe that it will get lot of traction.

Thanks

efstathiosntonas avatar May 07 '21 08:05 efstathiosntonas

I looked a bit deeper into this.

Using the system managed downloader might take a bit of work, but should be reasonably straight forward implementation.

  • https://developer.android.com/reference/android/app/DownloadManager
  • https://developer.apple.com/documentation/foundation/url_loading_system/downloading_files_in_the_background

App managed pause/resume for downloads would be more complicated. Would need to manage HTTP range requests, and handle servers that do not understand/obey them. On the plus side, this may be possible to implement js/ts side (on top of the existing APIs in this library), so might only need to implement once. Although, native side would be a bit more performant.

Download progress events would need to be emitted from native code for higher resolution. I suppose rough events could be triggered from js if implemented range requests there.

Not currently a priority for me, though I hope to look into at some point. Of course, accepting pull requests if you wish to help.

alpha0010 avatar May 10 '21 15:05 alpha0010

Thanks for looking into it, I have no clue on native code so I can’t help on this part.

I think everything should be done on native side as you stated. How are the other react native file system libs are handling such cases? (expo fs too)

efstathiosntonas avatar May 10 '21 15:05 efstathiosntonas

Expo constructs headers on Android

  • https://github.com/expo/expo/blob/da133f953e946fa77ad06d5515f69a9462671805/packages/expo-file-system/android/src/main/java/expo/modules/filesystem/FileSystemModule.java#L891-L911
  • https://github.com/expo/expo/blob/da133f953e946fa77ad06d5515f69a9462671805/packages/expo-file-system/android/src/main/java/expo/modules/filesystem/FileSystemModule.java#L925-L928

And on iOS, there is apparently a convenient native call https://developer.apple.com/documentation/foundation/nsurlsession/1411598-downloadtaskwithresumedata

alpha0010 avatar May 10 '21 16:05 alpha0010

Added progress events in branch https://github.com/alpha0010/react-native-file-access/tree/download-progress . I plan to test a bit more before tagging a release.

alpha0010 avatar May 23 '21 03:05 alpha0010

Thanks @alpha0010, if you want we can test too with the caching lib in real apps.

efstathiosntonas avatar May 27 '21 19:05 efstathiosntonas

Thanks. Just published, but of course if you notice any bugs in your testing/use, open an issue here.

Download progress updates live in 1.7.0.

alpha0010 avatar May 27 '21 22:05 alpha0010

Unfortunately, react-native-blob-courier doesn't support passing body to fetchBlob at the moment and FileSystem.fetch doesn't support request cancel. So none of both suits our requirements — we need to pass body to fetch request and have an option to cancel it because big amount of data is transferred. Would be awesome if we could add missing options, guys.

ezze avatar Dec 15 '21 10:12 ezze

Thanks for your interest. To confirm, passing request body works for you (when using this library), but it is missing request cancel? Background architecture for supporting cancels is already there. I just ran out of time before wiring it in. Will try to get to that soon.

alpha0010 avatar Dec 15 '21 15:12 alpha0010

@alpha0010 Thanks for reply. Yes, I use your library to download blobs right now because it supports passing a body to request. And I use react-native-blob-courier to upload blobs due to it supports request cancel.

ezze avatar Dec 15 '21 16:12 ezze

Published a release; let me know how it works for you.

alpha0010 avatar Dec 16 '21 23:12 alpha0010

@alpha0010 Cool, it was very fast reaction, mate!

But I am sorry, can't test it this time. Already implemented a workaround with react-native-blob-courier suitable for me at the moment. Anyway, I am still using react-native-file-access to work with file system so I'll definitely check it later.

ezze avatar Dec 17 '21 14:12 ezze

Thanks for the library.

Any possibility to add support for copy progress ( FileSystem.cp)?

Nirav-12 avatar Apr 10 '23 11:04 Nirav-12