mirror of
https://github.com/oxen-io/session-ios.git
synced 2023-12-13 21:30:14 +01:00
f1714bf252
// FREEBIE
106 lines
3.8 KiB
Objective-C
106 lines
3.8 KiB
Objective-C
//
|
|
// Copyright (c) 2018 Open Whisper Systems. All rights reserved.
|
|
//
|
|
|
|
#import "TSAttachmentPointer.h"
|
|
|
|
NS_ASSUME_NONNULL_BEGIN
|
|
|
|
@implementation TSAttachmentPointer
|
|
|
|
- (nullable instancetype)initWithCoder:(NSCoder *)coder
|
|
{
|
|
self = [super initWithCoder:coder];
|
|
if (!self) {
|
|
return self;
|
|
}
|
|
|
|
// A TSAttachmentPointer is a yet-to-be-downloaded attachment.
|
|
// If this is an old TSAttachmentPointer from another session,
|
|
// we know that it failed to complete before the session completed.
|
|
if (![coder containsValueForKey:@"state"]) {
|
|
_state = TSAttachmentPointerStateFailed;
|
|
}
|
|
|
|
return self;
|
|
}
|
|
|
|
- (instancetype)initWithServerId:(UInt64)serverId
|
|
key:(NSData *)key
|
|
digest:(nullable NSData *)digest
|
|
byteCount:(UInt32)byteCount
|
|
contentType:(NSString *)contentType
|
|
relay:(NSString *)relay
|
|
sourceFilename:(nullable NSString *)sourceFilename
|
|
attachmentType:(TSAttachmentType)attachmentType
|
|
{
|
|
self = [super initWithServerId:serverId
|
|
encryptionKey:key
|
|
byteCount:byteCount
|
|
contentType:contentType
|
|
sourceFilename:sourceFilename];
|
|
if (!self) {
|
|
return self;
|
|
}
|
|
|
|
_digest = digest;
|
|
_state = TSAttachmentPointerStateEnqueued;
|
|
_relay = relay;
|
|
self.attachmentType = attachmentType;
|
|
|
|
return self;
|
|
}
|
|
|
|
|
|
+ (TSAttachmentPointer *)attachmentPointerFromProto:(OWSSignalServiceProtosAttachmentPointer *)attachmentProto
|
|
relay:(NSString *_Nullable)relay
|
|
{
|
|
OWSAssert(attachmentProto.id != 0);
|
|
OWSAssert(attachmentProto.key != nil);
|
|
OWSAssert(attachmentProto.contentType != nil);
|
|
|
|
// digest will be empty for old clients.
|
|
NSData *digest = attachmentProto.hasDigest ? attachmentProto.digest : nil;
|
|
|
|
TSAttachmentType attachmentType = TSAttachmentTypeDefault;
|
|
if ([attachmentProto hasFlags]) {
|
|
UInt32 flags = attachmentProto.flags;
|
|
if ((flags & (UInt32)OWSSignalServiceProtosAttachmentPointerFlagsVoiceMessage) > 0) {
|
|
attachmentType = TSAttachmentTypeVoiceMessage;
|
|
}
|
|
}
|
|
|
|
TSAttachmentPointer *pointer = [[TSAttachmentPointer alloc] initWithServerId:attachmentProto.id
|
|
key:attachmentProto.key
|
|
digest:digest
|
|
byteCount:attachmentProto.size
|
|
contentType:attachmentProto.contentType
|
|
relay:relay
|
|
sourceFilename:attachmentProto.fileName
|
|
attachmentType:attachmentType];
|
|
return pointer;
|
|
}
|
|
|
|
- (BOOL)isDecimalNumberText:(NSString *)text
|
|
{
|
|
return [text componentsSeparatedByCharactersInSet:[NSCharacterSet decimalDigitCharacterSet]].count == 1;
|
|
}
|
|
|
|
- (void)upgradeFromAttachmentSchemaVersion:(NSUInteger)attachmentSchemaVersion
|
|
{
|
|
// Legacy instances of TSAttachmentPointer apparently used the serverId as their
|
|
// uniqueId.
|
|
if (attachmentSchemaVersion < 2 && self.serverId == 0) {
|
|
OWSAssert([self isDecimalNumberText:self.uniqueId]);
|
|
if ([self isDecimalNumberText:self.uniqueId]) {
|
|
// For legacy instances, try to parse the serverId from the uniqueId.
|
|
self.serverId = [self.uniqueId integerValue];
|
|
} else {
|
|
DDLogError(@"%@ invalid legacy attachment uniqueId: %@.", self.logTag, self.uniqueId);
|
|
}
|
|
}
|
|
}
|
|
|
|
@end
|
|
|
|
NS_ASSUME_NONNULL_END
|