kesha-antonov / react-native-background-downloader

About A library for React-Native to help you download large files on iOS and Android both in the foreground and most importantly in the background.
https://www.npmjs.com/package/@kesha-antonov/react-native-background-downloader
Other
26 stars 3 forks source link

fixed critical issue in copying large files #7

Open l3utterfly opened 2 months ago

l3utterfly commented 2 months ago

Critical issue in android for copying large files: previous version downloads the large file but does not copy anything over 2GB.

Sorry! This PR fixes it.

kesha-antonov commented 2 months ago

Is it working on android 7.0?

kesha-antonov commented 2 months ago

https://developer.android.com/reference/java/nio/file/package-summary

Added in API level 26 java.nio.file

I previously added this API but changed to FileChannel because we need to support android 7.0+ (api 24+)

l3utterfly commented 2 months ago

Hmm.. my app has min version 26 so this was not a problem.

How about this code?

private static void moveFile(File src, File dst) throws IOException {
    FileChannel inChannel = new FileInputStream(src).getChannel();
    FileChannel outChannel = new FileOutputStream(dst).getChannel();
    try {
        long bytesTransferred = 0;
        long totalBytes = inChannel.size();
        while (bytesTransferred < totalBytes) {
            long remainingBytes = totalBytes - bytesTransferred;
            long chunkSize = Math.min(remainingBytes, Integer.MAX_VALUE);
            long transferredBytes = inChannel.transferTo(bytesTransferred, chunkSize, outChannel);
            bytesTransferred += transferredBytes;
        }
        src.delete();
    } finally {
        if (inChannel != null)
            inChannel.close();
        if (outChannel != null)
            outChannel.close();
    }
}

(untested, but should work). The main problem with the previous code is that transferTo takes an Int, which cannot go past 2GB

kesha-antonov commented 2 months ago

Could you test in android emulator with api 24?

I can do it also but on Wednesday.

Prashant13Pawar commented 3 weeks ago

Not able to download files above 1GB . Please provide fix for this

l3utterfly commented 3 weeks ago

Did you try the code in the PR? I tested it and it works for files over 4GB with no problems

Prashant13Pawar commented 3 weeks ago

I tried code from PR still same issue

l3utterfly commented 3 weeks ago

Hmm.. not sure, will take a look later. The code works fine, and it is already in production, so not sure what's wrong with your version