From b748566f34b074f3c61e9046d3cc0e0d4bbd7ae4 Mon Sep 17 00:00:00 2001 From: Niels Andriesse Date: Thu, 17 Oct 2019 12:50:17 +1100 Subject: [PATCH] Implement attachment downloading --- .../Attachments/OWSAttachmentDownloads.m | 73 ++++--------------- .../Attachments/TSAttachmentPointer.m | 3 + 2 files changed, 16 insertions(+), 60 deletions(-) diff --git a/SignalServiceKit/src/Messages/Attachments/OWSAttachmentDownloads.m b/SignalServiceKit/src/Messages/Attachments/OWSAttachmentDownloads.m index 4ca391c61..52339c018 100644 --- a/SignalServiceKit/src/Messages/Attachments/OWSAttachmentDownloads.m +++ b/SignalServiceKit/src/Messages/Attachments/OWSAttachmentDownloads.m @@ -370,66 +370,19 @@ typedef void (^AttachmentDownloadFailure)(NSError *error); }); }; - if (attachmentPointer.serverId < 100) { - OWSLogError(@"Suspicious attachment id: %llu", (unsigned long long)attachmentPointer.serverId); - } - TSRequest *request = [OWSRequestFactory attachmentRequestWithAttachmentId:attachmentPointer.serverId]; - - [self.networkManager makeRequest:request - success:^(NSURLSessionDataTask *task, id responseObject) { - if (![responseObject isKindOfClass:[NSDictionary class]]) { - OWSLogError(@"Failed retrieval of attachment. Response had unexpected format."); - NSError *error = OWSErrorMakeUnableToProcessServerResponseError(); - return markAndHandleFailure(error); - } - NSString *location = [(NSDictionary *)responseObject objectForKey:@"location"]; - if (!location) { - OWSLogError(@"Failed retrieval of attachment. Response had no location."); - NSError *error = OWSErrorMakeUnableToProcessServerResponseError(); - return markAndHandleFailure(error); - } - - dispatch_async([OWSDispatch attachmentsQueue], ^{ - [self downloadFromLocation:location - job:job - success:^(NSString *encryptedDataFilePath) { - [self decryptAttachmentPath:encryptedDataFilePath - attachmentPointer:attachmentPointer - success:markAndHandleSuccess - failure:markAndHandleFailure]; - } - failure:^(NSURLSessionTask *_Nullable task, NSError *error) { - if (attachmentPointer.serverId < 100) { - // This looks like the symptom of the "frequent 404 - // downloading attachments with low server ids". - NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)task.response; - NSInteger statusCode = [httpResponse statusCode]; - OWSFailDebug(@"%d Failure with suspicious attachment id: %llu, %@", - (int)statusCode, - (unsigned long long)attachmentPointer.serverId, - error); - } - markAndHandleFailure(error); - }]; - }); - } - failure:^(NSURLSessionDataTask *task, NSError *error) { - if (!IsNSErrorNetworkFailure(error)) { - OWSProdError([OWSAnalyticsEvents errorAttachmentRequestFailed]); - } - OWSLogError(@"Failed retrieval of attachment with error: %@", error); - if (attachmentPointer.serverId < 100) { - // This _shouldn't_ be the symptom of the "frequent 404 - // downloading attachments with low server ids". - NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)task.response; - NSInteger statusCode = [httpResponse statusCode]; - OWSFailDebug(@"%d Failure with suspicious attachment id: %llu, %@", - (int)statusCode, - (unsigned long long)attachmentPointer.serverId, - error); + dispatch_async([OWSDispatch attachmentsQueue], ^{ + [self downloadFromLocation:attachmentPointer.downloadURL + job:job + success:^(NSString *encryptedDataFilePath) { + [self decryptAttachmentPath:encryptedDataFilePath + attachmentPointer:attachmentPointer + success:markAndHandleSuccess + failure:markAndHandleFailure]; } - return markAndHandleFailure(error); - }]; + failure:^(NSURLSessionTask *_Nullable task, NSError *error) { + markAndHandleFailure(error); + }]; + }); } - (void)decryptAttachmentPath:(NSString *)encryptedDataFilePath @@ -530,7 +483,7 @@ typedef void (^AttachmentDownloadFailure)(NSError *error); manager.completionQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); // We want to avoid large downloads from a compromised or buggy service. - const long kMaxDownloadSize = 150 * 1024 * 1024; + const long kMaxDownloadSize = 12 * 1024 * 1024; __block BOOL hasCheckedContentLength = NO; NSString *tempFilePath = diff --git a/SignalServiceKit/src/Messages/Attachments/TSAttachmentPointer.m b/SignalServiceKit/src/Messages/Attachments/TSAttachmentPointer.m index 0e04b52bd..58186c8d7 100644 --- a/SignalServiceKit/src/Messages/Attachments/TSAttachmentPointer.m +++ b/SignalServiceKit/src/Messages/Attachments/TSAttachmentPointer.m @@ -167,6 +167,9 @@ NS_ASSUME_NONNULL_BEGIN albumMessageId:albumMessageId attachmentType:attachmentType mediaSize:mediaSize]; + + pointer.downloadURL = attachmentProto.url; // Loki + return pointer; }