itinance / react-native-fs

Native filesystem access for react-native
MIT License
4.93k stars 971 forks source link

Replace rn-fetch-blob with this library fail on writeFile() #817

Open vvusts opened 4 years ago

vvusts commented 4 years ago

I take photo and it is kept on this path temporary: /Users/user/Library/Developer/CoreSimulator/Devices/1A0F7397-23AE-4631-A422-ECB1E71906AC/data/Containers/Data/Application/7F903709-BC0B-42C9-B424-190D7970B528/tmp/ReactNative/73A573A573A573A573A520B93.jpg Then I use (this work): RNFetchBlob.fs.writeFile(destination, this.state.res, "uri") When I try to do the same with this library (doesn't work): RNFS.writeFile(destination, this.state.previewSource.uri, "uri") or RNFS.writeFile(destination, this.state.previewSource.uri, "base64") It same file but file is corrupted doesn't work. Any idea?

SilverInfinity commented 4 years ago

I believe this may be related to the issue reported in https://github.com/itinance/react-native-fs/issues/700

We've noticed an issue in Android 10 where writeFile is not over-writing the original file correctly. if the original file was larger that the overwriting one, it does not truncate the extra bytes.

ex if the original file is 'aaaaaaaa' and the overwritting file is 'bbbb', you end up with a corrupted file: 'bbbbaaaa' instead of just 'bbbb'.

In your case, you may be able to patch this by deleting the file first then writing a new one: RNFS.unlink(destination).then(() => { RNFS.writeFile(destination, this.state.previewSource.uri, "uri")); }); But you would need to watch out for race conditions if you are re-writting to the same file multiple times very quickly