|  |  |  | @ -1,6 +1,13 @@ | 
		
	
		
			
				|  |  |  |  | // | 
		
	
		
			
				|  |  |  |  | //  Copyright (c) 2017 Open Whisper Systems. All rights reserved. | 
		
	
		
			
				|  |  |  |  | // | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  | #import "MIMETypeUtil.h" | 
		
	
		
			
				|  |  |  |  | #if TARGET_OS_IPHONE | 
		
	
		
			
				|  |  |  |  | #import "UIImage+contentTypes.h" | 
		
	
		
			
				|  |  |  |  | #import <MobileCoreServices/MobileCoreServices.h> | 
		
	
		
			
				|  |  |  |  | #else | 
		
	
		
			
				|  |  |  |  | #import <CoreServices/CoreServices.h> | 
		
	
		
			
				|  |  |  |  | #endif | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  | NSString *const OWSMimeTypeApplicationOctetStream = @"application/octet-stream"; | 
		
	
	
		
			
				
					|  |  |  | @ -9,17 +16,25 @@ NSString *const OWSMimeTypeImagePng = @"image/png"; | 
		
	
		
			
				|  |  |  |  | @implementation MIMETypeUtil | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  | + (NSDictionary *)supportedVideoMIMETypesToExtensionTypes { | 
		
	
		
			
				|  |  |  |  |     return @{ | 
		
	
		
			
				|  |  |  |  |     static NSDictionary *result = nil; | 
		
	
		
			
				|  |  |  |  |     static dispatch_once_t onceToken; | 
		
	
		
			
				|  |  |  |  |     dispatch_once(&onceToken, ^{ | 
		
	
		
			
				|  |  |  |  |         result = @{ | 
		
	
		
			
				|  |  |  |  |             @"video/3gpp" : @"3gp", | 
		
	
		
			
				|  |  |  |  |             @"video/3gpp2" : @"3g2", | 
		
	
		
			
				|  |  |  |  |             @"video/mp4" : @"mp4", | 
		
	
		
			
				|  |  |  |  |             @"video/quicktime" : @"mov", | 
		
	
		
			
				|  |  |  |  |             @"video/x-m4v" : @"m4v" | 
		
	
		
			
				|  |  |  |  |         }; | 
		
	
		
			
				|  |  |  |  |     }); | 
		
	
		
			
				|  |  |  |  |     return result; | 
		
	
		
			
				|  |  |  |  | } | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  | + (NSDictionary *)supportedAudioMIMETypesToExtensionTypes { | 
		
	
		
			
				|  |  |  |  |     return @{ | 
		
	
		
			
				|  |  |  |  |     static NSDictionary *result = nil; | 
		
	
		
			
				|  |  |  |  |     static dispatch_once_t onceToken; | 
		
	
		
			
				|  |  |  |  |     dispatch_once(&onceToken, ^{ | 
		
	
		
			
				|  |  |  |  |         result = @{ | 
		
	
		
			
				|  |  |  |  |             @"audio/aac" : @"m4a", | 
		
	
		
			
				|  |  |  |  |             @"audio/x-m4p" : @"m4p", | 
		
	
		
			
				|  |  |  |  |             @"audio/x-m4b" : @"m4b", | 
		
	
	
		
			
				
					|  |  |  | @ -39,10 +54,15 @@ NSString *const OWSMimeTypeImagePng = @"image/png"; | 
		
	
		
			
				|  |  |  |  |             @"audio/3gpp2" : @"3g2", | 
		
	
		
			
				|  |  |  |  |             @"audio/3gpp" : @"3gp" | 
		
	
		
			
				|  |  |  |  |         }; | 
		
	
		
			
				|  |  |  |  |     }); | 
		
	
		
			
				|  |  |  |  |     return result; | 
		
	
		
			
				|  |  |  |  | } | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  | + (NSDictionary *)supportedImageMIMETypesToExtensionTypes { | 
		
	
		
			
				|  |  |  |  |     return @{ | 
		
	
		
			
				|  |  |  |  |     static NSDictionary *result = nil; | 
		
	
		
			
				|  |  |  |  |     static dispatch_once_t onceToken; | 
		
	
		
			
				|  |  |  |  |     dispatch_once(&onceToken, ^{ | 
		
	
		
			
				|  |  |  |  |         result = @{ | 
		
	
		
			
				|  |  |  |  |             @"image/jpeg" : @"jpeg", | 
		
	
		
			
				|  |  |  |  |             @"image/pjpeg" : @"jpeg", | 
		
	
		
			
				|  |  |  |  |             OWSMimeTypeImagePng : @"png", | 
		
	
	
		
			
				
					|  |  |  | @ -51,23 +71,38 @@ NSString *const OWSMimeTypeImagePng = @"image/png"; | 
		
	
		
			
				|  |  |  |  |             @"image/bmp" : @"bmp", | 
		
	
		
			
				|  |  |  |  |             @"image/x-windows-bmp" : @"bmp" | 
		
	
		
			
				|  |  |  |  |         }; | 
		
	
		
			
				|  |  |  |  |     }); | 
		
	
		
			
				|  |  |  |  |     return result; | 
		
	
		
			
				|  |  |  |  | } | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  | + (NSDictionary *)supportedAnimatedMIMETypesToExtensionTypes { | 
		
	
		
			
				|  |  |  |  |     return @{ | 
		
	
		
			
				|  |  |  |  |     static NSDictionary *result = nil; | 
		
	
		
			
				|  |  |  |  |     static dispatch_once_t onceToken; | 
		
	
		
			
				|  |  |  |  |     dispatch_once(&onceToken, ^{ | 
		
	
		
			
				|  |  |  |  |         result = @{ | 
		
	
		
			
				|  |  |  |  |             @"image/gif" : @"gif", | 
		
	
		
			
				|  |  |  |  |         }; | 
		
	
		
			
				|  |  |  |  |     }); | 
		
	
		
			
				|  |  |  |  |     return result; | 
		
	
		
			
				|  |  |  |  | } | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  | + (NSDictionary *)supportedBinaryDataMIMETypesToExtensionTypes | 
		
	
		
			
				|  |  |  |  | { | 
		
	
		
			
				|  |  |  |  |     return @{ | 
		
	
		
			
				|  |  |  |  |     static NSDictionary *result = nil; | 
		
	
		
			
				|  |  |  |  |     static dispatch_once_t onceToken; | 
		
	
		
			
				|  |  |  |  |     dispatch_once(&onceToken, ^{ | 
		
	
		
			
				|  |  |  |  |         result = @{ | 
		
	
		
			
				|  |  |  |  |             OWSMimeTypeApplicationOctetStream : @"dat", | 
		
	
		
			
				|  |  |  |  |         }; | 
		
	
		
			
				|  |  |  |  |     }); | 
		
	
		
			
				|  |  |  |  |     return result; | 
		
	
		
			
				|  |  |  |  | } | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  | + (NSDictionary *)supportedVideoExtensionTypesToMIMETypes { | 
		
	
		
			
				|  |  |  |  |     return @{ | 
		
	
		
			
				|  |  |  |  |     static NSDictionary *result = nil; | 
		
	
		
			
				|  |  |  |  |     static dispatch_once_t onceToken; | 
		
	
		
			
				|  |  |  |  |     dispatch_once(&onceToken, ^{ | 
		
	
		
			
				|  |  |  |  |         result = @{ | 
		
	
		
			
				|  |  |  |  |             @"3gp" : @"video/3gpp", | 
		
	
		
			
				|  |  |  |  |             @"3gpp" : @"video/3gpp", | 
		
	
		
			
				|  |  |  |  |             @"3gp2" : @"video/3gpp2", | 
		
	
	
		
			
				
					|  |  |  | @ -77,9 +112,15 @@ NSString *const OWSMimeTypeImagePng = @"image/png"; | 
		
	
		
			
				|  |  |  |  |             @"mqv" : @"video/quicktime", | 
		
	
		
			
				|  |  |  |  |             @"m4v" : @"video/x-m4v" | 
		
	
		
			
				|  |  |  |  |         }; | 
		
	
		
			
				|  |  |  |  |     }); | 
		
	
		
			
				|  |  |  |  |     return result; | 
		
	
		
			
				|  |  |  |  | } | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  | + (NSDictionary *)supportedAudioExtensionTypesToMIMETypes { | 
		
	
		
			
				|  |  |  |  |     return @{ | 
		
	
		
			
				|  |  |  |  |     static NSDictionary *result = nil; | 
		
	
		
			
				|  |  |  |  |     static dispatch_once_t onceToken; | 
		
	
		
			
				|  |  |  |  |     dispatch_once(&onceToken, ^{ | 
		
	
		
			
				|  |  |  |  |         result = @{ | 
		
	
		
			
				|  |  |  |  |             @"3gp" : @"audio/3gpp", | 
		
	
		
			
				|  |  |  |  |             @"3gpp" : @"@audio/3gpp", | 
		
	
		
			
				|  |  |  |  |             @"3g2" : @"audio/3gpp2", | 
		
	
	
		
			
				
					|  |  |  | @ -100,10 +141,15 @@ NSString *const OWSMimeTypeImagePng = @"image/png"; | 
		
	
		
			
				|  |  |  |  |             @"m4b" : @"audio/x-m4b", | 
		
	
		
			
				|  |  |  |  |             @"m4p" : @"audio/x-m4p" | 
		
	
		
			
				|  |  |  |  |         }; | 
		
	
		
			
				|  |  |  |  |     }); | 
		
	
		
			
				|  |  |  |  |     return result; | 
		
	
		
			
				|  |  |  |  | } | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  | + (NSDictionary *)supportedImageExtensionTypesToMIMETypes { | 
		
	
		
			
				|  |  |  |  |     return @{ | 
		
	
		
			
				|  |  |  |  |     static NSDictionary *result = nil; | 
		
	
		
			
				|  |  |  |  |     static dispatch_once_t onceToken; | 
		
	
		
			
				|  |  |  |  |     dispatch_once(&onceToken, ^{ | 
		
	
		
			
				|  |  |  |  |         result = @{ | 
		
	
		
			
				|  |  |  |  |             @"png" : OWSMimeTypeImagePng, | 
		
	
		
			
				|  |  |  |  |             @"x-png" : OWSMimeTypeImagePng, | 
		
	
		
			
				|  |  |  |  |             @"jfif" : @"image/jpeg", | 
		
	
	
		
			
				
					|  |  |  | @ -116,12 +162,19 @@ NSString *const OWSMimeTypeImagePng = @"image/png"; | 
		
	
		
			
				|  |  |  |  |             @"tif" : @"image/tiff", | 
		
	
		
			
				|  |  |  |  |             @"tiff" : @"image/tiff" | 
		
	
		
			
				|  |  |  |  |         }; | 
		
	
		
			
				|  |  |  |  |     }); | 
		
	
		
			
				|  |  |  |  |     return result; | 
		
	
		
			
				|  |  |  |  | } | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  | + (NSDictionary *)supportedAnimatedExtensionTypesToMIMETypes { | 
		
	
		
			
				|  |  |  |  |     return @{ | 
		
	
		
			
				|  |  |  |  |     static NSDictionary *result = nil; | 
		
	
		
			
				|  |  |  |  |     static dispatch_once_t onceToken; | 
		
	
		
			
				|  |  |  |  |     dispatch_once(&onceToken, ^{ | 
		
	
		
			
				|  |  |  |  |         result = @{ | 
		
	
		
			
				|  |  |  |  |             @"gif" : @"image/gif", | 
		
	
		
			
				|  |  |  |  |         }; | 
		
	
		
			
				|  |  |  |  |     }); | 
		
	
		
			
				|  |  |  |  |     return result; | 
		
	
		
			
				|  |  |  |  | } | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  | + (BOOL)isSupportedVideoMIMEType:(NSString *)contentType { | 
		
	
	
		
			
				
					|  |  |  | @ -303,4 +356,64 @@ NSString *const OWSMimeTypeImagePng = @"image/png"; | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  | #endif | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  | + (NSString *)utiTypeForMIMEType:(NSString *)mimeType | 
		
	
		
			
				|  |  |  |  | { | 
		
	
		
			
				|  |  |  |  |     CFStringRef utiType | 
		
	
		
			
				|  |  |  |  |         = UTTypeCreatePreferredIdentifierForTag(kUTTagClassMIMEType, (__bridge CFStringRef)mimeType, NULL); | 
		
	
		
			
				|  |  |  |  |     return (__bridge_transfer NSString *)utiType; | 
		
	
		
			
				|  |  |  |  | } | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  | + (NSSet<NSString *> *)utiTypesForMIMETypes:(NSArray *)mimeTypes | 
		
	
		
			
				|  |  |  |  | { | 
		
	
		
			
				|  |  |  |  |     NSMutableSet<NSString *> *result = [NSMutableSet new]; | 
		
	
		
			
				|  |  |  |  |     for (NSString *mimeType in mimeTypes) { | 
		
	
		
			
				|  |  |  |  |         [result addObject:[self utiTypeForMIMEType:mimeType]]; | 
		
	
		
			
				|  |  |  |  |     } | 
		
	
		
			
				|  |  |  |  |     return result; | 
		
	
		
			
				|  |  |  |  | } | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  | + (NSSet<NSString *> *)supportedVideoUTITypes | 
		
	
		
			
				|  |  |  |  | { | 
		
	
		
			
				|  |  |  |  |     OWSAssert([NSThread isMainThread]); | 
		
	
		
			
				|  |  |  |  |     static NSSet<NSString *> *result = nil; | 
		
	
		
			
				|  |  |  |  |     static dispatch_once_t onceToken; | 
		
	
		
			
				|  |  |  |  |     dispatch_once(&onceToken, ^{ | 
		
	
		
			
				|  |  |  |  |         result = [self utiTypesForMIMETypes:[self supportedVideoMIMETypesToExtensionTypes].allKeys]; | 
		
	
		
			
				|  |  |  |  |     }); | 
		
	
		
			
				|  |  |  |  |     return result; | 
		
	
		
			
				|  |  |  |  | } | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  | + (NSSet<NSString *> *)supportedAudioUTITypes | 
		
	
		
			
				|  |  |  |  | { | 
		
	
		
			
				|  |  |  |  |     OWSAssert([NSThread isMainThread]); | 
		
	
		
			
				|  |  |  |  |     static NSSet<NSString *> *result = nil; | 
		
	
		
			
				|  |  |  |  |     static dispatch_once_t onceToken; | 
		
	
		
			
				|  |  |  |  |     dispatch_once(&onceToken, ^{ | 
		
	
		
			
				|  |  |  |  |         result = [self utiTypesForMIMETypes:[self supportedAudioMIMETypesToExtensionTypes].allKeys]; | 
		
	
		
			
				|  |  |  |  |     }); | 
		
	
		
			
				|  |  |  |  |     return result; | 
		
	
		
			
				|  |  |  |  | } | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  | + (NSSet<NSString *> *)supportedImageUTITypes | 
		
	
		
			
				|  |  |  |  | { | 
		
	
		
			
				|  |  |  |  |     OWSAssert([NSThread isMainThread]); | 
		
	
		
			
				|  |  |  |  |     static NSSet<NSString *> *result = nil; | 
		
	
		
			
				|  |  |  |  |     static dispatch_once_t onceToken; | 
		
	
		
			
				|  |  |  |  |     dispatch_once(&onceToken, ^{ | 
		
	
		
			
				|  |  |  |  |         result = [self utiTypesForMIMETypes:[self supportedImageMIMETypesToExtensionTypes].allKeys]; | 
		
	
		
			
				|  |  |  |  |     }); | 
		
	
		
			
				|  |  |  |  |     return result; | 
		
	
		
			
				|  |  |  |  | } | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  | + (NSSet<NSString *> *)supportedAnimatedImageUTITypes | 
		
	
		
			
				|  |  |  |  | { | 
		
	
		
			
				|  |  |  |  |     OWSAssert([NSThread isMainThread]); | 
		
	
		
			
				|  |  |  |  |     static NSSet<NSString *> *result = nil; | 
		
	
		
			
				|  |  |  |  |     static dispatch_once_t onceToken; | 
		
	
		
			
				|  |  |  |  |     dispatch_once(&onceToken, ^{ | 
		
	
		
			
				|  |  |  |  |         result = [self utiTypesForMIMETypes:[self supportedAnimatedMIMETypesToExtensionTypes].allKeys]; | 
		
	
		
			
				|  |  |  |  |     }); | 
		
	
		
			
				|  |  |  |  |     return result; | 
		
	
		
			
				|  |  |  |  | } | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  | @end | 
		
	
	
		
			
				
					|  |  |  | 
 |