mirror of
https://github.com/oxen-io/session-ios.git
synced 2023-12-13 21:30:14 +01:00
Delete unused OWSAttachmentDownloads
This commit is contained in:
parent
6b97f86c32
commit
862b4c0512
|
@ -478,7 +478,6 @@
|
|||
C33FDC78255A582000E217F9 /* TSConstants.m in Sources */ = {isa = PBXBuildFile; fileRef = C33FDABE255A580100E217F9 /* TSConstants.m */; };
|
||||
C33FDC7B255A582000E217F9 /* NSSet+Functional.m in Sources */ = {isa = PBXBuildFile; fileRef = C33FDAC1255A580100E217F9 /* NSSet+Functional.m */; };
|
||||
C33FDC7D255A582000E217F9 /* OWSDispatch.m in Sources */ = {isa = PBXBuildFile; fileRef = C33FDAC3255A580200E217F9 /* OWSDispatch.m */; };
|
||||
C33FDC89255A582000E217F9 /* OWSAttachmentDownloads.h in Headers */ = {isa = PBXBuildFile; fileRef = C33FDACF255A580300E217F9 /* OWSAttachmentDownloads.h */; settings = {ATTRIBUTES = (Public, ); }; };
|
||||
C33FDC95255A582000E217F9 /* OWSFailedMessagesJob.h in Headers */ = {isa = PBXBuildFile; fileRef = C33FDADB255A580400E217F9 /* OWSFailedMessagesJob.h */; settings = {ATTRIBUTES = (Public, ); }; };
|
||||
C33FDC96255A582000E217F9 /* NSObject+Casting.h in Headers */ = {isa = PBXBuildFile; fileRef = C33FDADC255A580400E217F9 /* NSObject+Casting.h */; settings = {ATTRIBUTES = (Public, ); }; };
|
||||
C33FDC98255A582000E217F9 /* SwiftSingletons.swift in Sources */ = {isa = PBXBuildFile; fileRef = C33FDADE255A580400E217F9 /* SwiftSingletons.swift */; };
|
||||
|
@ -516,7 +515,6 @@
|
|||
C33FDDC0255A582000E217F9 /* SignalAccount.m in Sources */ = {isa = PBXBuildFile; fileRef = C33FDC06255A581D00E217F9 /* SignalAccount.m */; };
|
||||
C33FDDC5255A582000E217F9 /* OWSError.m in Sources */ = {isa = PBXBuildFile; fileRef = C33FDC0B255A581D00E217F9 /* OWSError.m */; };
|
||||
C33FDDCC255A582000E217F9 /* TSConstants.h in Headers */ = {isa = PBXBuildFile; fileRef = C33FDC12255A581E00E217F9 /* TSConstants.h */; settings = {ATTRIBUTES = (Public, ); }; };
|
||||
C33FDDCD255A582000E217F9 /* OWSAttachmentDownloads.m in Sources */ = {isa = PBXBuildFile; fileRef = C33FDC13255A581E00E217F9 /* OWSAttachmentDownloads.m */; };
|
||||
C33FDDD0255A582000E217F9 /* FunctionalUtil.h in Headers */ = {isa = PBXBuildFile; fileRef = C33FDC16255A581E00E217F9 /* FunctionalUtil.h */; settings = {ATTRIBUTES = (Public, ); }; };
|
||||
C33FDDD3255A582000E217F9 /* OWSQueues.h in Headers */ = {isa = PBXBuildFile; fileRef = C33FDC19255A581F00E217F9 /* OWSQueues.h */; settings = {ATTRIBUTES = (Public, ); }; };
|
||||
C33FDEF8255A656D00E217F9 /* Promise+Delaying.swift in Sources */ = {isa = PBXBuildFile; fileRef = C3C2A5D32553860900C340D1 /* Promise+Delaying.swift */; };
|
||||
|
@ -1394,7 +1392,6 @@
|
|||
C33FDAC2255A580200E217F9 /* TSAttachment.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TSAttachment.m; sourceTree = "<group>"; };
|
||||
C33FDAC3255A580200E217F9 /* OWSDispatch.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = OWSDispatch.m; sourceTree = "<group>"; };
|
||||
C33FDAC4255A580200E217F9 /* TSAttachmentStream.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TSAttachmentStream.m; sourceTree = "<group>"; };
|
||||
C33FDACF255A580300E217F9 /* OWSAttachmentDownloads.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = OWSAttachmentDownloads.h; sourceTree = "<group>"; };
|
||||
C33FDAD3255A580300E217F9 /* TSThread.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TSThread.h; sourceTree = "<group>"; };
|
||||
C33FDAD5255A580300E217F9 /* TSQuotedMessage.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TSQuotedMessage.h; sourceTree = "<group>"; };
|
||||
C33FDAD9255A580300E217F9 /* OWSDisappearingMessagesConfiguration.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = OWSDisappearingMessagesConfiguration.h; sourceTree = "<group>"; };
|
||||
|
@ -1528,7 +1525,6 @@
|
|||
C33FDC0B255A581D00E217F9 /* OWSError.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = OWSError.m; sourceTree = "<group>"; };
|
||||
C33FDC0C255A581E00E217F9 /* TSInfoMessage.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TSInfoMessage.m; sourceTree = "<group>"; };
|
||||
C33FDC12255A581E00E217F9 /* TSConstants.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TSConstants.h; sourceTree = "<group>"; };
|
||||
C33FDC13255A581E00E217F9 /* OWSAttachmentDownloads.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = OWSAttachmentDownloads.m; sourceTree = "<group>"; };
|
||||
C33FDC15255A581E00E217F9 /* TSAttachment.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TSAttachment.h; sourceTree = "<group>"; };
|
||||
C33FDC16255A581E00E217F9 /* FunctionalUtil.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = FunctionalUtil.h; sourceTree = "<group>"; };
|
||||
C33FDC18255A581F00E217F9 /* TSAttachmentPointer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TSAttachmentPointer.h; sourceTree = "<group>"; };
|
||||
|
@ -3197,8 +3193,6 @@
|
|||
B8F5F52825EC4F8A003BF8D4 /* BlockListUIUtils.m */,
|
||||
C38EF2FC255B6DBD007E1867 /* ConversationStyle.swift */,
|
||||
C38EF3D4255B6DEE007E1867 /* DisappearingTimerConfigurationView.swift */,
|
||||
C33FDACF255A580300E217F9 /* OWSAttachmentDownloads.h */,
|
||||
C33FDC13255A581E00E217F9 /* OWSAttachmentDownloads.m */,
|
||||
C33FDA72255A57FA00E217F9 /* OWSFailedAttachmentDownloadsJob.h */,
|
||||
C33FDB59255A580E00E217F9 /* OWSFailedAttachmentDownloadsJob.m */,
|
||||
C33FDADB255A580400E217F9 /* OWSFailedMessagesJob.h */,
|
||||
|
@ -3696,7 +3690,6 @@
|
|||
B8C2B3442563782400551B4D /* ThreadUtil.h in Headers */,
|
||||
C38EF334255B6DBF007E1867 /* UIUtil.h in Headers */,
|
||||
C33FDD5B255A582000E217F9 /* OWSOperation.h in Headers */,
|
||||
C33FDC89255A582000E217F9 /* OWSAttachmentDownloads.h in Headers */,
|
||||
C38EF313255B6DBF007E1867 /* OWSUnreadIndicator.h in Headers */,
|
||||
C33FDD7C255A582000E217F9 /* SSKAsserts.h in Headers */,
|
||||
C33FDDA9255A582000E217F9 /* TSStorageKeys.h in Headers */,
|
||||
|
@ -4584,7 +4577,6 @@
|
|||
C33FDD49255A582000E217F9 /* ParamParser.swift in Sources */,
|
||||
C38EF35F255B6DCC007E1867 /* SelectRecipientViewController.m in Sources */,
|
||||
C38EF3C5255B6DE7007E1867 /* OWSViewController+ImageEditor.swift in Sources */,
|
||||
C33FDDCD255A582000E217F9 /* OWSAttachmentDownloads.m in Sources */,
|
||||
C38EF320255B6DBF007E1867 /* OWSScrubbingLogFormatter.m in Sources */,
|
||||
C38EF39B255B6DDA007E1867 /* ThreadViewModel.swift in Sources */,
|
||||
C38EF2A5255B6D93007E1867 /* Identicon+ObjC.swift in Sources */,
|
||||
|
|
|
@ -1,52 +0,0 @@
|
|||
//
|
||||
// Copyright (c) 2018 Open Whisper Systems. All rights reserved.
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
extern NSString *const kAttachmentDownloadProgressNotification;
|
||||
extern NSString *const kAttachmentDownloadProgressKey;
|
||||
extern NSString *const kAttachmentDownloadAttachmentIDKey;
|
||||
|
||||
@class SNProtoAttachmentPointer;
|
||||
@class TSAttachment;
|
||||
@class TSAttachmentPointer;
|
||||
@class TSAttachmentStream;
|
||||
@class TSMessage;
|
||||
@class YapDatabaseReadTransaction;
|
||||
@class YapDatabaseReadWriteTransaction;
|
||||
|
||||
#pragma mark -
|
||||
|
||||
/**
|
||||
* Given incoming attachment protos, determines which we support.
|
||||
* It can download those that we support and notifies threads when it receives unsupported attachments.
|
||||
*/
|
||||
@interface OWSAttachmentDownloads : NSObject
|
||||
|
||||
- (nullable NSNumber *)downloadProgressForAttachmentId:(NSString *)attachmentId;
|
||||
|
||||
// This will try to download all un-downloaded attachments for a given message.
|
||||
// Any attachments for the message which are already downloaded are skipped BUT
|
||||
// they are included in the success callback.
|
||||
//
|
||||
// success/failure are always called on a worker queue.
|
||||
- (void)downloadAttachmentsForMessage:(TSMessage *)message
|
||||
transaction:(YapDatabaseReadTransaction *)transaction
|
||||
success:(void (^)(NSArray<TSAttachmentStream *> *attachmentStreams))success
|
||||
failure:(void (^)(NSError *error))failure;
|
||||
|
||||
// This will try to download a single attachment.
|
||||
//
|
||||
// success/failure are always called on a worker queue.
|
||||
- (void)downloadAttachmentPointer:(TSAttachmentPointer *)attachmentPointer
|
||||
success:(void (^)(NSArray<TSAttachmentStream *> *attachmentStreams))success
|
||||
failure:(void (^)(NSError *error))failure;
|
||||
|
||||
- (void)continueDownloadIfPossible;
|
||||
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
|
@ -1,549 +0,0 @@
|
|||
//
|
||||
// Copyright (c) 2018 Open Whisper Systems. All rights reserved.
|
||||
//
|
||||
|
||||
#import "OWSAttachmentDownloads.h"
|
||||
#import "AppContext.h"
|
||||
#import "MIMETypeUtil.h"
|
||||
#import "NSNotificationCenter+OWS.h"
|
||||
#import "OWSBackgroundTask.h"
|
||||
#import "OWSDispatch.h"
|
||||
#import "OWSError.h"
|
||||
#import "OWSFileSystem.h"
|
||||
#import "OWSPrimaryStorage.h"
|
||||
|
||||
#import "SSKEnvironment.h"
|
||||
#import "TSAttachmentPointer.h"
|
||||
#import "TSAttachmentStream.h"
|
||||
#import "TSGroupModel.h"
|
||||
#import "TSGroupThread.h"
|
||||
#import "TSInfoMessage.h"
|
||||
#import "TSMessage.h"
|
||||
|
||||
#import "TSThread.h"
|
||||
#import <PromiseKit/AnyPromise.h>
|
||||
#import <SignalCoreKit/Cryptography.h>
|
||||
#import <SignalUtilitiesKit/SignalUtilitiesKit-Swift.h>
|
||||
#import <YapDatabase/YapDatabaseConnection.h>
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
NSString *const kAttachmentDownloadProgressNotification = @"kAttachmentDownloadProgressNotification";
|
||||
NSString *const kAttachmentDownloadProgressKey = @"kAttachmentDownloadProgressKey";
|
||||
NSString *const kAttachmentDownloadAttachmentIDKey = @"kAttachmentDownloadAttachmentIDKey";
|
||||
|
||||
// Use a slightly non-zero value to ensure that the progress
|
||||
// indicator shows up as quickly as possible.
|
||||
static const CGFloat kAttachmentDownloadProgressTheta = 0.001f;
|
||||
|
||||
typedef void (^AttachmentDownloadSuccess)(TSAttachmentStream *attachmentStream);
|
||||
typedef void (^AttachmentDownloadFailure)(NSError *error);
|
||||
|
||||
@interface OWSAttachmentDownloadJob : NSObject
|
||||
|
||||
@property (nonatomic, readonly) TSAttachmentPointer *attachmentPointer;
|
||||
@property (nonatomic, readonly, nullable) TSMessage *message;
|
||||
@property (nonatomic, readonly) AttachmentDownloadSuccess success;
|
||||
@property (nonatomic, readonly) AttachmentDownloadFailure failure;
|
||||
@property (atomic) CGFloat progress;
|
||||
|
||||
@end
|
||||
|
||||
#pragma mark -
|
||||
|
||||
@implementation OWSAttachmentDownloadJob
|
||||
|
||||
- (instancetype)initWithAttachmentPointer:(TSAttachmentPointer *)attachmentPointer
|
||||
message:(nullable TSMessage *)message
|
||||
success:(AttachmentDownloadSuccess)success
|
||||
failure:(AttachmentDownloadFailure)failure
|
||||
{
|
||||
self = [super init];
|
||||
if (!self) {
|
||||
return self;
|
||||
}
|
||||
|
||||
_attachmentPointer = attachmentPointer;
|
||||
_message = message;
|
||||
_success = success;
|
||||
_failure = failure;
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
#pragma mark -
|
||||
|
||||
@interface OWSAttachmentDownloads ()
|
||||
|
||||
// This property should only be accessed while synchronized on this class.
|
||||
@property (nonatomic, readonly) NSMutableDictionary<NSString *, OWSAttachmentDownloadJob *> *downloadingJobMap;
|
||||
// This property should only be accessed while synchronized on this class.
|
||||
@property (nonatomic, readonly) NSMutableArray<OWSAttachmentDownloadJob *> *attachmentDownloadJobQueue;
|
||||
|
||||
@end
|
||||
|
||||
#pragma mark -
|
||||
|
||||
@implementation OWSAttachmentDownloads
|
||||
|
||||
#pragma mark - Dependencies
|
||||
|
||||
- (OWSPrimaryStorage *)primaryStorage
|
||||
{
|
||||
return SSKEnvironment.shared.primaryStorage;
|
||||
}
|
||||
|
||||
#pragma mark -
|
||||
|
||||
- (instancetype)init
|
||||
{
|
||||
self = [super init];
|
||||
if (!self) {
|
||||
return self;
|
||||
}
|
||||
|
||||
_downloadingJobMap = [NSMutableDictionary new];
|
||||
_attachmentDownloadJobQueue = [NSMutableArray new];
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
#pragma mark -
|
||||
|
||||
- (nullable NSNumber *)downloadProgressForAttachmentId:(NSString *)attachmentId
|
||||
{
|
||||
|
||||
@synchronized(self) {
|
||||
OWSAttachmentDownloadJob *_Nullable job = self.downloadingJobMap[attachmentId];
|
||||
if (!job) {
|
||||
return nil;
|
||||
}
|
||||
return @(job.progress);
|
||||
}
|
||||
}
|
||||
|
||||
- (void)downloadAttachmentsForMessage:(TSMessage *)message
|
||||
transaction:(YapDatabaseReadTransaction *)transaction
|
||||
success:(void (^)(NSArray<TSAttachmentStream *> *attachmentStreams))success
|
||||
failure:(void (^)(NSError *error))failure
|
||||
{
|
||||
OWSAssertDebug(transaction);
|
||||
OWSAssertDebug(message);
|
||||
|
||||
NSMutableArray<TSAttachmentStream *> *attachmentStreams = [NSMutableArray array];
|
||||
NSMutableArray<TSAttachmentPointer *> *attachmentPointers = [NSMutableArray new];
|
||||
|
||||
for (TSAttachment *attachment in [message attachmentsWithTransaction:transaction]) {
|
||||
if ([attachment isKindOfClass:[TSAttachmentStream class]]) {
|
||||
TSAttachmentStream *attachmentStream = (TSAttachmentStream *)attachment;
|
||||
[attachmentStreams addObject:attachmentStream];
|
||||
} else if ([attachment isKindOfClass:[TSAttachmentPointer class]]) {
|
||||
TSAttachmentPointer *attachmentPointer = (TSAttachmentPointer *)attachment;
|
||||
if (attachmentPointer.pointerType != TSAttachmentPointerTypeIncoming) {
|
||||
OWSLogInfo(@"Ignoring restoring attachment.");
|
||||
continue;
|
||||
}
|
||||
[attachmentPointers addObject:attachmentPointer];
|
||||
} else {
|
||||
OWSFailDebug(@"Unexpected attachment type: %@", attachment.class);
|
||||
}
|
||||
}
|
||||
|
||||
[self enqueueJobsForAttachmentStreams:attachmentStreams
|
||||
attachmentPointers:attachmentPointers
|
||||
message:message
|
||||
success:success
|
||||
failure:failure];
|
||||
}
|
||||
|
||||
- (void)downloadAttachmentPointer:(TSAttachmentPointer *)attachmentPointer
|
||||
success:(void (^)(NSArray<TSAttachmentStream *> *attachmentStreams))success
|
||||
failure:(void (^)(NSError *error))failure
|
||||
{
|
||||
OWSAssertDebug(attachmentPointer);
|
||||
|
||||
[self enqueueJobsForAttachmentStreams:@[]
|
||||
attachmentPointers:@[ attachmentPointer ]
|
||||
message:nil
|
||||
success:success
|
||||
failure:failure];
|
||||
}
|
||||
|
||||
- (void)enqueueJobsForAttachmentStreams:(NSArray<TSAttachmentStream *> *)attachmentStreamsParam
|
||||
attachmentPointers:(NSArray<TSAttachmentPointer *> *)attachmentPointers
|
||||
message:(nullable TSMessage *)message
|
||||
success:(void (^)(NSArray<TSAttachmentStream *> *attachmentStreams))successHandler
|
||||
failure:(void (^)(NSError *error))failureHandler
|
||||
{
|
||||
OWSAssertDebug(attachmentStreamsParam);
|
||||
|
||||
// To avoid deadlocks, synchronize on self outside of the transaction.
|
||||
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
|
||||
if (attachmentPointers.count < 1) {
|
||||
OWSAssertDebug(attachmentStreamsParam.count > 0);
|
||||
successHandler(attachmentStreamsParam);
|
||||
return;
|
||||
}
|
||||
|
||||
NSMutableArray<TSAttachmentStream *> *attachmentStreams = [attachmentStreamsParam mutableCopy];
|
||||
NSMutableArray<AnyPromise *> *promises = [NSMutableArray array];
|
||||
for (TSAttachmentPointer *attachmentPointer in attachmentPointers) {
|
||||
AnyPromise *promise = [AnyPromise promiseWithResolverBlock:^(PMKResolver resolve) {
|
||||
[self enqueueJobForAttachmentPointer:attachmentPointer
|
||||
message:message
|
||||
success:^(TSAttachmentStream *attachmentStream) {
|
||||
@synchronized(attachmentStreams) {
|
||||
[attachmentStreams addObject:attachmentStream];
|
||||
}
|
||||
|
||||
resolve(@(1));
|
||||
}
|
||||
failure:^(NSError *error) {
|
||||
resolve(error);
|
||||
}];
|
||||
}];
|
||||
[promises addObject:promise];
|
||||
}
|
||||
|
||||
// We use PMKJoin(), not PMKWhen(), because we don't want the
|
||||
// completion promise to execute until _all_ promises
|
||||
// have either succeeded or failed. PMKWhen() executes as
|
||||
// soon as any of its input promises fail.
|
||||
AnyPromise *completionPromise
|
||||
= PMKJoin(promises)
|
||||
.then(^(id value) {
|
||||
NSArray<TSAttachmentStream *> *attachmentStreamsCopy;
|
||||
@synchronized(attachmentStreams) {
|
||||
attachmentStreamsCopy = [attachmentStreams copy];
|
||||
}
|
||||
OWSLogInfo(@"Attachment downloads succeeded: %lu.", (unsigned long)attachmentStreamsCopy.count);
|
||||
|
||||
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
|
||||
successHandler(attachmentStreamsCopy);
|
||||
});
|
||||
})
|
||||
.catch(^(NSError *error) {
|
||||
OWSLogError(@"Attachment downloads failed.");
|
||||
|
||||
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
|
||||
failureHandler(error);
|
||||
});
|
||||
});
|
||||
[completionPromise retainUntilComplete];
|
||||
});
|
||||
}
|
||||
|
||||
- (void)enqueueJobForAttachmentPointer:(TSAttachmentPointer *)attachmentPointer
|
||||
message:(nullable TSMessage *)message
|
||||
success:(void (^)(TSAttachmentStream *attachmentStream))success
|
||||
failure:(void (^)(NSError *error))failure
|
||||
{
|
||||
OWSAssertDebug(attachmentPointer);
|
||||
|
||||
OWSAttachmentDownloadJob *job = [[OWSAttachmentDownloadJob alloc] initWithAttachmentPointer:attachmentPointer
|
||||
message:message
|
||||
success:success
|
||||
failure:failure];
|
||||
|
||||
@synchronized(self) {
|
||||
[self.attachmentDownloadJobQueue addObject:job];
|
||||
}
|
||||
|
||||
[self startDownloadIfPossible];
|
||||
}
|
||||
|
||||
- (void)startDownloadIfPossible
|
||||
{
|
||||
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
|
||||
OWSAttachmentDownloadJob *_Nullable job;
|
||||
|
||||
@synchronized(self) {
|
||||
const NSUInteger kMaxSimultaneousDownloads = 4;
|
||||
if (self.downloadingJobMap.count >= kMaxSimultaneousDownloads) {
|
||||
return;
|
||||
}
|
||||
job = self.attachmentDownloadJobQueue.firstObject;
|
||||
if (!job) {
|
||||
return;
|
||||
}
|
||||
if (self.downloadingJobMap[job.attachmentPointer.uniqueId] != nil) {
|
||||
// Ensure we only have one download in flight at a time for a given attachment.
|
||||
OWSLogWarn(@"Ignoring duplicate download.");
|
||||
return;
|
||||
}
|
||||
[self.attachmentDownloadJobQueue removeObjectAtIndex:0];
|
||||
self.downloadingJobMap[job.attachmentPointer.uniqueId] = job;
|
||||
}
|
||||
|
||||
[LKStorage writeSyncWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
|
||||
job.attachmentPointer.state = TSAttachmentPointerStateDownloading;
|
||||
[job.attachmentPointer saveWithTransaction:transaction];
|
||||
|
||||
if (job.message) {
|
||||
[job.message saveWithTransaction:transaction];
|
||||
[job.message touchWithTransaction:transaction];
|
||||
}
|
||||
}];
|
||||
|
||||
[self retrieveAttachmentForJob:job
|
||||
success:^(TSAttachmentStream *attachmentStream) {
|
||||
OWSLogVerbose(@"Attachment download succeeded.");
|
||||
|
||||
[LKStorage writeSyncWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
|
||||
[attachmentStream saveWithTransaction:transaction];
|
||||
|
||||
if (job.message) {
|
||||
[job.message touchWithTransaction:transaction];
|
||||
}
|
||||
}];
|
||||
|
||||
job.success(attachmentStream);
|
||||
|
||||
@synchronized(self) {
|
||||
[self.downloadingJobMap removeObjectForKey:job.attachmentPointer.uniqueId];
|
||||
}
|
||||
|
||||
[self startDownloadIfPossible];
|
||||
}
|
||||
failure:^(NSError *error) {
|
||||
OWSLogError(@"Attachment download failed with error: %@", error);
|
||||
|
||||
[LKStorage writeSyncWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
|
||||
job.attachmentPointer.mostRecentFailureLocalizedText = error.localizedDescription;
|
||||
job.attachmentPointer.state = TSAttachmentPointerStateFailed;
|
||||
[job.attachmentPointer saveWithTransaction:transaction];
|
||||
|
||||
if (job.message) {
|
||||
[job.message touchWithTransaction:transaction];
|
||||
}
|
||||
}];
|
||||
|
||||
@synchronized(self) {
|
||||
[self.downloadingJobMap removeObjectForKey:job.attachmentPointer.uniqueId];
|
||||
}
|
||||
|
||||
job.failure(error);
|
||||
|
||||
[self startDownloadIfPossible];
|
||||
}];
|
||||
});
|
||||
}
|
||||
|
||||
#pragma mark -
|
||||
|
||||
- (void)continueDownloadIfPossible
|
||||
{
|
||||
if (self.attachmentDownloadJobQueue.count > 0) {
|
||||
NSLog(@"[Loki] Continuing unfinished attachment download tasks.");
|
||||
[self startDownloadIfPossible];
|
||||
}
|
||||
}
|
||||
|
||||
#pragma mark -
|
||||
|
||||
- (void)retrieveAttachmentForJob:(OWSAttachmentDownloadJob *)job
|
||||
success:(void (^)(TSAttachmentStream *attachmentStream))successHandler
|
||||
failure:(void (^)(NSError *error))failureHandler
|
||||
{
|
||||
OWSAssertDebug(job);
|
||||
TSAttachmentPointer *attachmentPointer = job.attachmentPointer;
|
||||
|
||||
__block OWSBackgroundTask *_Nullable backgroundTask =
|
||||
[OWSBackgroundTask backgroundTaskWithLabelStr:__PRETTY_FUNCTION__];
|
||||
|
||||
void (^markAndHandleFailure)(NSError *) = ^(NSError *error) {
|
||||
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
|
||||
failureHandler(error);
|
||||
|
||||
OWSAssertDebug(backgroundTask);
|
||||
backgroundTask = nil;
|
||||
});
|
||||
};
|
||||
|
||||
void (^markAndHandleSuccess)(TSAttachmentStream *attachmentStream) = ^(TSAttachmentStream *attachmentStream) {
|
||||
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
|
||||
successHandler(attachmentStream);
|
||||
|
||||
OWSAssertDebug(backgroundTask);
|
||||
backgroundTask = nil;
|
||||
});
|
||||
};
|
||||
|
||||
__block NSUInteger retryCount = 0;
|
||||
NSUInteger maxRetryCount = 4;
|
||||
__block void (^attempt)();
|
||||
attempt = ^() {
|
||||
dispatch_async([OWSDispatch attachmentsQueue], ^{
|
||||
[self downloadFromLocation:attachmentPointer.downloadURL
|
||||
job:job
|
||||
success:^(NSString *encryptedDataFilePath) {
|
||||
[self decryptAttachmentPath:encryptedDataFilePath
|
||||
attachmentPointer:attachmentPointer
|
||||
success:markAndHandleSuccess
|
||||
failure:markAndHandleFailure];
|
||||
}
|
||||
failure:^(NSURLSessionTask *task, NSError *error) {
|
||||
if (retryCount == maxRetryCount) {
|
||||
markAndHandleFailure(error);
|
||||
} else {
|
||||
retryCount += 1;
|
||||
attempt();
|
||||
}
|
||||
}];
|
||||
});
|
||||
};
|
||||
attempt();
|
||||
}
|
||||
|
||||
- (void)decryptAttachmentPath:(NSString *)encryptedDataFilePath
|
||||
attachmentPointer:(TSAttachmentPointer *)attachmentPointer
|
||||
success:(void (^)(TSAttachmentStream *attachmentStream))success
|
||||
failure:(void (^)(NSError *error))failure
|
||||
{
|
||||
OWSAssertDebug(encryptedDataFilePath.length > 0);
|
||||
OWSAssertDebug(attachmentPointer);
|
||||
|
||||
// Use attachmentDecryptSerialQueue to ensure that we only load into memory
|
||||
// & decrypt a single attachment at a time.
|
||||
dispatch_async(self.attachmentDecryptSerialQueue, ^{
|
||||
@autoreleasepool {
|
||||
NSData *_Nullable encryptedData = [NSData dataWithContentsOfFile:encryptedDataFilePath];
|
||||
if (!encryptedData) {
|
||||
OWSLogError(@"Could not load encrypted data.");
|
||||
NSError *error = OWSErrorWithCodeDescription(
|
||||
OWSErrorCodeInvalidMessage, NSLocalizedString(@"ERROR_MESSAGE_INVALID_MESSAGE", @""));
|
||||
return failure(error);
|
||||
}
|
||||
|
||||
[self decryptAttachmentData:encryptedData
|
||||
attachmentPointer:attachmentPointer
|
||||
success:success
|
||||
failure:failure];
|
||||
|
||||
if (![OWSFileSystem deleteFile:encryptedDataFilePath]) {
|
||||
OWSLogError(@"Could not delete temporary file.");
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
- (void)decryptAttachmentData:(NSData *)cipherText
|
||||
attachmentPointer:(TSAttachmentPointer *)attachmentPointer
|
||||
success:(void (^)(TSAttachmentStream *attachmentStream))successHandler
|
||||
failure:(void (^)(NSError *error))failureHandler
|
||||
{
|
||||
OWSAssertDebug(attachmentPointer);
|
||||
|
||||
NSError *decryptError;
|
||||
NSData *_Nullable plaintext;
|
||||
if (attachmentPointer.encryptionKey != nil) {
|
||||
plaintext = [Cryptography decryptAttachment:cipherText
|
||||
withKey:attachmentPointer.encryptionKey
|
||||
digest:attachmentPointer.digest
|
||||
unpaddedSize:attachmentPointer.byteCount
|
||||
error:&decryptError];
|
||||
} else {
|
||||
plaintext = cipherText; // Loki: Public chat attachments are unencrypted
|
||||
}
|
||||
|
||||
if (decryptError) {
|
||||
OWSLogError(@"failed to decrypt with error: %@", decryptError);
|
||||
failureHandler(decryptError);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!plaintext) {
|
||||
NSError *error = OWSErrorWithCodeDescription(
|
||||
OWSErrorCodeFailedToDecryptMessage, NSLocalizedString(@"ERROR_MESSAGE_INVALID_MESSAGE", @""));
|
||||
failureHandler(error);
|
||||
return;
|
||||
}
|
||||
|
||||
TSAttachmentStream *stream = [[TSAttachmentStream alloc] initWithPointer:attachmentPointer];
|
||||
|
||||
NSError *writeError;
|
||||
[stream writeData:plaintext error:&writeError];
|
||||
if (writeError) {
|
||||
OWSLogError(@"Failed writing attachment stream with error: %@", writeError);
|
||||
failureHandler(writeError);
|
||||
return;
|
||||
}
|
||||
|
||||
successHandler(stream);
|
||||
}
|
||||
|
||||
- (dispatch_queue_t)attachmentDecryptSerialQueue
|
||||
{
|
||||
static dispatch_queue_t _serialQueue;
|
||||
static dispatch_once_t onceToken;
|
||||
dispatch_once(&onceToken, ^{
|
||||
_serialQueue = dispatch_queue_create("org.whispersystems.attachment.decrypt", DISPATCH_QUEUE_SERIAL);
|
||||
});
|
||||
|
||||
return _serialQueue;
|
||||
}
|
||||
|
||||
- (void)downloadFromLocation:(NSString *)location
|
||||
job:(OWSAttachmentDownloadJob *)job
|
||||
success:(void (^)(NSString *encryptedDataPath))successHandler
|
||||
failure:(void (^)(NSURLSessionTask *_Nullable task, NSError *error))failureHandlerParam
|
||||
{
|
||||
OWSAssertDebug(job);
|
||||
TSAttachmentPointer *attachmentPointer = job.attachmentPointer;
|
||||
|
||||
// We want to avoid large downloads from a compromised or buggy service.
|
||||
const long kMaxDownloadSize = 10 * 1024 * 1024;
|
||||
__block BOOL hasCheckedContentLength = NO;
|
||||
|
||||
NSString *tempFilePath =
|
||||
[OWSTemporaryDirectoryAccessibleAfterFirstAuth() 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 #1.");
|
||||
}
|
||||
|
||||
failureHandlerParam(task, error);
|
||||
};
|
||||
|
||||
[[SNFileServerAPI downloadAttachmentFrom:location].then(^(NSData *data) {
|
||||
BOOL success = [data writeToFile:tempFilePath atomically:YES];
|
||||
if (success) {
|
||||
successHandler(tempFilePath);
|
||||
}
|
||||
|
||||
NSNumber *_Nullable fileSize = [OWSFileSystem fileSizeOfPath:tempFilePath];
|
||||
if (!fileSize) {
|
||||
OWSLogError(@"Could not determine attachment file size.");
|
||||
NSError *error = OWSErrorWithCodeDescription(
|
||||
OWSErrorCodeInvalidMessage, NSLocalizedString(@"ERROR_MESSAGE_INVALID_MESSAGE", @""));
|
||||
return failureHandler(error);
|
||||
}
|
||||
if (fileSize.unsignedIntegerValue > kMaxDownloadSize) {
|
||||
OWSLogError(@"Attachment download length exceeds max size.");
|
||||
NSError *error = OWSErrorWithCodeDescription(
|
||||
OWSErrorCodeInvalidMessage, NSLocalizedString(@"ERROR_MESSAGE_INVALID_MESSAGE", @""));
|
||||
return failureHandler(error);
|
||||
}
|
||||
}) retainUntilComplete];
|
||||
}
|
||||
|
||||
- (void)fireProgressNotification:(CGFloat)progress attachmentId:(NSString *)attachmentId
|
||||
{
|
||||
NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
|
||||
[notificationCenter postNotificationNameAsync:kAttachmentDownloadProgressNotification
|
||||
object:nil
|
||||
userInfo:@{
|
||||
kAttachmentDownloadProgressKey : @(progress),
|
||||
kAttachmentDownloadAttachmentIDKey : attachmentId
|
||||
}];
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
|
@ -22,7 +22,6 @@ FOUNDATION_EXPORT const unsigned char SignalUtilitiesKitVersionString[];
|
|||
#import <SignalUtilitiesKit/NSSet+Functional.h>
|
||||
#import <SignalUtilitiesKit/NSURLSessionDataTask+StatusCode.h>
|
||||
#import <SignalUtilitiesKit/OWSAnyTouchGestureRecognizer.h>
|
||||
#import <SignalUtilitiesKit/OWSAttachmentDownloads.h>
|
||||
#import <SignalUtilitiesKit/OWSDatabaseMigration.h>
|
||||
#import <SignalUtilitiesKit/OWSDatabaseMigrationRunner.h>
|
||||
#import <SignalUtilitiesKit/OWSDispatch.h>
|
||||
|
|
|
@ -7,7 +7,6 @@
|
|||
#import "VersionMigrations.h"
|
||||
#import <SignalUtilitiesKit/OWSDatabaseMigration.h>
|
||||
#import <SignalUtilitiesKit/OWSProfileManager.h>
|
||||
#import <SignalUtilitiesKit/OWSAttachmentDownloads.h>
|
||||
#import <SessionMessagingKit/OWSBackgroundTask.h>
|
||||
#import <SessionMessagingKit/OWSBlockingManager.h>
|
||||
#import <SessionMessagingKit/OWSDisappearingMessagesJob.h>
|
||||
|
|
Loading…
Reference in a new issue