From 1b820c48c645200b8a3cb39c4527ce579d17be2b Mon Sep 17 00:00:00 2001 From: Morgan Pretty Date: Mon, 14 Feb 2022 10:02:33 +1100 Subject: [PATCH] Added support for Microsoft Word, Excel & Powerpoint previews --- .../ConversationVC+Interaction.swift | 6 +++- .../Attachments/TSAttachment.h | 1 + .../Attachments/TSAttachment.m | 4 +++ SessionUtilitiesKit/Media/MIMETypeUtil.h | 1 + SessionUtilitiesKit/Media/MIMETypeUtil.m | 36 +++++++++++++++++++ 5 files changed, 47 insertions(+), 1 deletion(-) diff --git a/Session/Conversations/ConversationVC+Interaction.swift b/Session/Conversations/ConversationVC+Interaction.swift index 823423680..a802e285b 100644 --- a/Session/Conversations/ConversationVC+Interaction.swift +++ b/Session/Conversations/ConversationVC+Interaction.swift @@ -472,7 +472,11 @@ extension ConversationVC : InputViewDelegate, MessageCellDelegate, ContextMenuAc Storage.shared.getContact(with: thread.contactSessionID())?.isTrusted != true { confirmDownload() } - else if (viewItem.attachmentStream?.isText == true || viewItem.attachmentStream?.contentType == OWSMimeTypeApplicationPdf), let filePathString: String = viewItem.attachmentStream?.originalFilePath { + else if ( + viewItem.attachmentStream?.isText == true || + viewItem.attachmentStream?.isMicrosoftDoc == true || + viewItem.attachmentStream?.contentType == OWSMimeTypeApplicationPdf + ), let filePathString: String = viewItem.attachmentStream?.originalFilePath { let fileUrl: URL = URL(fileURLWithPath: filePathString) let interactionController: UIDocumentInteractionController = UIDocumentInteractionController(url: fileUrl) interactionController.delegate = self diff --git a/SessionMessagingKit/Sending & Receiving/Attachments/TSAttachment.h b/SessionMessagingKit/Sending & Receiving/Attachments/TSAttachment.h index b9ab550f4..d8f0d8936 100644 --- a/SessionMessagingKit/Sending & Receiving/Attachments/TSAttachment.h +++ b/SessionMessagingKit/Sending & Receiving/Attachments/TSAttachment.h @@ -85,6 +85,7 @@ typedef NS_ENUM(NSUInteger, TSAttachmentType) { @property (nonatomic, readonly) BOOL isVoiceMessage; @property (nonatomic, readonly) BOOL isVisualMedia; @property (nonatomic, readonly) BOOL isText; +@property (nonatomic, readonly) BOOL isMicrosoftDoc; @property (nonatomic, readonly) BOOL isOversizeText; + (NSString *)emojiForMimeType:(NSString *)contentType; diff --git a/SessionMessagingKit/Sending & Receiving/Attachments/TSAttachment.m b/SessionMessagingKit/Sending & Receiving/Attachments/TSAttachment.m index 601eec0f9..f3722bd80 100644 --- a/SessionMessagingKit/Sending & Receiving/Attachments/TSAttachment.m +++ b/SessionMessagingKit/Sending & Receiving/Attachments/TSAttachment.m @@ -241,6 +241,10 @@ NSUInteger const TSAttachmentSchemaVersion = 4; return [MIMETypeUtil isText:self.contentType]; } +- (BOOL)isMicrosoftDoc { + return [MIMETypeUtil isMicrosoftDoc:self.contentType]; +} + - (BOOL)isOversizeText { return [self.contentType isEqualToString:OWSMimeTypeOversizeTextMessage]; diff --git a/SessionUtilitiesKit/Media/MIMETypeUtil.h b/SessionUtilitiesKit/Media/MIMETypeUtil.h index 2093b5bf3..6b09c6437 100644 --- a/SessionUtilitiesKit/Media/MIMETypeUtil.h +++ b/SessionUtilitiesKit/Media/MIMETypeUtil.h @@ -42,6 +42,7 @@ extern NSString *const kSyncMessageFileExtension; + (BOOL)isVideo:(NSString *)contentType; + (BOOL)isAudio:(NSString *)contentType; + (BOOL)isText:(NSString *)contentType; ++ (BOOL)isMicrosoftDoc:(NSString *)contentType; + (BOOL)isVisualMedia:(NSString *)contentType; // filename is optional and should not be trusted. diff --git a/SessionUtilitiesKit/Media/MIMETypeUtil.m b/SessionUtilitiesKit/Media/MIMETypeUtil.m index fb4d15df4..d1aa2ce6c 100644 --- a/SessionUtilitiesKit/Media/MIMETypeUtil.m +++ b/SessionUtilitiesKit/Media/MIMETypeUtil.m @@ -290,6 +290,42 @@ NSString *const kSyncMessageFileExtension = @"bin"; ]; } ++ (BOOL)isMicrosoftDoc:(NSString *)contentType { + return [ + @[ + // Word files + @"application/msword", + + @"application/vnd.openxmlformats-officedocument.wordprocessingml.document", + @"application/vnd.openxmlformats-officedocument.wordprocessingml.template", + @"application/vnd.ms-word.document.macroEnabled.12", + @"application/vnd.ms-word.template.macroEnabled.12", + + // Excel files + @"application/vnd.ms-excel", + + @"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", + @"application/vnd.openxmlformats-officedocument.spreadsheetml.template", + @"application/vnd.ms-excel.sheet.macroEnabled.12", + @"application/vnd.ms-excel.template.macroEnabled.12", + @"application/vnd.ms-excel.addin.macroEnabled.12", + @"application/vnd.ms-excel.sheet.binary.macroEnabled.12", + + // Powerpoint files + @"application/vnd.ms-powerpoint", + + @"application/vnd.openxmlformats-officedocument.presentationml.presentation", + @"application/vnd.openxmlformats-officedocument.presentationml.template", + @"application/vnd.openxmlformats-officedocument.presentationml.slideshow", + @"application/vnd.ms-powerpoint.addin.macroEnabled.12", + @"application/vnd.ms-powerpoint.presentation.macroEnabled.12", + @"application/vnd.ms-powerpoint.template.macroEnabled.12", + @"application/vnd.ms-powerpoint.slideshow.macroEnabled.12" + ] + containsObject:contentType + ]; +} + + (BOOL)isVisualMedia:(NSString *)contentType { if ([self isImage:contentType]) {