From 48971478dd2d8af38e8adb44a36c2209e7da0a28 Mon Sep 17 00:00:00 2001 From: Michael Kirk Date: Thu, 27 Apr 2017 09:46:51 -0400 Subject: [PATCH] Allow importing of any file that's not a directory // FREEBIE --- Signal/src/AppDelegate.m | 41 +++++++++++++++---- .../ViewControllers/MessagesViewController.m | 11 +++-- 2 files changed, 38 insertions(+), 14 deletions(-) diff --git a/Signal/src/AppDelegate.m b/Signal/src/AppDelegate.m index 984651572..691a455b7 100644 --- a/Signal/src/AppDelegate.m +++ b/Signal/src/AppDelegate.m @@ -293,17 +293,42 @@ static NSString *const kURLHostVerifyPrefix = @"verify"; @"unknown type.")]; return NO; } - NSString *utiType = [MIMETypeUtil utiTypeForFileExtension:fileExtension]; - if (utiType.length < 1) { + + + NSString *utiType; + NSError *typeError; + [url getResourceValue:&utiType forKey:NSURLTypeIdentifierKey error:&typeError]; + if (typeError) { + DDLogError( + @"%@ Determining type of picked document at url: %@ failed with error: %@", self.tag, url, typeError); + OWSAssert(NO); + } + if (!utiType) { + DDLogDebug(@"%@ falling back to default filetype for picked document at url: %@", self.tag, url); + OWSAssert(NO); + utiType = (__bridge NSString *)kUTTypeData; + } + + NSNumber *isDirectory; + NSError *isDirectoryError; + [url getResourceValue:&isDirectory forKey:NSURLIsDirectoryKey error:&isDirectoryError]; + if (isDirectoryError) { + DDLogError(@"%@ Determining if picked document at url: %@ was a directory failed with error: %@", + self.tag, + url, + isDirectoryError); + OWSAssert(NO); + return NO; + } else if ([isDirectory boolValue]) { + DDLogInfo(@"%@ User picked directory at url: %@", self.tag, url); DDLogError(@"Application opened with URL of unknown UTI type: %@", url); - [self showErrorAlertWithTitle: - NSLocalizedString(@"EXPORT_WITH_SIGNAL_ERROR_TITLE", - @"Title for the alert indicating the 'export with signal' attachment had an error.") - message:NSLocalizedString(@"EXPORT_WITH_SIGNAL_ERROR_MESSAGE_UNKNOWN_TYPE", - @"Message for the alert indicating the 'export with signal' file had " - @"unknown type.")]; + [self showErrorAlertWithTitle:NSLocalizedString(@"ATTACHMENT_PICKER_DOCUMENTS_PICKED_DIRECTORY_FAILED_ALERT_TITLE", + @"Alert title when picking a document fails because user picked a directory/bundle") + message:NSLocalizedString(@"ATTACHMENT_PICKER_DOCUMENTS_PICKED_DIRECTORY_FAILED_ALERT_BODY", + @"Alert body when picking a document fails because user picked a directory/bundle")]; return NO; } + NSData *data = [NSData dataWithContentsOfURL:url]; if (!data) { DDLogError(@"Application opened with URL with unloadable content: %@", url); diff --git a/Signal/src/ViewControllers/MessagesViewController.m b/Signal/src/ViewControllers/MessagesViewController.m index dbcd6d42f..b37bf6c12 100644 --- a/Signal/src/ViewControllers/MessagesViewController.m +++ b/Signal/src/ViewControllers/MessagesViewController.m @@ -2216,6 +2216,11 @@ typedef enum : NSUInteger { @"%@ Determining type of picked document at url: %@ failed with error: %@", self.tag, url, typeError); OWSAssert(NO); } + if (!type) { + DDLogDebug(@"%@ falling back to default filetype for picked document at url: %@", self.tag, url); + OWSAssert(NO); + type = (__bridge NSString *)kUTTypeData; + } NSNumber *isDirectory; NSError *isDirectoryError; @@ -2248,12 +2253,6 @@ typedef enum : NSUInteger { return; } - if (!type) { - DDLogDebug(@"%@ falling back to default filetype for picked document at url: %@", self.tag, url); - OWSAssert(NO); - type = (__bridge NSString *)kUTTypeData; - } - NSString *filename = url.lastPathComponent; if (!filename) { DDLogDebug(@"%@ Unable to determine filename from url: %@", self.tag, url);