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

the downloaded partial file is override when resume breakpoint download #141

Closed
haibinyu opened this issue Sep 23, 2016 · 7 comments
Closed

Comments

@haibinyu
Copy link

haibinyu commented Sep 23, 2016

react native: 0.33.0
react-native-fetch-blob:0.9.5.

In my project, I need to implement file breakpoint download. When the file is download partially (for example 50%), I exit the app, and then open the app to continue the file download, the content already downloaded in the file is gone.

@haibinyu haibinyu changed the title the download partail file is override when resume breakpoint downloa the download partail file is override when resume breakpoint download Sep 23, 2016
@haibinyu haibinyu changed the title the download partail file is override when resume breakpoint download the download partial file is override when resume breakpoint download Sep 23, 2016
@haibinyu haibinyu changed the title the download partial file is override when resume breakpoint download the downloaded partial file is override when resume breakpoint download Sep 23, 2016
@wkh237
Copy link
Owner

wkh237 commented Sep 23, 2016

@haibinyu , thank you for the information, I've never considered about this scenario, but it'd be great if we can fix this, I'll try fix it in next release ( probably 0.9.6).

There will be a beta release once I fix it 👍

@wkh237
Copy link
Owner

wkh237 commented Sep 24, 2016

@kejinliang , @haibinyu , after doing some tests on both Android and IOS, I think they both acting the same at this moment.

The PR #139 changes the behavior so that the response data always appended to the file if it already exists, but I think this change may effect those who use it by intended for replace the existing file.

In my opinion, it's better to introduce an option for better backward compatibility.

On the other hand, there's also a workaround like this ..

const {fs, config} = RNFetchBlob

function getTemporaryPathForDownload(dest) {
    return dest + '.download'
}

function resumableDownload(dest) {

    let tmpPath = getTemporaryPathForDownload(dest)
    fs.exists(tmpPath)
    .then((ext) => {
        // if the tmp file exists, which means the previous 
        // download session was interrupted.
        // append data in content ${dest}.download to destination
        if(ext) {
            // specify encoding `uri` is the most performant way
            // to perform file-to-file data transfer, 
            // the whole process is done in native 
            return fs.appendFile(tmpPath, dest, 'uri')
           .then(() =>fs.stat(dest))
        } 
        else
            return Promise.resolve({ size : 0 })
    })
    // after the previous downloaded data moved to desitation (if there is)
    // we can start a new session which overwrites the ${dest}.download
    // the new seesion will use range request which ask for the remain 
    // part from server
    .then((stat) => {
        return RNFetchBlob
            .config({ path : tmpPath })
            .fetch('GET', 'http://example.com/large-file.zip', {
                Range : `bytes=${stat.size}-`
            }) 
    })
    // if the task completed, write the content to the true destination
    .then((res) => fs.appendFile(res.path(), dest, 'uri') )
    // remove tmp file ( file at ${dest}.download )
    .then(() => {
        return fs.unlink(tmpPath)
    })
    .then(() => {
        /* done !! */
    })

}

@haibinyu
Copy link
Author

I agree that it is better to add a config option to enable append file behavior.

@haibinyu
Copy link
Author

Thanks for the workround, 👍

wkh237 added a commit that referenced this issue Sep 25, 2016
@wkh237
Copy link
Owner

wkh237 commented Sep 26, 2016

@haibinyu , I've published a beta release to npm, please try upgrade you package.

In this version, you are able to append response data to existing file by append ?append=true to the file path

RNFetchBlob.config({
  path : pathOfFile + '?append=true'
})
.fetch('GET', 'http://example.com')

Please try if it works, thank you 😄

@wkh237 wkh237 added the beta label Sep 26, 2016
@haibinyu
Copy link
Author

@wkh237 👍 , I will try and let you know the result.

@wkh237 wkh237 removed the beta label Oct 1, 2016
@haibinyu
Copy link
Author

it works now.

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

2 participants