react-native-fetch-blob icon indicating copy to clipboard operation
react-native-fetch-blob copied to clipboard

Getting error "File ... couldn’t be opened because there is no such file" for exising file

Open laurent22 opened this issue 6 years ago • 4 comments

I'm using rn-fetch-blob 0.10.8 with RN 0.49 on iOS with iPhone 6 Simulator.

I'm using the react-native-image-resizer module to resize an image and then I use react-native-fetch-blob to move that image to the right location. However, when doing so I'm getting the error:

The file “6492238E-AAAC-4DC6-90F3-AFAB7225DBD5.jpg” couldn’t be opened because there is no such file.

Before doing the copy, I'm printing the source and destination to the console:

"Moving file:///Users/laurent/Library/Developer/CoreSimulator/Devices/3AF6C788-B6ED-41DD-85F0-32D719DB0DBE/data/Containers/Data/Application/A401D341-9C60-4AA3-8D8F-69207A8C9454/Library/Caches/6492238E-AAAC-4DC6-90F3-AFAB7225DBD5.jpg => /Users/laurent/Library/Developer/CoreSimulator/Devices/3AF6C788-B6ED-41DD-85F0-32D719DB0DBE/data/Containers/Data/Application/A401D341-9C60-4AA3-8D8F-69207A8C9454/Documents/testing.jpg"

I can verify that the path //Users/laurent/Library/Developer/CoreSimulator/Devices/3AF6C788-B6ED-41DD-85F0-32D719DB0DBE/data/Containers/Data/Application/A401D341-9C60-4AA3-8D8F-69207A8C9454/Library/Caches/6492238E-AAAC-4DC6-90F3-AFAB7225DBD5.jpg exist as I can open it in Finder. However for some reason rn-fetch-blob doesn't find it.

Any idea what could be the issue? Is that a bug in rn-fetch-blob or am I missing something?

Edit

For information this the code I'm using:

const resizedImage = await ImageResizer.createResizedImage(localFilePath, dimensions.width, dimensions.height, format, 85);
const resizedImagePath = resizedImage.uri;
console.info('Moving ' + resizedImagePath + ' => ' + targetPath);
await RNFetchBlob.fs.cp(resizedImagePath, targetPath); // Throws error

laurent22 avatar Nov 19 '17 19:11 laurent22

I'm seeing the same issue, also when calling stat on the file it fails with the same file not found error. Any ideas?

gvillenave avatar Dec 22 '17 19:12 gvillenave

In my case I've simply switched to https://github.com/itinance/react-native-fs which works fine.

laurent22 avatar Dec 22 '17 19:12 laurent22

I actually just managed to solve my issue by following these instructions: https://github.com/wkh237/react-native-fetch-blob/wiki/File-System-Access-API#readfilepath-encodingpromise

NOTICE: On iOS platform the directory path will be changed every time you access to the file system. So if you need read file on iOS, you need to get dir path first and concat file name with it.

// fileUri is a string like "file:///var/mobile/Containers/Data/Application/9B754FAA-2588-4FEC-B0F7-6D890B7B4681/Documents/filename" if (Platform.OS === 'ios') { let arr = fileUri.split('/') const dirs = RNFetchBlob.fs.dirs filePath = ${dirs.DocumentDir}/${arr[arr.length - 1]} } else { filePath = audioDataUri }

gvillenave avatar Dec 22 '17 20:12 gvillenave

I am using react-native-image-resizer to first resize the image.

the initial path is: file:///Users/nicoara/Library/Developer/CoreSimulator/Devices/CE689F2D-2A45-48F9-9F47-7A4A18DAC588/data/Containers/Data/Application/7BF23562-BF62-4870-92F9-FD72A1E92FFE/Library/Caches/B540B334-768C-435B-BFB7-988B00687338.jpg

My solution was to replace the initial 'file:///Users' with '/Users'.

(The above solution with https://github.com/wkh237/react-native-fetch-blob/wiki/File-System-Access-API#readfilepath-encodingpromise did not work, since the converted path is: /Users/nicoara/Library/Developer/CoreSimulator/Devices/CE689F2D-2A45-48F9-9F47-7A4A18DAC588/data/Containers/Data/Application/7BF23562-BF62-4870-92F9-FD72A1E92FFE/Documents/B540B334-768C-435B-BFB7-988B00687338.jpg and the path does not exist, because: there is an images folder inside the documents folder, but it always has another hash name for the image (here 1A1.. instead of B54..) /Users/nicoara/Library/Developer/CoreSimulator/Devices/CE689F2D-2A45-48F9-9F47-7A4A18DAC588/data/Containers/Data/Application/7BF23562-BF62-4870-92F9-FD72A1E92FFE/Documents/images/1A1D6CBF-504F-4C80-8E04-1A011A404246.jpg' )

nicoara avatar Jun 07 '18 07:06 nicoara