2017-03-15 14:17:41 +01:00
|
|
|
//
|
|
|
|
// Copyright (c) 2017 Open Whisper Systems. All rights reserved.
|
|
|
|
//
|
2015-12-07 03:31:43 +01:00
|
|
|
|
|
|
|
#import "TSAttachment.h"
|
2016-07-28 01:58:49 +02:00
|
|
|
#import "MIMETypeUtil.h"
|
2015-12-07 03:31:43 +01:00
|
|
|
|
2016-10-14 23:00:29 +02:00
|
|
|
NS_ASSUME_NONNULL_BEGIN
|
|
|
|
|
2017-03-15 14:17:41 +01:00
|
|
|
NSUInteger const TSAttachmentSchemaVersion = 3;
|
2016-10-14 23:00:29 +02:00
|
|
|
|
|
|
|
@interface TSAttachment ()
|
|
|
|
|
|
|
|
@property (nonatomic, readonly) NSUInteger attachmentSchemaVersion;
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
2015-12-07 03:31:43 +01:00
|
|
|
@implementation TSAttachment
|
|
|
|
|
2016-10-14 23:00:29 +02:00
|
|
|
- (instancetype)initWithServerId:(UInt64)serverId
|
|
|
|
encryptionKey:(NSData *)encryptionKey
|
|
|
|
contentType:(NSString *)contentType
|
|
|
|
{
|
|
|
|
self = [super init];
|
|
|
|
if (!self) {
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
_serverId = serverId;
|
|
|
|
_encryptionKey = encryptionKey;
|
|
|
|
_contentType = contentType;
|
|
|
|
_attachmentSchemaVersion = TSAttachmentSchemaVersion;
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (nullable instancetype)initWithCoder:(NSCoder *)coder
|
|
|
|
{
|
|
|
|
self = [super initWithCoder:coder];
|
|
|
|
if (!self) {
|
|
|
|
return self;
|
|
|
|
}
|
2015-12-07 03:31:43 +01:00
|
|
|
|
2017-03-15 14:17:41 +01:00
|
|
|
if (_attachmentSchemaVersion < TSAttachmentSchemaVersion) {
|
|
|
|
[self upgradeFromAttachmentSchemaVersion:_attachmentSchemaVersion];
|
|
|
|
_attachmentSchemaVersion = TSAttachmentSchemaVersion;
|
|
|
|
}
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)upgradeFromAttachmentSchemaVersion:(NSUInteger)attachmentSchemaVersion
|
|
|
|
{
|
|
|
|
if (attachmentSchemaVersion < 2) {
|
2016-10-14 23:00:29 +02:00
|
|
|
if (!_serverId) {
|
|
|
|
_serverId = [self.uniqueId integerValue];
|
|
|
|
if (!_serverId) {
|
|
|
|
DDLogError(@"%@ failed to parse legacy uniqueId:%@ as integer.", self.tag, self.uniqueId);
|
|
|
|
}
|
|
|
|
}
|
2015-12-07 03:31:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (NSString *)collection {
|
|
|
|
return @"TSAttachements";
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString *)description {
|
|
|
|
NSString *attachmentString = NSLocalizedString(@"ATTACHMENT", nil);
|
|
|
|
|
|
|
|
if ([MIMETypeUtil isImage:self.contentType]) {
|
|
|
|
return [NSString stringWithFormat:@"📷 %@", attachmentString];
|
|
|
|
} else if ([MIMETypeUtil isVideo:self.contentType]) {
|
|
|
|
return [NSString stringWithFormat:@"📽 %@", attachmentString];
|
|
|
|
} else if ([MIMETypeUtil isAudio:self.contentType]) {
|
|
|
|
return [NSString stringWithFormat:@"📻 %@", attachmentString];
|
2016-08-02 19:50:59 +02:00
|
|
|
} else if ([MIMETypeUtil isAnimated:self.contentType]) {
|
|
|
|
return [NSString stringWithFormat:@"🎡 %@", attachmentString];
|
2015-12-07 03:31:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return attachmentString;
|
|
|
|
}
|
|
|
|
|
2016-10-14 23:00:29 +02:00
|
|
|
#pragma mark - Logging
|
|
|
|
|
|
|
|
+ (NSString *)tag
|
|
|
|
{
|
|
|
|
return [NSString stringWithFormat:@"[%@]", self.class];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString *)tag
|
|
|
|
{
|
|
|
|
return self.class.tag;
|
|
|
|
}
|
|
|
|
|
2015-12-07 03:31:43 +01:00
|
|
|
@end
|
2016-10-14 23:00:29 +02:00
|
|
|
|
|
|
|
NS_ASSUME_NONNULL_END
|