From c180d20dcd879458445f725b180e161d0c9bb859 Mon Sep 17 00:00:00 2001 From: Matthew Chen Date: Fri, 15 Mar 2019 09:36:07 -0400 Subject: [PATCH] Store media size from attachment pointer protos. --- .../ViewControllers/DebugUI/DebugUIMessages.m | 6 +++-- .../Attachments/TSAttachmentPointer.h | 7 ++++-- .../Attachments/TSAttachmentPointer.m | 23 +++++++++++++++++-- .../Messages/Attachments/TSAttachmentStream.m | 13 +++++++++++ 4 files changed, 43 insertions(+), 6 deletions(-) diff --git a/Signal/src/ViewControllers/DebugUI/DebugUIMessages.m b/Signal/src/ViewControllers/DebugUI/DebugUIMessages.m index 660c0362e..acf3440cf 100644 --- a/Signal/src/ViewControllers/DebugUI/DebugUIMessages.m +++ b/Signal/src/ViewControllers/DebugUI/DebugUIMessages.m @@ -3802,7 +3802,8 @@ typedef OWSContact * (^OWSContactBlock)(YapDatabaseReadWriteTransaction *transac sourceFilename:@"test.mp3" caption:nil albumMessageId:nil - attachmentType:TSAttachmentTypeDefault]; + attachmentType:TSAttachmentTypeDefault + mediaSize:CGSizeZero]; pointer.state = TSAttachmentPointerStateFailed; [pointer saveWithTransaction:transaction]; // MJK - should be safe to remove this senderTimestamp @@ -4701,7 +4702,8 @@ typedef OWSContact * (^OWSContactBlock)(YapDatabaseReadWriteTransaction *transac sourceFilename:fakeAssetLoader.filename caption:nil albumMessageId:nil - attachmentType:TSAttachmentTypeDefault]; + attachmentType:TSAttachmentTypeDefault + mediaSize:CGSizeZero]; attachmentPointer.state = TSAttachmentPointerStateFailed; [attachmentPointer saveWithTransaction:transaction]; return attachmentPointer; diff --git a/SignalServiceKit/src/Messages/Attachments/TSAttachmentPointer.h b/SignalServiceKit/src/Messages/Attachments/TSAttachmentPointer.h index a4a19bd28..9090c9cf8 100644 --- a/SignalServiceKit/src/Messages/Attachments/TSAttachmentPointer.h +++ b/SignalServiceKit/src/Messages/Attachments/TSAttachmentPointer.h @@ -1,5 +1,5 @@ // -// Copyright (c) 2018 Open Whisper Systems. All rights reserved. +// Copyright (c) 2019 Open Whisper Systems. All rights reserved. // #import "TSAttachment.h" @@ -36,6 +36,8 @@ typedef NS_ENUM(NSUInteger, TSAttachmentPointerState) { // messages received from other clients @property (nullable, nonatomic, readonly) NSData *digest; +@property (nonatomic, readonly) CGSize mediaSize; + // Non-nil for attachments which need "lazy backup restore." - (nullable OWSBackupFragment *)lazyRestoreFragment; @@ -49,7 +51,8 @@ typedef NS_ENUM(NSUInteger, TSAttachmentPointerState) { sourceFilename:(nullable NSString *)sourceFilename caption:(nullable NSString *)caption albumMessageId:(nullable NSString *)albumMessageId - attachmentType:(TSAttachmentType)attachmentType NS_DESIGNATED_INITIALIZER; + attachmentType:(TSAttachmentType)attachmentType + mediaSize:(CGSize)mediaSize NS_DESIGNATED_INITIALIZER; - (instancetype)initForRestoreWithAttachmentStream:(TSAttachmentStream *)attachmentStream NS_DESIGNATED_INITIALIZER; diff --git a/SignalServiceKit/src/Messages/Attachments/TSAttachmentPointer.m b/SignalServiceKit/src/Messages/Attachments/TSAttachmentPointer.m index bcf7aef20..e967ed40d 100644 --- a/SignalServiceKit/src/Messages/Attachments/TSAttachmentPointer.m +++ b/SignalServiceKit/src/Messages/Attachments/TSAttachmentPointer.m @@ -1,5 +1,5 @@ // -// Copyright (c) 2018 Open Whisper Systems. All rights reserved. +// Copyright (c) 2019 Open Whisper Systems. All rights reserved. // #import "TSAttachmentPointer.h" @@ -12,6 +12,15 @@ NS_ASSUME_NONNULL_BEGIN +@interface TSAttachmentStream (TSAttachmentPointer) + +- (CGSize)cachedImageSize; + +@end + +#pragma mark - + + @interface TSAttachmentPointer () // Optional property. Only set for attachments which need "lazy backup restore." @@ -53,6 +62,7 @@ NS_ASSUME_NONNULL_BEGIN caption:(nullable NSString *)caption albumMessageId:(nullable NSString *)albumMessageId attachmentType:(TSAttachmentType)attachmentType + mediaSize:(CGSize)mediaSize { self = [super initWithServerId:serverId encryptionKey:key @@ -69,6 +79,7 @@ NS_ASSUME_NONNULL_BEGIN _state = TSAttachmentPointerStateEnqueued; self.attachmentType = attachmentType; _pointerType = TSAttachmentPointerTypeIncoming; + _mediaSize = mediaSize; return self; } @@ -89,6 +100,7 @@ NS_ASSUME_NONNULL_BEGIN _state = TSAttachmentPointerStateEnqueued; self.attachmentType = attachmentStream.attachmentType; _pointerType = TSAttachmentPointerTypeRestoring; + _mediaSize = (attachmentStream.shouldHaveImageSize ? attachmentStream.cachedImageSize : CGSizeZero); return self; } @@ -129,6 +141,12 @@ NS_ASSUME_NONNULL_BEGIN albumMessageId = albumMessage.uniqueId; } + CGSize mediaSize = CGSizeZero; + if (attachmentProto.hasWidth && attachmentProto.hasHeight && attachmentProto.width > 0 + && attachmentProto.height > 0) { + mediaSize = CGSizeMake(attachmentProto.width, attachmentProto.height); + } + TSAttachmentPointer *pointer = [[TSAttachmentPointer alloc] initWithServerId:attachmentProto.id key:attachmentProto.key digest:digest @@ -137,7 +155,8 @@ NS_ASSUME_NONNULL_BEGIN sourceFilename:attachmentProto.fileName caption:caption albumMessageId:albumMessageId - attachmentType:attachmentType]; + attachmentType:attachmentType + mediaSize:mediaSize]; return pointer; } diff --git a/SignalServiceKit/src/Messages/Attachments/TSAttachmentStream.m b/SignalServiceKit/src/Messages/Attachments/TSAttachmentStream.m index 9109cd4e3..bb0384d8d 100644 --- a/SignalServiceKit/src/Messages/Attachments/TSAttachmentStream.m +++ b/SignalServiceKit/src/Messages/Attachments/TSAttachmentStream.m @@ -526,6 +526,19 @@ typedef void (^OWSLoadedThumbnailSuccess)(OWSLoadedThumbnail *loadedThumbnail); } } +- (CGSize)cachedImageSize +{ + OWSAssertDebug(self.shouldHaveImageSize); + + @synchronized(self) { + if (self.cachedImageWidth && self.cachedImageHeight) { + return CGSizeMake(self.cachedImageWidth.floatValue, self.cachedImageHeight.floatValue); + } else { + return CGSizeZero; + } + } +} + #pragma mark - Update With... - (void)applyChangeAsyncToLatestCopyWithChangeBlock:(void (^)(TSAttachmentStream *))changeBlock