UTIConverter.m 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. // _____ _
  2. // |_ _| |_ _ _ ___ ___ _ __ __ _
  3. // | | | ' \| '_/ -_) -_) ' \/ _` |_
  4. // |_| |_||_|_| \___\___|_|_|_\__,_(_)
  5. //
  6. // Threema iOS Client
  7. // Copyright (c) 2015-2020 Threema GmbH
  8. //
  9. // This program is free software: you can redistribute it and/or modify
  10. // it under the terms of the GNU Affero General Public License, version 3,
  11. // as published by the Free Software Foundation.
  12. //
  13. // This program is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. // GNU Affero General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Affero General Public License
  19. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. #import "UTIConverter.h"
  21. #import "BundleUtil.h"
  22. @implementation UTIConverter
  23. + (NSString *)mimeTypeFromUTI:(NSString *)uti {
  24. if ([uti isEqualToString:UTTYPE_VCARD]) {
  25. return @"text/vcard";
  26. }
  27. CFStringRef UTI = (__bridge CFStringRef)uti;
  28. CFStringRef MIMEType = UTTypeCopyPreferredTagWithClass(UTI, kUTTagClassMIMEType);
  29. NSString *MIMETypeString = (__bridge_transfer NSString *)MIMEType;
  30. if (MIMETypeString) {
  31. return MIMETypeString;
  32. } else {
  33. // fallback if unknown
  34. return @"application/octet-stream";
  35. }
  36. }
  37. + (NSString *)utiFromMimeType:(NSString *)mimeType {
  38. CFStringRef MIMEType = (__bridge CFStringRef)mimeType;
  39. CFStringRef UTI = UTTypeCreatePreferredIdentifierForTag(kUTTagClassMIMEType, MIMEType, NULL);
  40. NSString *UTIString = (__bridge_transfer NSString *)UTI;
  41. return UTIString;
  42. }
  43. + (NSString *)utiForFileURL:(NSURL *)url {
  44. CFStringRef fileExtension = (__bridge CFStringRef)[url pathExtension];
  45. CFStringRef UTI = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, fileExtension, NULL);
  46. NSString *UTIString = (__bridge_transfer NSString *)UTI;
  47. return UTIString;
  48. }
  49. + (NSString *)preferedFileExtensionForMimeType:(NSString *)mimeType {
  50. CFStringRef mimeTypeRef = (__bridge CFStringRef)mimeType;
  51. CFStringRef uti = UTTypeCreatePreferredIdentifierForTag(kUTTagClassMIMEType, mimeTypeRef, NULL);
  52. CFStringRef fileExtensionRef = UTTypeCopyPreferredTagWithClass(uti, kUTTagClassFilenameExtension);
  53. NSString *fileExtension = (__bridge_transfer NSString *)fileExtensionRef;
  54. return fileExtension;
  55. }
  56. + (BOOL)isImageMimeType:(NSString *)mimeType {
  57. return [self isKind:kUTTypeImage mimeType:mimeType];
  58. }
  59. + (BOOL)isRenderingImageMimeType:(NSString *)mimeType {
  60. if ([self isKind:kUTTypeJPEG mimeType:mimeType] || [self isKind:kUTTypePNG mimeType:mimeType]) {
  61. return true;
  62. }
  63. return false;
  64. }
  65. + (BOOL)isPNGImageMimeType:(NSString *)mimeType {
  66. if ([self isKind:kUTTypePNG mimeType:mimeType]) {
  67. return true;
  68. }
  69. return false;
  70. }
  71. + (BOOL)isRenderingVideoMimeType:(NSString *)mimeType {
  72. if ([mimeType isEqualToString:@"video/mp4"] || [mimeType isEqualToString:@"video/mpeg4"] || [mimeType isEqualToString:@"video/x-m4v"]) {
  73. return true;
  74. }
  75. return false;
  76. }
  77. + (BOOL)isRenderingAudioMimeType:(NSString *)mimeType {
  78. if ([mimeType isEqualToString:@"audio/aac"] || [mimeType isEqualToString:@"audio/m4a"] || [mimeType isEqualToString:@"audio/x-m4a"] || [mimeType isEqualToString:@"audio/mp4"]) {
  79. return true;
  80. }
  81. return false;
  82. }
  83. + (BOOL)isGifMimeType:(NSString *)mimeType {
  84. return [self isKind:kUTTypeGIF mimeType:mimeType];
  85. }
  86. + (BOOL)isAudioMimeType:(NSString *)mimeType {
  87. return [self isKind:kUTTypeAudio mimeType:mimeType];
  88. }
  89. + (BOOL)isVideoMimeType:(NSString *)mimeType {
  90. return [self isKind:kUTTypeVideo mimeType:mimeType];
  91. }
  92. + (BOOL)isMovieMimeType:(NSString *)mimeType {
  93. return [self isKind:kUTTypeMovie mimeType:mimeType];
  94. }
  95. + (BOOL)isPDFMimeType:(NSString *)mimeType {
  96. return [self isKind:kUTTypePDF mimeType:mimeType];
  97. }
  98. + (BOOL)isContactMimeType:(NSString *)mimeType {
  99. return [self isKind:kUTTypeContact mimeType:mimeType];
  100. }
  101. + (BOOL)isCalendarMimeType:(NSString *)mimeType {
  102. return [self isKind:kUTTypeCalendarEvent mimeType:mimeType];
  103. }
  104. + (BOOL)isArchiveMimeType:(NSString *)mimeType {
  105. return [self isKind:kUTTypeArchive mimeType:mimeType];
  106. }
  107. + (BOOL)isPublicContentMimeType:(NSString *)mimeType {
  108. return [self isKind:kUTTypeContent mimeType:mimeType];
  109. }
  110. + (BOOL)isPublicCompositeContentMimeType:(NSString *)mimeType {
  111. return [self isKind:kUTTypeCompositeContent mimeType:mimeType];
  112. }
  113. + (BOOL)isWordMimeType:(NSString *)mimeType {
  114. if ([mimeType hasPrefix:@"application/vnd.openxmlformats-officedocument.wordprocessingml"]) {
  115. return YES;
  116. }
  117. return [mimeType isEqualToString:@"application/msword"];
  118. }
  119. + (BOOL)isPowerpointMimeType:(NSString *)mimeType {
  120. if ([mimeType hasPrefix:@"application/vnd.openxmlformats-officedocument.presentationml"]) {
  121. return YES;
  122. }
  123. return [mimeType isEqualToString:@"application/vnd.ms-powerpointtd"];
  124. }
  125. + (BOOL)isExcelMimeType:(NSString *)mimeType {
  126. if ([mimeType hasPrefix:@"application/vnd.openxmlformats-officedocument.spreadsheetml"]) {
  127. return YES;
  128. }
  129. return [mimeType isEqualToString:@"application/vnd.ms-excel"];
  130. }
  131. + (BOOL)isTextMimeType:(NSString *)mimeType {
  132. return [self isKind:kUTTypeText mimeType:mimeType];
  133. }
  134. + (BOOL)isPassMimeType:(NSString *)mimeType {
  135. return [mimeType hasPrefix:@"application/vnd.apple.pkpass"];
  136. }
  137. + (BOOL)type:(NSString *)type conformsTo:(NSString *)referenceType {
  138. return UTTypeConformsTo((__bridge CFStringRef)type, (__bridge CFStringRef)referenceType);
  139. }
  140. + (BOOL)conformsToImageType:(NSString *)uti {
  141. return [UTIConverter type:uti conformsTo:UTTYPE_IMAGE];
  142. }
  143. + (BOOL)conformsToMovieType:(NSString *)uti {
  144. return [UTIConverter type:uti conformsTo:UTTYPE_MOVIE];
  145. }
  146. + (BOOL)isKind:(CFStringRef)type mimeType:(NSString *)mimeType {
  147. CFStringRef MIMEType = (__bridge CFStringRef)mimeType;
  148. CFStringRef UTI = UTTypeCreatePreferredIdentifierForTag(kUTTagClassMIMEType, MIMEType, NULL);
  149. return UTTypeConformsTo(UTI, type);
  150. }
  151. + (UIImage *)getDefaultThumbnailForMimeType:(NSString *)mimeType {
  152. if ([UTIConverter isImageMimeType:mimeType]) {
  153. return [BundleUtil imageNamed:@"ThumbImageFile"];
  154. }
  155. if ([UTIConverter isAudioMimeType:mimeType]) {
  156. return [BundleUtil imageNamed:@"ThumbAudioFile"];
  157. }
  158. if ([UTIConverter isVideoMimeType:mimeType]) {
  159. return [BundleUtil imageNamed:@"ThumbVideoFile"];
  160. }
  161. if ([UTIConverter isPDFMimeType:mimeType]) {
  162. return [BundleUtil imageNamed:@"ThumbPDF"];
  163. }
  164. if ([UTIConverter isContactMimeType:mimeType]) {
  165. return [BundleUtil imageNamed:@"ThumbBusinessContact"];
  166. }
  167. if ([UTIConverter isCalendarMimeType:mimeType]) {
  168. return [BundleUtil imageNamed:@"ThumbCalendar"];
  169. }
  170. if ([UTIConverter isWordMimeType:mimeType]) {
  171. return [BundleUtil imageNamed:@"ThumbWord"];
  172. }
  173. if ([UTIConverter isPowerpointMimeType:mimeType]) {
  174. return [BundleUtil imageNamed:@"ThumbPowerpoint"];
  175. }
  176. if ([UTIConverter isExcelMimeType:mimeType]) {
  177. return [BundleUtil imageNamed:@"ThumbExcel"];
  178. }
  179. if ([UTIConverter isTextMimeType:mimeType]) {
  180. return [BundleUtil imageNamed:@"ThumbDocument"];
  181. }
  182. if ([UTIConverter isArchiveMimeType:mimeType]) {
  183. return [BundleUtil imageNamed:@"ThumbArchive"];
  184. }
  185. // fallback
  186. return [BundleUtil imageNamed:@"ThumbFile"];
  187. }
  188. + (NSString *)localizedDescriptionForMimeType:(NSString *)mimeType {
  189. CFStringRef MIMEType = (__bridge CFStringRef)mimeType;
  190. CFStringRef UTI = UTTypeCreatePreferredIdentifierForTag(kUTTagClassMIMEType, MIMEType, NULL);
  191. CFStringRef description = UTTypeCopyDescription(UTI);
  192. return (__bridge_transfer NSString *)description;
  193. }
  194. @end