Merge branch 'zhantx-master'
* zhantx-master: handle 304 on android bugfix make sure response with error status code won't change downloaded file on iOS
This commit is contained in:
commit
d7efa212b2
16
Downloader.m
16
Downloader.m
|
@ -32,12 +32,12 @@
|
|||
|
||||
NSURL* url = [NSURL URLWithString:_params.fromUrl];
|
||||
|
||||
[[NSFileManager defaultManager] createFileAtPath:_params.toFile contents:nil attributes:nil];
|
||||
_fileHandle = [NSFileHandle fileHandleForWritingAtPath:_params.toFile];
|
||||
if ([[NSFileManager defaultManager] fileExistsAtPath:_params.toFile]) {
|
||||
_fileHandle = [NSFileHandle fileHandleForWritingAtPath:_params.toFile];
|
||||
|
||||
if (!_fileHandle) {
|
||||
NSError* error = [NSError errorWithDomain:@"Downloader" code:NSURLErrorFileDoesNotExist
|
||||
userInfo:@{NSLocalizedDescriptionKey: [NSString stringWithFormat: @"Failed to create target file at path: %@", _params.toFile]}];
|
||||
if (!_fileHandle) {
|
||||
NSError* error = [NSError errorWithDomain:@"Downloader" code:NSURLErrorFileDoesNotExist
|
||||
userInfo:@{NSLocalizedDescriptionKey: [NSString stringWithFormat: @"Failed to write target file at path: %@", _params.toFile]}];
|
||||
|
||||
_params.errorCallback(error);
|
||||
return nil;
|
||||
|
@ -103,8 +103,10 @@
|
|||
NSURL *destURL = [NSURL fileURLWithPath:_params.toFile];
|
||||
NSFileManager *fm = [NSFileManager defaultManager];
|
||||
NSError *error = nil;
|
||||
[fm removeItemAtURL:destURL error:nil]; // Remove file at destination path, if it exists
|
||||
[fm moveItemAtURL:location toURL:destURL error:&error];
|
||||
if([_statusCode integerValue] >= 200 && [_statusCode integerValue] < 300) {
|
||||
[fm removeItemAtURL:destURL error:nil]; // Remove file at destination path, if it exists
|
||||
[fm moveItemAtURL:location toURL:destURL error:&error];
|
||||
}
|
||||
if (error) {
|
||||
NSLog(@"RNFS download: unable to move tempfile to destination. %@, %@", error, error.userInfo);
|
||||
}
|
||||
|
|
|
@ -87,53 +87,53 @@ public class Downloader extends AsyncTask<DownloadParams, int[], DownloadResult>
|
|||
statusCode = connection.getResponseCode();
|
||||
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()) {
|
||||
String headerKey = entry.getKey();
|
||||
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});
|
||||
}
|
||||
if (headerKey != null && valueKey != null) {
|
||||
headersFlat.put(headerKey, valueKey);
|
||||
}
|
||||
}
|
||||
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.bytesWritten = total;
|
||||
} finally {
|
||||
if (output != null) output.close();
|
||||
if (input != null) input.close();
|
||||
|
|
Loading…
Reference in New Issue