GithubHelp home page GithubHelp logo

hossein-zare / react-native-chunk-upload Goto Github PK

View Code? Open in Web Editor NEW
55.0 2.0 10.0 76 KB

A package to bring Chunked File Upload / Resumable File Upload into React Native. Split a large file into multiple smaller pieces then upload them without worrying about network disconnection, even if it happens React Native Chunk Upload will only upload the failed chunk not the whole file!

License: MIT License

JavaScript 100.00%
upload upload-file chunk chunked-uploads chunk-upload resume resumable-upload resumable blob react-native

react-native-chunk-upload's Introduction

React Native Chunk Upload 2.x

React-Native-Chunk-Upload

A package to bring Chunked File Upload / Resumable File Upload into React Native. Split a large file into multiple smaller pieces then upload them without worrying about network disconnection, even if it happens React Native Chunk Upload will only upload the failed chunk not the whole file!

Changelog

In v1.x we had to first break the whole file into smaller pieces and then start uploading them.
But in v2.x this problem has been fixed. In addition, the speed of this process has increased 10 times.

.digIn(file instead of files, next*, retry*, unlink);

You may want to take a look at the Schema section.

Dependencies

⚠ Make sure the following packages are installed.

Installation

  • via NPM

    npm i react-native-chunk-upload
  • via Yarn

    yarn add react-native-chunk-upload

Basic Usage

import Axios from 'axios';
import ChunkUpload from 'react-native-chunk-upload';

const chunk = new ChunkUpload({
    path: response.path, // Path to the file
    size: 10095, // Chunk size (must be multiples of 3)
    fileName: response.fileName, // Original file name
    fileSize: response.size, // Original file size

    // Errors
    onFetchBlobError: (e) => console.log(e),
    onWriteFileError: (e) => console.log(e),
});

chunk.digIn(this.upload.bind(this));

upload(file, next, retry, unlink) {
    const body = new FormData();

    body.append('video', file.blob); // param name

    Axios.post('❌ URL HERE ❌', body, {
        headers: {
            "Content-Type": "multipart/form-data",
            "Accept": 'application/json',

            // 💥 Choose one of the following methods:

            // 1️⃣ If you're using the wester-chunk-upload php library...
            ...file.headers,

            // 2️⃣ Customize the headers
            "x-chunk-number": file.headers["x-chunk-number"],
            "x-chunk-total-number": file.headers["x-chunk-total-number"],
            "x-chunk-size": file.headers["x-chunk-size"],
            "x-file-name": file.headers["x-file-name"],
            "x-file-size": file.headers["x-file-size"],
            "x-file-identity": file.headers["x-file-identity"]
        }
    })
        .then(response => {
            switch (response.status) {
                // ✅ done
                case 200:

                    console.log(response.data);
                    
                break;

                // 🕗 still uploading...
                case 201:
                    console.log(`${response.data.progress}% uploaded...`);

                    next();
                break;
            }
        })
        .catch(error => {
            // ❌ waddafuk? 😟
            if (error.response) {
                if ([400, 404, 415, 500, 501].includes(error.response.status)) {
                    console.log(error.response.status, 'Failed to upload the chunk.');

                    unlink(file.path);
                } else if (error.response.status === 422) {
                    console.log('Validation Error', error.response.data);
                    
                    unlink(file.path);
                } else {
                    console.log('Re-uploading the chunk...');

                    retry();
                }
            } else {
                console.log('Re-uploading the chunk...');

                retry();
            }
        });
}

Wester Chunk Upload PHP Library

If you're going to use this library, you won't need much to do...

// easy peasy, right? 😁
headers: {
    "Content-Type": "multipart/form-data",
    "Accept": 'application/json',

    ...file.headers
}

Schema

chunk.digIn(
    (
        file: {
            path: string,
            headers: {
                "x-chunk-number": number,
                "x-chunk-total-number": number,
                "x-chunk-size": number,
                "x-file-name": string,
                "x-file-size": number,
                "x-file-identity": string
            },
            blob: {
                name: string,
                type: string,
                uri: string
            }
        },
        next: () => void,
        retry: () => void,
        unlink: (path: string) => void
    ): void
): void;

Support Us

Just star the repository, that's it! 😉

react-native-chunk-upload's People

Contributors

hossein-zare avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

react-native-chunk-upload's Issues

video from gallery not working

Hi,

Whenever I try selecting a video from gallery, I get "No file found error : onFetchBlobError"
But when I record a video & upload it works fine. Any idea on this

missing semicolon error

i use this package Basic Usage in my RN project and show this error : missing semicolon error in this line ==>

upload(file, next, retry, unlink) "missing semicolon error "{ ...

what i do??
helpe me plz

File does not exist at path : onFetchBlobError

Hey @hossein-zare

Thank you for the work here. I'm sorry but I've been looking into the other issues and pretty much googled everything I could on the subject but I keep getting the fetch blob error on iOS :

Error: File does not exist at path file://var/mobile/Containers/Data/Application/673C5AA2-C147-4FB8-B7D5-7A7F8E72BFAE/tmp/IMG_1975.mov

I've been trying with paths like :

  • file://var/mobile/Containers/...
  • file:///var/mobile/Containers/...
  • /var/mobile/Containers/...
  • var/mobile/Containers/...

Looks like the fetchblob module is automatically adding "file://"

I'm stuck here and can't make use of your lib. Do you have any idea of what I could do ? I've checked all the stackoverflow pieces and github issues.

Thank you very much 🙌🏼

Memory error when uploading larger videos

Hey guys, right now I'm able to upload smaller videos with no issues but when I try to upload a larger video ( >1GB ) the app crashes and says "Terminated due to memory issue".

Is there a way around this? I've tried a few different chunk sizes but that didn't seem to help either.

Any help would be appreciated, thanks.

I think should have custom field in Header in namespace ChunkUpload

in \index.d.ts

export interface Header {
        "x-chunk-number": number;
        "x-chunk-total-number": number;
        "x-chunk-size": number;
        "x-file-name": string;
        "x-file-size": number;
        "x-file-identity": string;
        "custom": Object
    }

in \src\index.js

constructor(props) {
        this.data = {
            path: String(props.path),
            size: parseInt(props.size),
            fileName: String(props.fileName),
            fileSize: parseInt(props.fileSize),
            fileIdentity: this.generateFileIdentity(),
            fileShortId: null,
            destinationPath: RNFS.TemporaryDirectoryPath,
            totalNumber: 0,
            custom: props.custom,
        };
getHeaders(index) {
        return {
            "x-chunk-number" : index,
            "x-chunk-total-number" : this.data.totalNumber,
            "x-chunk-size" : this.data.size,
            "x-file-name" : this.data.fileName,
            "x-file-size" : this.data.fileSize,
            "x-file-identity" : this.data.fileIdentity,
            "custom": this.data.custom
        }
    }

Error after running `npm run web`

react-native-chunk-upload\node_modules\react-native-fs\FS.common.js: Unexpected token, expected "," (30:29)

28 | };
29 |

30 | var normalizeFilePath = (path: string) => (path.startsWith('file://') ? path.slice(7) : path);
| ^
31 |
32 | type MkdirOptions = {
33 | NSURLIsExcludedFromBackupKey?: boolean; // iOS only

Using with MUX

Thanks again for the great work here @hossein-zare

I'm trying to use the lib to upload to MUX as direct upload but somehow I'm getting 400 bad request :
https://docs.mux.com/guides/video/upload-files-directly#if-you-dont-want-to-use-upchunk

I've added "content-length" and "content-range" headers as Mux is asking, but the catch block only sends back a text error saying "Error: Request failed with status code 400". Therefore I'm having trouble debugging because I don't know what I'm doing wrong not having the whole error object.

Here is my code :

    axios.put(muxUploadUrl, body, {
      headers: {
        "Content-Type": "multipart/form-data",
        "Accept": 'application/json',
        "Content-Length": file.headers["x-chunk-size"],
        "Content-Range": `bytes ${file.number - 1}-${file.headers["x-chunk-size"]}/${file.headers["x-file-size"]}`,
        // Customize the headers
        "x-chunk-number": file.headers["x-chunk-number"],
        "x-chunk-total-number": file.headers["x-chunk-total-number"],
        "x-chunk-size": file.headers["x-chunk-size"],
        "x-file-name": file.headers["x-file-name"],
        "x-file-size": file.headers["x-file-size"],
        "x-file-identity": file.headers["x-file-identity"]
      }
    })
      .then(response => {
        console.log('response : ', response)
      })
      .catch(error => {
        console.log( 'Upload error : ', error)
        ...
       
  Am I mistaking the headers configuration ? Should I split the file chunks in other values than the ones from the readme ?
  Have you ever tried the lib with Mux ?
  
  Thank you so much for your help 🙌🏼

Blob error = Error: File does not exist at path in iOS

Hello guys, Please help me regarding this. I am using this library to upload video to server. but in iOS getting this error in
onFetchBlobError: (e) => console.log(e),

Blob error = Error: File does not exist at path file:///....VIDEO_0010.mp4
at RNFetchBlobReadStream.js:47

I am doing this in my code:

const chunk = new ChunkUpload({
path: state.videoPath, // Path to the file
size: 4000, // Chunk size (must be multiples of 3)
fileName: state.videoFileName, // Original file name
fileSize: state.videoSize, // Original file size
onFetchBlobError: (e) => { console.log("Blob error = ", e) },
onWriteFileError: (e) => console.log("FileWrite error= ", e),
});
const upload = (file, next, retry, unlink) => {
const body = new FormData();
body.append('video', file.blob); // param name
Axios.post('my_server_url', body, {
headers: {
"Content-Type": "multipart/form-data",
"Accept": 'application/json',
Authorization: Bearer ${token},
'X-Requested-With': 'XMLHttpRequest',
"x-chunk-number": file.headers["x-chunk-number"],
"x-chunk-total-number": file.headers["x-chunk-total-number"],
"x-chunk-size": file.headers["x-chunk-size"],
"x-file-name": file.headers["x-file-name"],
"x-file-size": file.headers["x-file-size"] ? file.headers["x-file-size"] : state.videoSize,
"x-file-identity": file.headers["x-file-identity"]
}
})
.then(response => {
switch (response.status) {
case 200:
console.log("data = ", response.data);
break;
case 201:
console.log("progress = ", ${response.data.progress}% uploaded...);
next();
break;
}
})
.catch(error => {
console.log("error = ", error);
if (error.response) {
if ([400, 404, 415, 500, 501, 504].includes(error.response.status)) {
console.log(error.response, 'Failed to upload the chunk.');
unlink(file.path);
} else if (error.response.status === 422) {
console.log('Validation Error', error.response.data);
unlink(file.path);
} else {
console.log('Re-uploading the chunk...');
retry();
}
} else {
console.log('Re-uploading the chunk...');
retry();
}
});
}
chunk.digIn(upload.bind(this));

No such file

Hi
i have this error >

onFetchBlobError=> Error: No such file 'content://com.android.providers.media.documents/document/video%3A25'

i use react-native-document-picker package for get video file path
actully i use emulator and debug mode

sendVideo=()=>{
const file = this.state.videoFile
const chunk = new ChunkUpload({
path: file.uri, // Path to the file
size: 10095, // Chunk size (must be multiples of 3)
fileName: file.name, // Original file name
fileSize: file.size, // Original file size

   // Errors
   onFetchBlobError: (e) =>  console.log('onFetchBlobError=>', e), //this line
   onWriteFileError: (e) =>  console.log('onWriteFileError=>', e),
 });
 chunk.digIn(this.upload.bind(this));

}

and this is file state

fileCopyUri: "content://com.android.providers.media.documents/document/video%3A25"
name: "file_example_MP4_1920_18MG.mp4"
size: 17839845
type: "video/mp4"
uri: "content://com.android.providers.media.documents/document/video%3A25"

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.