tus-js-client icon indicating copy to clipboard operation
tus-js-client copied to clipboard

Error: tus: cannot fetch `file.uri` as Blob, make sure the uri is correct and accessible and some time the video is uploaded to the server and i get the url

Open fazzysyed opened this issue 3 years ago • 12 comments

I am using this Library with React native I am uploading a video to Cloudflare and I am getting the video through react-native image picker. Some of the time the video gets uploaded and sometimes I get this error. this is the response Screenshot 2021-03-15 at 1 51 15 PM

this is my code :

  const options2 = {
      title: 'Select video',
      mediaType: 'video',
      path: 'video',
      durationLimit: 30,
      quality: 1,
      cameraType: 'front',

      takePhotoButtonTitle: 'Take a video',
    };

    ImagePicker.showImagePicker(options2, (response) => {
      console.log('Response = ', response);

      if (response.didCancel) {
        console.log('User cancelled image picker');
      } else if (response.error) {
        console.log('ImagePicker Error: ', response.error);
      } else {
        const file = {
          uri: response.uri,
          type: 'video',
        };
        const upload = new Upload(file, {
          endpoint:
            'https://api.cloudflare.com/client/v4/accounts/"Myaccount/stream',
          retryDelays: [0, 1000, 3000, 5000],
          metadata: {
            filename: `video.mp4`,
            filetype: 'mp4',
          },
          headers: {
            Authorization: 'Bearer token',
          },
          onError: (error) => {
            console.log(error);
          },
          onProgress: (uploadedBytes, totalBytes) => {
            console.log(uploadedBytes / totalBytes);
          },
          onSuccess: () => {
            console.log(upload.url);
          },
        });
        upload.start();
      }
    });

this the url i get someof the time https://api.cloudflare.com/client/v4/accounts/"Myaccount"/media/"MyAccount"?tusv2=true

Screenshot 2021-03-15 at 2 03 29 PM

Mostly get this. Screenshot 2021-03-15 at 2 07 50 PM

Setup details

  • Runtime environment:
  • Mac Os.
  • Node js v12.18.4 -"react-native": "0.63.3",
  • "tus-js-client": "^2.2.0"

fazzysyed avatar Mar 15 '21 09:03 fazzysyed

I assume your are developing for Android, is that correct?

I don't have a concrete idea but I noticed that you are using the showImagePicker method, which is removed in react-native-image-picker v3: https://github.com/react-native-image-picker/react-native-image-picker#migration-from-2xx-to-3xx Maybe upgrading react-native-image-picker and using the new API can help you here.

Acconut avatar Mar 16 '21 08:03 Acconut

I have the same issue in react-native: the files have uploaded successfully before, but intermittently I am getting this file.uri ... error. It seems to be happening here: https://github.com/tus/tus-js-client/blob/master/lib/browser/uriToBlob.js

I notice this file is in the /browser folder. I am on mobile, not browser - not sure if that matters.

lululeon avatar Mar 29 '21 18:03 lululeon

@lululeon Please share more details about your setup and try to narrow down in which situations the error occurs. That would help us a lot.

I notice this file is in the /browser folder. I am on mobile, not browser - not sure if that matters.

No, that's intended and all fine. We categorize React Native as a brower-like environment as it has many similarities to web browsers.

Acconut avatar Mar 29 '21 21:03 Acconut

@lululeon Please share more details about your setup and try to narrow down in which situations the error occurs. That would help us a lot.

You know what I found out, when I am running my react-native app in debug mode, it routes to react-native-debugger, and then the upload fails immediately with the file.uri error. But when I switch debugging off, the tus upload runs just fine! Is it possible that react-native-debugger confuses tus about the type of environment it's running in? eg in the openFile() call, might rndb handling traffic for the app cause confusion in this line:

if (isReactNative() && input && typeof input.uri !== 'undefined')

No, that's intended and all fine. We categorize React Native as a brower-like environment as it has many similarities to web browsers.

I think that makes sense, and might explain the above, actually. Thank you!

lululeon avatar Mar 30 '21 18:03 lululeon

Is it possible that react-native-debugger confuses tus about the type of environment it's running in?

Interesting theory, let's test it out! What is the value of navigator.product when you run your app inside the debugger? It should be ReactNative for our isReactNative function to work: https://github.com/tus/tus-js-client/blob/master/lib/browser/isReactNative.js#L3

This should be the case for all applications but maybe the debugger is interfering: https://github.com/facebook/react-native/blob/6e6443afd04a847ef23fb6254a84e48c70b45896/Libraries/Core/setUpNavigator.js#L21

If that does not work, please log what file object you are passing into the tus.Upload constructor and check if it has the uri property.

Acconut avatar Mar 30 '21 23:03 Acconut

Interesting theory, let's test it out! What is the value of navigator.product when you run your app inside the debugger? It should be ReactNative for our isReactNative function to work: https://github.com/tus/tus-js-client/blob/master/lib/browser/isReactNative.js#L3

In rndb the returned string directly on the console is "gecko". I can't access navigator in react-native; it appears undefined. Anyway the upload works, uri values are OK, as long as debugging is off.

lululeon avatar Mar 31 '21 00:03 lululeon

In rndb the returned string directly on the console is "gecko". I can't access navigator in react-native; it appears undefined. Anyway the upload works, uri values are OK, as long as debugging is off.

Oh, that's interesting. In this case, we should maybe switch the if statement to not test whether the environment is React Native, but whether the object has a uri property and looks like a React Native file object. Would you be interested in opening a PR for this?

Acconut avatar May 04 '21 11:05 Acconut

So is there a way to fix this ? For me it only happens on Android.

bogdandobai avatar Sep 17 '21 10:09 bogdandobai

@bogdandobai Currently this has not been fixed as I am not very experienced with React Native. That being said, would you be interested in working on a PR to implement the fix I suggested:

switch the if statement to not test whether the environment is React Native, but whether the object has a uri property and looks like a React Native file object

Acconut avatar Oct 13 '21 17:10 Acconut

Hi @fazzysyed!

try replacing response.uri with response.path and as I remember, you need to add files:// :

  if (response.didCancel) {
    console.log('User cancelled image picker');
  } else if (response.error) {
    console.log('ImagePicker Error: ', response.error);
  } else {
    const file = {
      uri: response.path, <==== replace 
      type: 'video',
    };

nikolaytsigvintsev avatar Nov 12 '21 12:11 nikolaytsigvintsev

Hi @fazzysyed! Add android:requestLegacyExternalStorage="true" inside <application> in AndroidManifest.xml and compileSdkVersion = 29 targetSdkVersion = 29 in build.gradle.

Hope this helps!

SandipJawaleRN avatar Nov 18 '21 12:11 SandipJawaleRN

@SandipJawaleRN https://developer.android.com/google/play/requirements/target-sdk

"Starting in November 2021, app updates will be required to target API level 30 or above and adjust for behavioral changes in Android 11."

majirosstefan avatar Dec 23 '21 21:12 majirosstefan