Include size in attachment pointer

// FREEBIE
pull/1/head
Michael Kirk 8 years ago
parent efd58022d0
commit 6eeb78157a

@ -917,10 +917,12 @@ NS_ASSUME_NONNULL_BEGIN
break; break;
} }
case 2: { case 2: {
UInt32 filesize = 64;
TSAttachmentPointer *pointer = TSAttachmentPointer *pointer =
[[TSAttachmentPointer alloc] initWithServerId:237391539706350548 [[TSAttachmentPointer alloc] initWithServerId:237391539706350548
key:[self createRandomNSDataOfSize:64] key:[self createRandomNSDataOfSize:filesize]
digest:nil digest:nil
byteCount:filesize
contentType:@"audio/mp3" contentType:@"audio/mp3"
relay:@"" relay:@""
sourceFilename:@"test.mp3" sourceFilename:@"test.mp3"
@ -947,11 +949,14 @@ NS_ASSUME_NONNULL_BEGIN
expiresInSeconds:0]; expiresInSeconds:0];
NSString *filename = @"test.mp3"; NSString *filename = @"test.mp3";
TSAttachmentStream *attachmentStream = UInt32 filesize = 16;
[[TSAttachmentStream alloc] initWithContentType:@"audio/mp3" sourceFilename:filename];
TSAttachmentStream *attachmentStream = [[TSAttachmentStream alloc] initWithContentType:@"audio/mp3"
byteCount:filesize
sourceFilename:filename];
NSError *error; NSError *error;
BOOL success = [attachmentStream writeData:[self createRandomNSDataOfSize:16] error:&error]; BOOL success = [attachmentStream writeData:[self createRandomNSDataOfSize:filesize] error:&error];
OWSAssert(success && !error); OWSAssert(success && !error);
[attachmentStream saveWithTransaction:transaction]; [attachmentStream saveWithTransaction:transaction];

@ -98,6 +98,7 @@ static const CGFloat kAttachmentDownloadProgressTheta = 0.001f;
TSAttachmentPointer *pointer = [[TSAttachmentPointer alloc] initWithServerId:attachmentProto.id TSAttachmentPointer *pointer = [[TSAttachmentPointer alloc] initWithServerId:attachmentProto.id
key:attachmentProto.key key:attachmentProto.key
digest:digest digest:digest
byteCount:attachmentProto.size
contentType:attachmentProto.contentType contentType:attachmentProto.contentType
relay:relay relay:relay
sourceFilename:attachmentProto.fileName sourceFilename:attachmentProto.fileName

@ -27,10 +27,12 @@ typedef NS_ENUM(NSUInteger, TSAttachmentType) {
@property (atomic, readwrite) UInt64 serverId; @property (atomic, readwrite) UInt64 serverId;
@property (atomic, readwrite) NSData *encryptionKey; @property (atomic, readwrite) NSData *encryptionKey;
@property (nonatomic, readonly) NSString *contentType; @property (nonatomic, readonly) NSString *contentType;
@property (atomic, readwrite) BOOL isDownloaded; @property (atomic, readwrite) BOOL isDownloaded;
@property (nonatomic) TSAttachmentType attachmentType; @property (nonatomic) TSAttachmentType attachmentType;
// Though now required, may incorrectly be 0 on legacy attachments.
@property (nonatomic, readonly) UInt32 byteCount;
// Represents the "source" filename sent or received in the protos, // Represents the "source" filename sent or received in the protos,
// not the filename on disk. // not the filename on disk.
@property (nonatomic, readonly, nullable) NSString *sourceFilename; @property (nonatomic, readonly, nullable) NSString *sourceFilename;
@ -39,12 +41,15 @@ typedef NS_ENUM(NSUInteger, TSAttachmentType) {
// i.e. undownloaded incoming attachments. // i.e. undownloaded incoming attachments.
- (instancetype)initWithServerId:(UInt64)serverId - (instancetype)initWithServerId:(UInt64)serverId
encryptionKey:(NSData *)encryptionKey encryptionKey:(NSData *)encryptionKey
byteCount:(UInt32)byteCount
contentType:(NSString *)contentType contentType:(NSString *)contentType
sourceFilename:(nullable NSString *)sourceFilename; sourceFilename:(nullable NSString *)sourceFilename;
// This constructor is used for new instances of TSAttachmentStream // This constructor is used for new instances of TSAttachmentStream
// that represent new, un-uploaded outgoing attachments. // that represent new, un-uploaded outgoing attachments.
- (instancetype)initWithContentType:(NSString *)contentType sourceFilename:(nullable NSString *)sourceFilename; - (instancetype)initWithContentType:(NSString *)contentType
byteCount:(UInt32)byteCount
sourceFilename:(nullable NSString *)sourceFilename;
// This constructor is used for new instances of TSAttachmentStream // This constructor is used for new instances of TSAttachmentStream
// that represent downloaded incoming attachments. // that represent downloaded incoming attachments.

@ -21,9 +21,18 @@ NSUInteger const TSAttachmentSchemaVersion = 3;
// i.e. undownloaded incoming attachments. // i.e. undownloaded incoming attachments.
- (instancetype)initWithServerId:(UInt64)serverId - (instancetype)initWithServerId:(UInt64)serverId
encryptionKey:(NSData *)encryptionKey encryptionKey:(NSData *)encryptionKey
byteCount:(UInt32)byteCount
contentType:(NSString *)contentType contentType:(NSString *)contentType
sourceFilename:(nullable NSString *)sourceFilename sourceFilename:(nullable NSString *)sourceFilename
{ {
OWSAssert(serverId > 0);
OWSAssert(encryptionKey.length > 0);
if (byteCount <= 0) {
// This will fail with legacy iOS clients which don't upload attachment size.
DDLogWarn(@"%@ Missing byteCount for attachment with serverId: %lld", self.tag, serverId);
}
OWSAssert(contentType.length > 0);
self = [super init]; self = [super init];
if (!self) { if (!self) {
return self; return self;
@ -31,26 +40,35 @@ NSUInteger const TSAttachmentSchemaVersion = 3;
_serverId = serverId; _serverId = serverId;
_encryptionKey = encryptionKey; _encryptionKey = encryptionKey;
_byteCount = byteCount;
_contentType = contentType; _contentType = contentType;
_attachmentSchemaVersion = TSAttachmentSchemaVersion;
_sourceFilename = sourceFilename; _sourceFilename = sourceFilename;
_attachmentSchemaVersion = TSAttachmentSchemaVersion;
return self; return self;
} }
// This constructor is used for new instances of TSAttachmentStream // This constructor is used for new instances of TSAttachmentStream
// that represent new, un-uploaded outgoing attachments. // that represent new, un-uploaded outgoing attachments.
- (instancetype)initWithContentType:(NSString *)contentType sourceFilename:(nullable NSString *)sourceFilename - (instancetype)initWithContentType:(NSString *)contentType
byteCount:(UInt32)byteCount
sourceFilename:(nullable NSString *)sourceFilename
{ {
OWSAssert(contentType.length > 0);
OWSAssert(byteCount > 0);
self = [super init]; self = [super init];
if (!self) { if (!self) {
return self; return self;
} }
_contentType = contentType; _contentType = contentType;
_attachmentSchemaVersion = TSAttachmentSchemaVersion; _byteCount = byteCount;
_sourceFilename = sourceFilename; _sourceFilename = sourceFilename;
_attachmentSchemaVersion = TSAttachmentSchemaVersion;
return self; return self;
} }
@ -58,6 +76,14 @@ NSUInteger const TSAttachmentSchemaVersion = 3;
// that represent downloaded incoming attachments. // that represent downloaded incoming attachments.
- (instancetype)initWithPointer:(TSAttachment *)pointer - (instancetype)initWithPointer:(TSAttachment *)pointer
{ {
OWSAssert(pointer.serverId > 0);
OWSAssert(pointer.encryptionKey.length > 0);
if (pointer.byteCount <= 0) {
// This will fail with legacy iOS clients which don't upload attachment size.
DDLogWarn(@"%@ Missing pointer.byteCount for attachment with serverId: %lld", self.tag, pointer.serverId);
}
OWSAssert(pointer.contentType.length > 0);
// Once saved, this AttachmentStream will replace the AttachmentPointer in the attachments collection. // Once saved, this AttachmentStream will replace the AttachmentPointer in the attachments collection.
self = [super initWithUniqueId:pointer.uniqueId]; self = [super initWithUniqueId:pointer.uniqueId];
if (!self) { if (!self) {
@ -66,8 +92,10 @@ NSUInteger const TSAttachmentSchemaVersion = 3;
_serverId = pointer.serverId; _serverId = pointer.serverId;
_encryptionKey = pointer.encryptionKey; _encryptionKey = pointer.encryptionKey;
_byteCount = pointer.byteCount;
_contentType = pointer.contentType; _contentType = pointer.contentType;
_sourceFilename = pointer.sourceFilename; _sourceFilename = pointer.sourceFilename;
_attachmentSchemaVersion = TSAttachmentSchemaVersion; _attachmentSchemaVersion = TSAttachmentSchemaVersion;
return self; return self;

@ -22,6 +22,7 @@ typedef NS_ENUM(NSUInteger, TSAttachmentPointerState) {
- (instancetype)initWithServerId:(UInt64)serverId - (instancetype)initWithServerId:(UInt64)serverId
key:(NSData *)key key:(NSData *)key
digest:(nullable NSData *)digest digest:(nullable NSData *)digest
byteCount:(UInt32)byteCount
contentType:(NSString *)contentType contentType:(NSString *)contentType
relay:(NSString *)relay relay:(NSString *)relay
sourceFilename:(nullable NSString *)sourceFilename sourceFilename:(nullable NSString *)sourceFilename

@ -28,12 +28,17 @@ NS_ASSUME_NONNULL_BEGIN
- (instancetype)initWithServerId:(UInt64)serverId - (instancetype)initWithServerId:(UInt64)serverId
key:(NSData *)key key:(NSData *)key
digest:(nullable NSData *)digest digest:(nullable NSData *)digest
byteCount:(UInt32)byteCount
contentType:(NSString *)contentType contentType:(NSString *)contentType
relay:(NSString *)relay relay:(NSString *)relay
sourceFilename:(nullable NSString *)sourceFilename sourceFilename:(nullable NSString *)sourceFilename
attachmentType:(TSAttachmentType)attachmentType attachmentType:(TSAttachmentType)attachmentType
{ {
self = [super initWithServerId:serverId encryptionKey:key contentType:contentType sourceFilename:sourceFilename]; self = [super initWithServerId:serverId
encryptionKey:key
byteCount:byteCount
contentType:contentType
sourceFilename:sourceFilename];
if (!self) { if (!self) {
return self; return self;
} }

@ -17,7 +17,9 @@ NS_ASSUME_NONNULL_BEGIN
- (instancetype)init NS_UNAVAILABLE; - (instancetype)init NS_UNAVAILABLE;
- (instancetype)initWithContentType:(NSString *)contentType - (instancetype)initWithContentType:(NSString *)contentType
byteCount:(UInt32)byteCount
sourceFilename:(nullable NSString *)sourceFilename NS_DESIGNATED_INITIALIZER; sourceFilename:(nullable NSString *)sourceFilename NS_DESIGNATED_INITIALIZER;
- (instancetype)initWithPointer:(TSAttachmentPointer *)pointer NS_DESIGNATED_INITIALIZER; - (instancetype)initWithPointer:(TSAttachmentPointer *)pointer NS_DESIGNATED_INITIALIZER;
- (nullable instancetype)initWithCoder:(NSCoder *)coder NS_DESIGNATED_INITIALIZER; - (nullable instancetype)initWithCoder:(NSCoder *)coder NS_DESIGNATED_INITIALIZER;

@ -30,9 +30,11 @@ NS_ASSUME_NONNULL_BEGIN
@implementation TSAttachmentStream @implementation TSAttachmentStream
- (instancetype)initWithContentType:(NSString *)contentType sourceFilename:(nullable NSString *)sourceFilename - (instancetype)initWithContentType:(NSString *)contentType
byteCount:(UInt32)byteCount
sourceFilename:(nullable NSString *)sourceFilename
{ {
self = [super initWithContentType:contentType sourceFilename:sourceFilename]; self = [super initWithContentType:contentType byteCount:byteCount sourceFilename:sourceFilename];
if (!self) { if (!self) {
return self; return self;
} }

@ -538,6 +538,7 @@ NSString *const kTSOutgoingMessageSentRecipientAll = @"kTSOutgoingMessageSentRec
[builder setId:attachmentStream.serverId]; [builder setId:attachmentStream.serverId];
[builder setContentType:attachmentStream.contentType]; [builder setContentType:attachmentStream.contentType];
[builder setFileName:filename]; [builder setFileName:filename];
[builder setSize:attachmentStream.byteCount];
[builder setKey:attachmentStream.encryptionKey]; [builder setKey:attachmentStream.encryptionKey];
[builder setDigest:attachmentStream.digest]; [builder setDigest:attachmentStream.digest];
[builder setFlags:(self.isVoiceMessage ? OWSSignalServiceProtosAttachmentPointerFlagsVoiceMessage : 0)]; [builder setFlags:(self.isVoiceMessage ? OWSSignalServiceProtosAttachmentPointerFlagsVoiceMessage : 0)];

@ -547,8 +547,9 @@ NSString *const OWSMessageSenderRateLimitedException = @"RateLimitedException";
OWSAssert(dataSource); OWSAssert(dataSource);
dispatch_async([OWSDispatch attachmentsQueue], ^{ dispatch_async([OWSDispatch attachmentsQueue], ^{
TSAttachmentStream *attachmentStream = TSAttachmentStream *attachmentStream = [[TSAttachmentStream alloc] initWithContentType:contentType
[[TSAttachmentStream alloc] initWithContentType:contentType sourceFilename:sourceFilename]; byteCount:dataSource.dataLength
sourceFilename:sourceFilename];
if (message.isVoiceMessage) { if (message.isVoiceMessage) {
attachmentStream.attachmentType = TSAttachmentTypeVoiceMessage; attachmentStream.attachmentType = TSAttachmentTypeVoiceMessage;
} }

Loading…
Cancel
Save