Skip to content
This repository was archived by the owner on Mar 16, 2019. It is now read-only.

Crash in [RNFetchBlobFS readFile] on iOS #266

Closed
cosmith opened this issue Feb 15, 2017 · 11 comments
Closed

Crash in [RNFetchBlobFS readFile] on iOS #266

cosmith opened this issue Feb 15, 2017 · 11 comments

Comments

@cosmith
Copy link
Contributor

cosmith commented Feb 15, 2017

Hi, I'm getting a crash when trying to send a file that doesn't exist anymore.

RNFetchBlob version 0.10.3, RN version 0.38.0

This is the stack trace I get:

#3  0x000000010ffd5fb4 in +[RNFetchBlobFS readFile:encoding:resolver:rejecter:onComplete:] at [APP]/node_modules/react-native-fetch-blob/ios/RNFetchBlobFS.m:426
#4  0x000000010ffe49f0 in __59+[RNFetchBlobReqBuilder buildFormBody:boundary:onComplete:]_block_invoke at [APP]/node_modules/react-native-fetch-blob/ios/RNFetchBlobReqBuilder.m:205
#5  0x000000010ffe3e42 in +[RNFetchBlobReqBuilder buildFormBody:boundary:onComplete:] at [APP]/node_modules/react-native-fetch-blob/ios/RNFetchBlobReqBuilder.m:251
#6  0x000000010ffe1843 in __89+[RNFetchBlobReqBuilder buildMultipartRequest:taskId:method:url:headers:form:onComplete:]_block_invoke at [APP]/node_modules/react-native-fetch-blob/ios/RNFetchBlobReqBuilder.m:54

It seems like the readFile method is called here and here with the resolver and rejecter set to nil.

The crash happens here:

reject(@"RNFetchBlobFS readFile error", @"file not exists", nil);

                if(![[NSFileManager defaultManager] fileExistsAtPath:path]) {
                    reject(@"RNFetchBlobFS readFile error", @"file not exists", nil);
                    return;
                }

I don't know why the try/catch block in readFile doesn't catch the exception. I tried adding a if (reject != nil) condition before the call, which works, but then the call just fails silently...

Any thoughts?

@cosmith
Copy link
Contributor Author

cosmith commented Feb 15, 2017

BTW I'd be happy to send a PR if someone can point me in the right direction.

@wkh237
Copy link
Owner

wkh237 commented Feb 16, 2017

@cosmith , could you provide a sample that replicates this error? It'd be helpful for me to find a way to fix it 👍

@wkh237
Copy link
Owner

wkh237 commented Feb 16, 2017

@cosmith , perhaps we can add a return; below [this line] (

onComplete(fileContent);
) that looks reasonable to me because the function doesn't have to encode the file content when either resolver and rejector are nil (which means the call is from native context instead of JS).

__block Byte * buffer;
            if(asset != nil)
            {
                buffer = malloc(asset.size);
                [asset getBytes:buffer fromOffset:0 length:asset.size error:&err];
                if(err != nil)
                {
                    reject(@"RNFetchBlobFS readFile error", @"failed to read asset", [err localizedDescription]);
                    return;
                }
                fileContent = [NSData dataWithBytes:buffer length:asset.size];
                free(buffer);
            }
            else
            {
                if(![[NSFileManager defaultManager] fileExistsAtPath:path]) {

                    reject(@"RNFetchBlobFS readFile error", @"file not exists", nil);
                    return;
                }
                fileContent = [NSData dataWithContentsOfFile:path];

            }
            if(onComplete != nil)
+            {                                                                                                                                                      
+                onComplete(fileContent);                                                                                                    
+                return;                                                                                                                                                      
+            }                                                                                                                                                                                                        

@cosmith
Copy link
Contributor Author

cosmith commented Feb 16, 2017

This is the function we use:

    static async sendFile(method, path, fileUri, fieldName, additionalData, onProgress = () => {}) {
        let token = await Storage.getUserProperty("token");

        let headers = {
            "Authorization": `Token ${token}`,
            "Content-Type": "multipart/form-data",
        };

        let source = fileUri.replace("file://", "");
        let file = RNFetchBlob.wrap(source);

        let formData = [
            {name: fieldName, filename: fieldName + ".png", data: file},
        ];

        for (let key in additionalData) {
            formData.push({name: key, data: String(additionalData[key])});
        }

        this.onLoadChange(true);
        return RNFetchBlob.fetch(method, Constants.ServerUrl + path, headers, formData)
            .uploadProgress((written, total) => {
                onProgress(written, total);
            })
            .then((response) => {
                this.onLoadChange(false);
                if (response.respInfo.status < 200 || response.respInfo.status > 399) {
                    Log.warn("[Server]", "error loading", path, response.data);
                    this.onLoadChange(false);
                    throw response;
                }
                return response.json();
            }, (error) => {
                this.onLoadChange(false);
                Log.warn("[Server]", "error making request", path, error);
                throw error;
            });
    }

Calling this function with a file that doesn't exist crashes:

Server.sendFile("POST", "/test/", "i-dont-exist.png", "photo", {});

Adding a return where you suggested doesn't fix the issue since the crash happens on line 448 (reject(@"RNFetchBlobFS readFile error", @"file not exists", nil);).

wkh237 added a commit that referenced this issue Feb 16, 2017
@wkh237
Copy link
Owner

wkh237 commented Feb 16, 2017

@cosmith , I have committed a fix to issue-266 branch, could you try if that fix the issue?

$ npm install --save github:wkh237/react-native-fetch-blob-package#issue-266

@cosmith
Copy link
Contributor Author

cosmith commented Feb 16, 2017

Yes that fixes it! Thanks 👍

@jiazil
Copy link

jiazil commented Aug 21, 2017

npm install --save github:wkh237/react-native-fetch-blob-package#issue-266
error Couldn't find match for "issue-266" in "0.10.0,0.10.0-beta.1,0.10.0-beta.2,0.10.0-beta.3,0.10.0-beta.4,0.10.0-beta.5,0.10.0-beta.7,0.10.0-beta.8,0.10.1,0.10.1-beta.2,0.10.3-fix-263,0.10.3-fix-264,0.9.6,master,v0.9.6" for "https://github.com/wkh237/react-native-fetch-blob-package".
info Visit https://yarnpkg.com/en/docs/cli/install for documentation about this command.

@cosmith
Copy link
Contributor Author

cosmith commented Aug 21, 2017

@jiazil this was merged in master, use the latest version

@jiazil
Copy link

jiazil commented Aug 23, 2017

@cosmith
image
still the error

@NawazKhanMd
Copy link

Is this issue resolved Cuz am facing it Now

@nicoara
Copy link

nicoara commented Jun 7, 2018

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'
)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants