Clean up attachment downloads.

pull/1/head
Matthew Chen 6 years ago
parent 32f1ce9473
commit 2ea751bbac

@ -192,6 +192,10 @@ static const CGFloat kAttachmentDownloadProgressTheta = 0.001f;
pointer:attachment
success:markAndHandleSuccess
failure:markAndHandleFailure];
if (![OWSFileSystem deleteFile:encryptedDataFilePath]) {
OWSLogError(@"Could not delete temporary file.");
}
}
});
}
@ -295,9 +299,17 @@ static const CGFloat kAttachmentDownloadProgressTheta = 0.001f;
const long kMaxDownloadSize = 150 * 1024 * 1024;
__block BOOL hasCheckedContentLength = NO;
NSString *tempFilePath = [NSTemporaryDirectory() stringByAppendingPathComponent:[NSUUID UUID].UUIDString];
NSURL *tempFileURL = [NSURL fileURLWithPath:tempFilePath];
__block NSURLSessionDownloadTask *task;
void (^failureHandler)(NSError *) = ^(NSError *error) {
OWSLogError(@"Failed to download attachment with error: %@", error.description);
if (![OWSFileSystem deleteFileIfExists:tempFilePath]) {
OWSLogError(@"Could not delete temporary file.");
}
failureHandlerParam(task, error);
};
@ -308,13 +320,9 @@ static const CGFloat kAttachmentDownloadProgressTheta = 0.001f;
parameters:nil
error:&serializationError];
if (serializationError) {
failureHandler(serializationError);
return;
return failureHandler(serializationError);
}
NSString *tempFilePath = [NSTemporaryDirectory() stringByAppendingPathComponent:[NSUUID UUID].UUIDString];
NSURL *tempFileURL = [NSURL fileURLWithPath:tempFilePath];
task = [manager downloadTaskWithRequest:request
progress:^(NSProgress *progress) {
OWSAssertDebug(progress != nil);
@ -403,6 +411,15 @@ static const CGFloat kAttachmentDownloadProgressTheta = 0.001f;
OWSErrorCodeInvalidMessage, NSLocalizedString(@"ERROR_MESSAGE_INVALID_MESSAGE", @""));
return failureHandler(error);
}
// Protect the temporary file.
if (![OWSFileSystem ensureFileExists:filePath.path]) {
OWSLogError(@"Could not protect temporary file.");
NSError *error = OWSErrorWithCodeDescription(
OWSErrorCodeInvalidMessage, NSLocalizedString(@"ERROR_MESSAGE_INVALID_MESSAGE", @""));
return failureHandler(error);
}
NSNumber *_Nullable fileSize = [OWSFileSystem fileSizeOfPath:filePath.path];
if (!fileSize) {
OWSLogError(@"Could not determine attachment file size.");

Loading…
Cancel
Save