Don't explode when attachment support revoked.

Also, more nullability annotations.

NSURL can't be given a nil filepath. This can occur when a previously
supported attachment was downloaded, and then viewed after that
attachment was no longer supported (e.g. when installing back and forth
across versions)

// FREEBIE
pull/1/head
Michael Kirk 9 years ago
parent 1df99c5812
commit 3e5af16dc3

@ -6,6 +6,8 @@
#import <UIKit/UIKit.h>
#endif
NS_ASSUME_NONNULL_BEGIN
@interface TSAttachmentStream : TSAttachment
@property (nonatomic) BOOL isDownloaded;
@ -16,17 +18,19 @@
contentType:(NSString *)contentType NS_DESIGNATED_INITIALIZER;
#if TARGET_OS_IPHONE
- (UIImage *)image;
- (nullable UIImage *)image;
#endif
- (BOOL)isAnimated;
- (BOOL)isImage;
- (BOOL)isVideo;
- (NSString *)filePath;
- (NSURL *)mediaURL;
- (nullable NSString *)filePath;
- (nullable NSURL *)mediaURL;
+ (void)deleteAttachments;
+ (NSString *)attachmentsFolder;
+ (NSUInteger)numberOfItemsInAttachmentsFolder;
@end
NS_ASSUME_NONNULL_END

@ -6,6 +6,8 @@
#import <AVFoundation/AVFoundation.h>
#import <YapDatabase/YapDatabaseTransaction.h>
NS_ASSUME_NONNULL_BEGIN
@implementation TSAttachmentStream
- (instancetype)initWithIdentifier:(NSString *)identifier
@ -73,15 +75,17 @@
return count;
}
- (NSString *)filePath
- (nullable NSString *)filePath
{
return [MIMETypeUtil filePathForAttachment:self.uniqueId
ofMIMEType:self.contentType
inFolder:[[self class] attachmentsFolder]];
}
- (NSURL *)mediaURL {
return [NSURL fileURLWithPath:[self filePath]];
- (nullable NSURL *)mediaURL
{
NSString *filePath = self.filePath;
return filePath ? [NSURL fileURLWithPath:filePath] : nil;
}
- (BOOL)isAnimated {
@ -100,7 +104,8 @@
return [MIMETypeUtil isAudio:self.contentType];
}
- (UIImage *)image {
- (nullable UIImage *)image
{
if ([self isVideo] || [self isAudio]) {
return [self videoThumbnail];
} else {
@ -109,7 +114,8 @@
}
}
- (UIImage *)videoThumbnail {
- (nullable UIImage *)videoThumbnail
{
AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:[NSURL fileURLWithPath:self.filePath] options:nil];
AVAssetImageGenerator *generate = [[AVAssetImageGenerator alloc] initWithAsset:asset];
generate.appliesPreferredTrackTransform = YES;
@ -129,3 +135,5 @@
}
@end
NS_ASSUME_NONNULL_END

Loading…
Cancel
Save