FileMessage.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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 "FileMessage.h"
  21. #import "ImageData.h"
  22. #import "UTIConverter.h"
  23. #import "FileMessageKeys.h"
  24. #import "NSString+Hex.h"
  25. #import "BundleUtil.h"
  26. #ifdef DEBUG
  27. static const DDLogLevel ddLogLevel = DDLogLevelVerbose;
  28. #else
  29. static const DDLogLevel ddLogLevel = DDLogLevelWarning;
  30. #endif
  31. @implementation FileMessage
  32. @dynamic encryptionKey;
  33. @dynamic blobId;
  34. @dynamic blobThumbnailId;
  35. @dynamic fileName;
  36. @dynamic fileSize;
  37. @dynamic progress;
  38. @dynamic type;
  39. @dynamic mimeType;
  40. @dynamic data;
  41. @dynamic thumbnail;
  42. @dynamic json;
  43. @synthesize caption;
  44. @synthesize correlationId;
  45. @synthesize mimeTypeThumbnail;
  46. @synthesize duration;
  47. @synthesize height;
  48. @synthesize width;
  49. - (NSString *)fileName {
  50. NSString *name = [self primitiveValueForKey:@"fileName"];
  51. if (name) {
  52. return name;
  53. } else {
  54. return self.mimeType;
  55. }
  56. }
  57. - (NSString*)logFileName {
  58. NSString *name = [self blobGetFilename];
  59. if ([self blobGetData] == nil) {
  60. name = [name stringByAppendingFormat:@" %@", [NSString stringWithFormat:@" %@", [BundleUtil localizedStringForKey:@"fileNotDownloaded"]]];
  61. }
  62. return name;
  63. }
  64. - (NSString*)logText {
  65. NSString *logCaption = [NSString string];
  66. if (self.caption != nil) {
  67. logCaption = [logCaption stringByAppendingFormat:@" %@", [NSString stringWithFormat:@"%@ %@", [BundleUtil localizedStringForKey:@"caption"], self.caption]];
  68. }
  69. return [NSString stringWithFormat:@"%@: %@%@", [BundleUtil localizedStringForKey:@"file"], [self logFileName], logCaption];
  70. }
  71. - (NSString*)previewText {
  72. if (self.type.intValue == 0 && self.fileName) {
  73. return self.fileName;
  74. }
  75. NSString *fileTypeDescriptionText = [self fileTypeDescriptionText];
  76. if (fileTypeDescriptionText != nil) {
  77. return fileTypeDescriptionText;
  78. }
  79. return [BundleUtil localizedStringForKey:@"file"];
  80. }
  81. - (NSString *)fileTypeDescriptionText {
  82. if ([UTIConverter isGifMimeType:self.mimeType]) {
  83. return [NSString stringWithFormat:@"%@", [BundleUtil localizedStringForKey:@"gif"]];
  84. } else if ([UTIConverter isImageMimeType:self.mimeType]) {
  85. if (self.type.intValue == 2) {
  86. return [NSString stringWithFormat:@"%@", [BundleUtil localizedStringForKey:@"sticker"]];
  87. }
  88. return [NSString stringWithFormat:@"%@", [BundleUtil localizedStringForKey:@"image"]];
  89. } else if ([UTIConverter isVideoMimeType:self.mimeType] || [UTIConverter isMovieMimeType:self.mimeType]) {
  90. return [NSString stringWithFormat:@"%@", [BundleUtil localizedStringForKey:@"video"]];
  91. } else if ([UTIConverter isAudioMimeType:self.mimeType]) {
  92. return [NSString stringWithFormat:@"%@", [BundleUtil localizedStringForKey:@"audio"]];
  93. }
  94. return nil;
  95. }
  96. - (NSData *)blobGetData {
  97. if (self.data) {
  98. return self.data.data;
  99. }
  100. return nil;
  101. }
  102. - (NSData *)blobGetId {
  103. return self.blobId;
  104. }
  105. - (NSData *)blobGetEncryptionKey {
  106. return self.encryptionKey;
  107. }
  108. - (NSNumber *)blobGetSize {
  109. return self.fileSize;
  110. }
  111. - (void)blobSetData:(NSData *)data {
  112. FileData *dbData = [NSEntityDescription
  113. insertNewObjectForEntityForName:@"FileData"
  114. inManagedObjectContext:self.managedObjectContext];
  115. dbData.data = data;
  116. self.data = dbData;
  117. }
  118. - (NSData *)blobGetThumbnail {
  119. if (self.thumbnail) {
  120. return self.thumbnail.data;
  121. }
  122. return nil;
  123. }
  124. - (NSString *)blobGetUTI {
  125. return [UTIConverter utiFromMimeType:self.mimeType];
  126. }
  127. - (NSString *)blobGetFilename {
  128. return [NSString stringWithFormat:@"%@-%@", [NSString stringWithHexData:self.id], self.fileName];
  129. }
  130. - (NSString *)blobGetWebFilename {
  131. return self.fileName;
  132. }
  133. - (void)blobUpdateProgress:(NSNumber *)progress {
  134. self.progress = progress;
  135. }
  136. - (NSNumber *)blobGetProgress {
  137. return self.progress;
  138. }
  139. - (NSString *)getCaption {
  140. if (self.json == nil) {
  141. return nil;
  142. }
  143. NSError *error;
  144. NSData *jsonData = [self.json dataUsingEncoding:NSUTF8StringEncoding];
  145. NSDictionary *json = (NSDictionary *)[NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];
  146. if (json == nil) {
  147. DDLogError(@"Error parsing json data %@, %@", error, [error userInfo]);
  148. return nil;
  149. }
  150. NSString *description = [json objectForKey:JSON_FILE_KEY_DESCRIPTION];
  151. return description;
  152. }
  153. - (NSURL *)tmpURL:(NSString *)tmpFileName {
  154. NSURL *tmpFileUrl = nil;
  155. NSURL *tmpDirUrl = [NSURL fileURLWithPath: NSTemporaryDirectory() isDirectory:YES];
  156. NSString *fileName = nil;
  157. if (self.fileName) {
  158. // Sanitize file name by stripping slashes
  159. fileName = self.fileName;
  160. NSURL *tmp = [tmpDirUrl URLByAppendingPathComponent:[self.fileName stringByReplacingOccurrencesOfString:@"/" withString:@""]];
  161. tmpFileUrl = tmp;
  162. }
  163. if (fileName == nil) {
  164. fileName = [self getFilename];
  165. }
  166. if (fileName != nil) {
  167. NSString *extension = [UTIConverter preferedFileExtensionForMimeType:self.mimeType];
  168. if (extension == nil) {
  169. extension = @"";
  170. }
  171. NSURL *tmp = [[tmpDirUrl URLByAppendingPathComponent:fileName] URLByAppendingPathExtension: extension];
  172. tmpFileUrl = tmp;
  173. }
  174. if (tmpFileUrl == nil) {
  175. NSString *extension = [UTIConverter preferedFileExtensionForMimeType:self.mimeType];
  176. if (extension == nil) {
  177. extension = @"";
  178. }
  179. tmpFileUrl = [[tmpDirUrl URLByAppendingPathComponent:tmpFileName] URLByAppendingPathExtension: extension];
  180. }
  181. return tmpFileUrl;
  182. }
  183. /// Exports the data for this message to url. Will overwrite any data at that URL
  184. /// @param url
  185. - (void)exportDataToURL:(NSURL *)url {
  186. NSData *data = [self blobGetData];
  187. if (![data writeToURL:url atomically:NO]) {
  188. DDLogWarn(@"Writing file data to temporary file failed");
  189. }
  190. }
  191. - (NSString *)mimeTypeThumbnail {
  192. if (self.json != nil) {
  193. NSError *error;
  194. NSData *jsonData = [self.json dataUsingEncoding:NSUTF8StringEncoding];
  195. NSDictionary *json = (NSDictionary *)[NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];
  196. if (json == nil) {
  197. DDLogError(@"Error parsing json data %@, %@", error, [error userInfo]);
  198. } else {
  199. NSString *mTT = [json objectForKey:JSON_FILE_KEY_MIMETYPETHUMBNAIL];
  200. if (mTT != nil) {
  201. return mTT;
  202. }
  203. }
  204. }
  205. return @"image/jpeg";
  206. }
  207. #pragma mark ExternalStorageInfo
  208. - (NSString *)getFilename {
  209. return self.data != nil ? [self getFilename:self.data.data] : nil;
  210. }
  211. - (NSString *)getThumbnailname {
  212. return self.thumbnail != nil ? [self getFilename:self.thumbnail.data] : nil;
  213. }
  214. - (NSString *)getFilename:(NSData *)ofData {
  215. if (ofData != nil && [ofData respondsToSelector:NSSelectorFromString(@"filename")]) {
  216. return [ofData performSelector:NSSelectorFromString(@"filename")];
  217. }
  218. return nil;
  219. }
  220. - (BOOL)renderFileImageMessage {
  221. if (self.type.intValue == 1 || self.type.intValue == 2) {
  222. if ([UTIConverter isImageMimeType:self.mimeType]) {
  223. return [UTIConverter isRenderingImageMimeType:self.mimeType];
  224. }
  225. }
  226. return false;
  227. }
  228. - (BOOL)renderFileVideoMessage {
  229. if (self.type.intValue == 1 || self.type.intValue == 2) {
  230. return [UTIConverter isRenderingVideoMimeType:self.mimeType];
  231. }
  232. return false;
  233. }
  234. - (BOOL)renderFileAudioMessage {
  235. if (self.type.intValue == 1 || self.type.intValue == 2) {
  236. return [UTIConverter isRenderingAudioMimeType:self.mimeType];
  237. }
  238. return false;
  239. }
  240. - (BOOL)renderMediaFileMessage {
  241. return self.type.intValue == 1;
  242. }
  243. - (BOOL)renderStickerFileMessage {
  244. return self.type.intValue == 2;
  245. }
  246. - (BOOL)renderFileGifMessage {
  247. if (self.type.intValue == 1 || self.type.intValue == 2) {
  248. return [UTIConverter isGifMimeType:self.mimeType];
  249. }
  250. return false;
  251. }
  252. - (BOOL)sendAsFileImageMessage {
  253. if ([UTIConverter isImageMimeType:self.mimeType]) {
  254. return [UTIConverter isRenderingImageMimeType:self.mimeType];
  255. }
  256. return false;
  257. }
  258. - (BOOL)sendAsFileVideoMessage {
  259. if ([UTIConverter isMovieMimeType:self.mimeType]) {
  260. return [UTIConverter isRenderingVideoMimeType:self.mimeType];
  261. }
  262. return false;
  263. }
  264. - (BOOL)sendAsFileAudioMessage {
  265. // if (self.type.intValue == 0) {
  266. // return false;
  267. // }
  268. // return true;
  269. return false;
  270. }
  271. - (BOOL)sendAsFileGifMessage {
  272. if ([UTIConverter isGifMimeType:self.mimeType]) {
  273. return true;
  274. }
  275. return false;
  276. }
  277. - (BOOL)shouldShowCaption {
  278. if (self.type.intValue == 2) {
  279. return false;
  280. }
  281. return true;
  282. }
  283. - (NSNumber *)getDuration {
  284. if (self.json != nil) {
  285. NSError *error;
  286. NSData *jsonData = [self.json dataUsingEncoding:NSUTF8StringEncoding];
  287. NSDictionary *json = (NSDictionary *)[NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];
  288. if (json == nil) {
  289. DDLogError(@"Error parsing json data %@, %@", error, [error userInfo]);
  290. } else {
  291. NSDictionary *meta = [json objectForKey:JSON_FILE_KEY_METADATA];
  292. if (meta != nil) {
  293. float durationFloat = [[meta objectForKey:JSON_FILE_KEY_METADATA_DURATION] floatValue];
  294. NSNumber *duration = [[NSNumber alloc] initWithFloat:durationFloat];
  295. return duration;
  296. }
  297. }
  298. }
  299. return self.duration;
  300. }
  301. - (NSNumber *)getHeight {
  302. if (self.json != nil) {
  303. NSError *error;
  304. NSData *jsonData = [self.json dataUsingEncoding:NSUTF8StringEncoding];
  305. NSDictionary *json = (NSDictionary *)[NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];
  306. if (json == nil) {
  307. DDLogError(@"Error parsing json data %@, %@", error, [error userInfo]);
  308. } else {
  309. NSDictionary *meta = [json objectForKey:JSON_FILE_KEY_METADATA];
  310. if (meta != nil) {
  311. NSNumber *height = [meta objectForKey:JSON_FILE_KEY_METADATA_HEIGHT];
  312. return height;
  313. }
  314. }
  315. }
  316. return @0;
  317. }
  318. - (NSNumber *)getWidth {
  319. if (self.json != nil) {
  320. NSError *error;
  321. NSData *jsonData = [self.json dataUsingEncoding:NSUTF8StringEncoding];
  322. NSDictionary *json = (NSDictionary *)[NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];
  323. if (json == nil) {
  324. DDLogError(@"Error parsing json data %@, %@", error, [error userInfo]);
  325. } else {
  326. NSDictionary *meta = [json objectForKey:JSON_FILE_KEY_METADATA];
  327. if (meta != nil) {
  328. NSNumber *width = [meta objectForKey:JSON_FILE_KEY_METADATA_WIDTH];
  329. return width;
  330. }
  331. }
  332. }
  333. return @0;
  334. }
  335. - (NSString *)quotePreviewText {
  336. NSString *quoteCaption = [self getCaption];
  337. if (!quoteCaption) {
  338. return @"";
  339. }
  340. return quoteCaption;
  341. }
  342. #ifdef DEBUG
  343. #else
  344. - (NSString *)debugDescription
  345. {
  346. return [NSString stringWithFormat:@"%@ <%@>: %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@", self.class, self, @"encryptionKey", @"*****", @"blobId", @"*****", @"blobThumbnailId = ", @"****", @"fileName = ", self.fileName.description, @"progress = ", self.progress.description, @"type = ", self.type.description, @"mimeType = ", self.mimeType.description, @"data = ", self.data.description, @"thumbnail", self.thumbnail.description, @"json = ", @"*****"];
  347. }
  348. #endif
  349. @end