fixed merge conflict

This commit is contained in:
Hagen Hübel 2017-12-10 14:50:32 +01:00
commit 35b9077bce
2 changed files with 49 additions and 47 deletions

View File

@ -32,12 +32,12 @@
NSURL* url = [NSURL URLWithString:_params.fromUrl]; NSURL* url = [NSURL URLWithString:_params.fromUrl];
[[NSFileManager defaultManager] createFileAtPath:_params.toFile contents:nil attributes:nil]; if ([[NSFileManager defaultManager] fileExistsAtPath:_params.toFile]) {
_fileHandle = [NSFileHandle fileHandleForWritingAtPath:_params.toFile]; _fileHandle = [NSFileHandle fileHandleForWritingAtPath:_params.toFile];
if (!_fileHandle) { if (!_fileHandle) {
NSError* error = [NSError errorWithDomain:@"Downloader" code:NSURLErrorFileDoesNotExist NSError* error = [NSError errorWithDomain:@"Downloader" code:NSURLErrorFileDoesNotExist
userInfo:@{NSLocalizedDescriptionKey: [NSString stringWithFormat: @"Failed to create target file at path: %@", _params.toFile]}]; userInfo:@{NSLocalizedDescriptionKey: [NSString stringWithFormat: @"Failed to write target file at path: %@", _params.toFile]}];
_params.errorCallback(error); _params.errorCallback(error);
return nil; return nil;
@ -103,8 +103,10 @@
NSURL *destURL = [NSURL fileURLWithPath:_params.toFile]; NSURL *destURL = [NSURL fileURLWithPath:_params.toFile];
NSFileManager *fm = [NSFileManager defaultManager]; NSFileManager *fm = [NSFileManager defaultManager];
NSError *error = nil; NSError *error = nil;
[fm removeItemAtURL:destURL error:nil]; // Remove file at destination path, if it exists if([_statusCode integerValue] >= 200 && [_statusCode integerValue] < 300) {
[fm moveItemAtURL:location toURL:destURL error:&error]; [fm removeItemAtURL:destURL error:nil]; // Remove file at destination path, if it exists
[fm moveItemAtURL:location toURL:destURL error:&error];
}
if (error) { if (error) {
NSLog(@"RNFS download: unable to move tempfile to destination. %@, %@", error, error.userInfo); NSLog(@"RNFS download: unable to move tempfile to destination. %@, %@", error, error.userInfo);
} }

View File

@ -87,53 +87,53 @@ public class Downloader extends AsyncTask<DownloadParams, int[], DownloadResult>
statusCode = connection.getResponseCode(); statusCode = connection.getResponseCode();
lengthOfFile = connection.getContentLength(); lengthOfFile = connection.getContentLength();
} }
if(statusCode >= 200 && statusCode < 300) {
Map<String, List<String>> headers = connection.getHeaderFields();
Map<String, List<String>> headers = connection.getHeaderFields(); Map<String, String> headersFlat = new HashMap<String, String>();
Map<String, String> headersFlat = new HashMap<String, String>(); for (Map.Entry<String, List<String>> entry : headers.entrySet()) {
String headerKey = entry.getKey();
String valueKey = entry.getValue().get(0);
for (Map.Entry<String, List<String>> entry : headers.entrySet()) { if (headerKey != null && valueKey != null) {
String headerKey = entry.getKey(); headersFlat.put(headerKey, valueKey);
String valueKey = entry.getValue().get(0);
if (headerKey != null && valueKey != null) {
headersFlat.put(headerKey, valueKey);
}
}
mParam.onDownloadBegin.onDownloadBegin(statusCode, lengthOfFile, headersFlat);
input = new BufferedInputStream(connection.getInputStream(), 8 * 1024);
output = new FileOutputStream(param.dest);
byte data[] = new byte[8 * 1024];
int total = 0;
int count;
double lastProgressValue = 0;
while ((count = input.read(data)) != -1) {
if (mAbort.get()) throw new Exception("Download has been aborted");
total += count;
if (param.progressDivider <= 0) {
publishProgress(new int[]{lengthOfFile, total});
} else {
double progress = Math.round(((double) total * 100) / lengthOfFile);
if (progress % param.progressDivider == 0) {
if ((progress != lastProgressValue) || (total == lengthOfFile)) {
Log.d("Downloader", "EMIT: " + String.valueOf(progress) + ", TOTAL:" + String.valueOf(total));
lastProgressValue = progress;
publishProgress(new int[]{lengthOfFile, total});
}
} }
} }
output.write(data, 0, count);
mParam.onDownloadBegin.onDownloadBegin(statusCode, lengthOfFile, headersFlat);
input = new BufferedInputStream(connection.getInputStream(), 8 * 1024);
output = new FileOutputStream(param.dest);
byte data[] = new byte[8 * 1024];
int total = 0;
int count;
double lastProgressValue = 0;
while ((count = input.read(data)) != -1) {
if (mAbort.get()) throw new Exception("Download has been aborted");
total += count;
if (param.progressDivider <= 0) {
publishProgress(new int[]{lengthOfFile, total});
} else {
double progress = Math.round(((double) total * 100) / lengthOfFile);
if (progress % param.progressDivider == 0) {
if ((progress != lastProgressValue) || (total == lengthOfFile)) {
Log.d("Downloader", "EMIT: " + String.valueOf(progress) + ", TOTAL:" + String.valueOf(total));
lastProgressValue = progress;
publishProgress(new int[]{lengthOfFile, total});
}
}
}
output.write(data, 0, count);
}
output.flush();
res.bytesWritten = total;
} }
output.flush();
res.statusCode = statusCode; res.statusCode = statusCode;
res.bytesWritten = total;
} finally { } finally {
if (output != null) output.close(); if (output != null) output.close();
if (input != null) input.close(); if (input != null) input.close();