From 53623adae891b9ea5c99d9e19b8765b1a346ff64 Mon Sep 17 00:00:00 2001 From: Matthew Chen Date: Tue, 28 Mar 2017 15:19:03 -0400 Subject: [PATCH] Accept arbitrary incoming attachments. // FREEBIE --- .../Attachments/OWSAttachmentsProcessor.m | 5 +++- src/Util/MIMETypeUtil.h | 5 ++++ src/Util/MIMETypeUtil.m | 29 ++++++++++++++++++- 3 files changed, 37 insertions(+), 2 deletions(-) diff --git a/src/Messages/Attachments/OWSAttachmentsProcessor.m b/src/Messages/Attachments/OWSAttachmentsProcessor.m index 720d32a10..2cd0d4dad 100644 --- a/src/Messages/Attachments/OWSAttachmentsProcessor.m +++ b/src/Messages/Attachments/OWSAttachmentsProcessor.m @@ -79,7 +79,10 @@ NS_ASSUME_NONNULL_BEGIN [attachmentIds addObject:pointer.uniqueId]; - if ([MIMETypeUtil isSupportedMIMEType:pointer.contentType]) { + // TODO: We need to revisit isSupportedMIMEType. + if ([MIMETypeUtil isSupportedMIMEType:pointer.contentType] || + [pointer.contentType isEqualToString:OWSMimeTypeOversizeTextMessage] || + [pointer.contentType isEqualToString:OWSMimeTypeUnknownForTests]) { [pointer save]; [supportedAttachmentPointers addObject:pointer]; [supportedAttachmentIds addObject:pointer.uniqueId]; diff --git a/src/Util/MIMETypeUtil.h b/src/Util/MIMETypeUtil.h index 17bbfe55f..2ec495994 100644 --- a/src/Util/MIMETypeUtil.h +++ b/src/Util/MIMETypeUtil.h @@ -5,6 +5,7 @@ extern NSString *const OWSMimeTypeApplicationOctetStream; extern NSString *const OWSMimeTypeImagePng; extern NSString *const OWSMimeTypeOversizeTextMessage; +extern NSString *const OWSMimeTypeUnknownForTests; @interface MIMETypeUtil : NSObject @@ -52,4 +53,8 @@ extern NSString *const OWSMimeTypeOversizeTextMessage; + (NSSet *)supportedImageUTITypes; + (NSSet *)supportedAnimatedImageUTITypes; ++ (NSString *)utiTypeForMIMEType:(NSString *)mimeType; ++ (NSString *)fileExtensionForUTIType:(NSString *)utiType; ++ (NSString *)fileExtensionForMIMEType:(NSString *)mimeType; + @end diff --git a/src/Util/MIMETypeUtil.m b/src/Util/MIMETypeUtil.m index 1842e570d..fb8ab8f0a 100644 --- a/src/Util/MIMETypeUtil.m +++ b/src/Util/MIMETypeUtil.m @@ -13,6 +13,7 @@ NSString *const OWSMimeTypeApplicationOctetStream = @"application/octet-stream"; NSString *const OWSMimeTypeImagePng = @"image/png"; NSString *const OWSMimeTypeOversizeTextMessage = @"text/x-signal-plain"; +NSString *const OWSMimeTypeUnknownForTests = @"unknown/mimetype"; @implementation MIMETypeUtil @@ -294,6 +295,8 @@ NSString *const OWSMimeTypeOversizeTextMessage = @"text/x-signal-plain"; return [MIMETypeUtil filePathForBinaryData:uniqueId ofMIMEType:contentType inFolder:folder]; } else if ([contentType isEqualToString:OWSMimeTypeOversizeTextMessage]) { return [MIMETypeUtil filePathForOversizeTextMessage:uniqueId inFolder:folder]; + } else if ([contentType isEqualToString:OWSMimeTypeUnknownForTests]) { + return [MIMETypeUtil filePathForUnknownContent:uniqueId inFolder:folder]; } DDLogError(@"Got asked for path of file %@ which is unsupported", contentType); @@ -354,6 +357,13 @@ NSString *const OWSMimeTypeOversizeTextMessage = @"text/x-signal-plain"; stringByAppendingPathExtension:@"signal-text-message"]; } ++ (NSString *)filePathForUnknownContent:(NSString *)uniqueId inFolder:(NSString *)folder { + // This file extension is arbitrary - it should never be exposed to the user or + // be used outside the app. + return [[folder stringByAppendingFormat:@"/%@", uniqueId] + stringByAppendingPathExtension:@"unknown"]; +} + #if TARGET_OS_IPHONE + (NSString *)getSupportedImageMIMETypeFromImage:(UIImage *)image { @@ -369,10 +379,27 @@ NSString *const OWSMimeTypeOversizeTextMessage = @"text/x-signal-plain"; + (NSString *)utiTypeForMIMEType:(NSString *)mimeType { CFStringRef utiType - = UTTypeCreatePreferredIdentifierForTag(kUTTagClassMIMEType, (__bridge CFStringRef)mimeType, NULL); + = UTTypeCreatePreferredIdentifierForTag(kUTTagClassMIMEType, (__bridge CFStringRef)mimeType, NULL); return (__bridge_transfer NSString *)utiType; } ++ (NSString *)fileExtensionForUTIType:(NSString *)utiType +{ + CFStringRef fileExtension + = UTTypeCopyPreferredTagWithClass((__bridge CFStringRef) utiType, kUTTagClassFilenameExtension); + return (__bridge_transfer NSString *)fileExtension; +} + ++ (NSString *)fileExtensionForMIMEType:(NSString *)mimeType +{ + NSString *utiType = [self utiTypeForMIMEType:mimeType]; + if (!utiType) { + return nil; + } + NSString *fileExtension = [self fileExtensionForUTIType:utiType]; + return fileExtension; +} + + (NSSet *)utiTypesForMIMETypes:(NSArray *)mimeTypes { NSMutableSet *result = [NSMutableSet new];