Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 85 additions & 0 deletions android/src/main/java/com/eko/RNBackgroundDownloaderModule.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.eko;

import android.annotation.SuppressLint;
import android.os.AsyncTask;
import android.util.Log;


import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactApplicationContext;
Expand Down Expand Up @@ -31,8 +33,10 @@

import org.jetbrains.annotations.NotNull;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
Expand All @@ -41,6 +45,8 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

import javax.annotation.Nullable;

Expand Down Expand Up @@ -343,6 +349,49 @@ public void onCompleted(Download download) {
params.putString("location", config.destination);

ee.emit("downloadComplete", params);
final String filePath = download.getFile();
final String taskId = config.id;
Log.d(getName(), "success callback!");

new Thread(new Runnable() {
@Override
public void run() {
try {
File downloadedZipFile = new File(filePath);
Log.d(getName(), "About to unzip: " + filePath + " (" + downloadedZipFile.exists() + ")");

File tempZipFile = new File(filePath + "_todelete");
if (tempZipFile.exists()) {
boolean deleteResult = tempZipFile.delete();

if (!deleteResult) {
throw new Exception("File not deleted");
}
}
boolean renameResult = downloadedZipFile.renameTo(tempZipFile);
if (!renameResult) {
throw new Exception("File not renamed");
}
unzip(tempZipFile, downloadedZipFile);
boolean deleteResult = tempZipFile.delete();

if (!deleteResult) {
Log.d(getName(), "Temp zip file not deleted");
}

WritableMap params = Arguments.createMap();
params.putString("id", taskId);
ee.emit("downloadComplete", params);
} catch(Exception e) {
e.printStackTrace();

WritableMap params = Arguments.createMap();
params.putString("id", taskId);
params.putInt("errorcode", -1);
ee.emit("downloadFailed", params);
}
}
}).start();
}

removeFromMaps(download.getId());
Expand Down Expand Up @@ -484,4 +533,40 @@ public void onDownloadBlockUpdated(Download download, DownloadBlock downloadBloc
@Override
public void onStarted(Download download, List<? extends DownloadBlock> list, int i) {
}


private void unzip(File zipFile, File targetDirectory) throws IOException {
ZipInputStream zis = new ZipInputStream(new BufferedInputStream(new FileInputStream(zipFile)));

try {
ZipEntry ze;
int count;
byte[] buffer = new byte[8192];

while ((ze = zis.getNextEntry()) != null) {
File file = new File(targetDirectory, ze.getName());
File dir = ze.isDirectory() ? file : file.getParentFile();

if (!dir.isDirectory() && !dir.mkdirs()) {
throw new FileNotFoundException("Failed to ensure directory: " + dir.getAbsolutePath());
}

if (ze.isDirectory()) {
continue;
}

FileOutputStream fout = new FileOutputStream(file);

try {
while ((count = zis.read(buffer)) != -1) {
fout.write(buffer, 0, count);
}
} finally {
fout.close();
}
}
} finally {
zis.close();
}
}
}
35 changes: 23 additions & 12 deletions ios/RNBackgroundDownloader.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
//
#import "RNBackgroundDownloader.h"
#import "RNBGDTaskConfig.h"
#import "SSZipArchive.h"

#define ID_TO_CONFIG_MAP_KEY @"com.eko.bgdownloadidmap"

Expand Down Expand Up @@ -324,18 +325,28 @@ - (void)URLSession:(nonnull NSURLSession *)session downloadTask:(nonnull NSURLSe
@synchronized (sharedLock) {
RNBGDTaskConfig *taskConfig = taskToConfigMap[@(downloadTask.taskIdentifier)];
if (taskConfig != nil) {
NSError *error = [self getServerError:downloadTask];
if (error == nil) {
[self saveDownloadedFile:taskConfig downloadURL:location error:&error];
}
if (self.bridge) {
if (error == nil) {
NSDictionary *responseHeaders = ((NSHTTPURLResponse *)downloadTask.response).allHeaderFields;
[self sendEventWithName:@"downloadComplete" body:@{@"id": taskConfig.id, @"headers": responseHeaders, @"location": taskConfig.destination}];
} else {
[self sendEventWithName:@"downloadFailed" body:@{@"id": taskConfig.id, @"error": [error localizedDescription]}];
// NSError *error = [self getServerError:downloadTask];
// if (error == nil) {
// [self saveDownloadedFile:taskConfig downloadURL:location error:&error];
// }
// if (self.bridge) {
// if (error == nil) {
// NSDictionary *responseHeaders = ((NSHTTPURLResponse *)downloadTask.response).allHeaderFields;
// [self sendEventWithName:@"downloadComplete" body:@{@"id": taskConfig.id, @"headers": responseHeaders, @"location": taskConfig.destination}];
// } else {
// [self sendEventWithName:@"downloadFailed" body:@{@"id": taskConfig.id, @"error": [error localizedDescription]}];
// }
// }
[SSZipArchive unzipFileAtPath:location.path toDestination:taskCofig.destination progressHandler:^(NSString *entry, unz_file_info zipInfo, long entryNumber, long total) {
} completionHandler:^(NSString *path, BOOL succeeded, NSError * _Nullable error) {
if (self.bridge) {
if (succeeded && error == nil) {
[self sendEventWithName:@"downloadComplete" body:@{@"id": taskCofig.id}];
} else {
[self sendEventWithName:@"downloadFailed" body:@{@"id": taskCofig.id, @"error": [error localizedDescription]}];
}
}
}
}];
[self removeTaskFromMap:downloadTask];
}
}
Expand Down Expand Up @@ -427,4 +438,4 @@ - (id)deserialize: (NSData *)data {
return [NSKeyedUnarchiver unarchiveObjectWithData:data];
}

@end
@end
1 change: 1 addition & 0 deletions react-native-background-downloader.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ Pod::Spec.new do |s|
s.requires_arc = true

s.dependency 'React-Core'
s.dependency 'SSZipArchive', '2.2.3'
end