Include filename, support sharing all other image types

// FREEBIE
pull/1/head
Michael Kirk 8 years ago
parent 3eceb86371
commit 3036337a5a

@ -382,41 +382,62 @@ public class ShareViewController: UINavigationController, SAELoadViewDelegate, S
} }
// TODO Multiple attachments. In that case I'm unclear if we'll // TODO Multiple attachments. In that case I'm unclear if we'll
// be given multile inputItems or a signle inputItem with multiple attachments. // be given multiple inputItems or a single inputItem with multiple attachments.
guard let itemProvider: NSItemProvider = inputItem.attachments?.first as? NSItemProvider else { guard let itemProvider: NSItemProvider = inputItem.attachments?.first as? NSItemProvider else {
let error = ShareViewControllerError.assertionError(description: "No item provider in input item attachments") let error = ShareViewControllerError.assertionError(description: "No item provider in input item attachments")
return Promise(error: error) return Promise(error: error)
} }
Logger.info("\(self.logTag) attachment: \(itemProvider)") Logger.info("\(self.logTag) attachment: \(itemProvider)")
guard itemProvider.hasItemConformingToTypeIdentifier(kUTTypeJPEG as String) else { // TODO support other utiTypes
let error = ShareViewControllerError.assertionError(description: "only supporting jpegs for now") let utiType = kUTTypeImage as String
guard itemProvider.hasItemConformingToTypeIdentifier(utiType) else {
let error = ShareViewControllerError.assertionError(description: "only supporting images for now")
return Promise(error: error) return Promise(error: error)
} }
let (promise, fulfill, reject) = Promise<UIImage>.pending() let (promise, fulfill, reject) = Promise<URL>.pending()
itemProvider.loadItem(forTypeIdentifier: utiType, options: nil, completionHandler: {
(provider, error) in
// TODO accept other data types
// TODO whitelist attachment types
// TODO coerce when necessary and possible
itemProvider.loadPreviewImage(options: nil,
completionHandler: { (previewImage, error) in
guard error == nil else { guard error == nil else {
reject(error!) reject(error!)
return return
} }
guard let image = previewImage as? UIImage else { guard let url = provider as? URL else {
let unexpectedTypeError = ShareViewControllerError.assertionError(description: "unexpected item type: \(String(describing: previewImage))") let unexpectedTypeError = ShareViewControllerError.assertionError(description: "unexpected item type: \(String(describing: provider))")
reject(unexpectedTypeError) reject(unexpectedTypeError)
return return
} }
fulfill(image) fulfill(url)
}) })
return promise.then { (image: UIImage) in // TODO accept other data types
return SignalAttachment.imageAttachment(image: image, dataUTI: kUTTypeJPEG as String, filename: "from-share-extension-2345") // TODO whitelist attachment types
// TODO coerce when necessary and possible
return promise.then { (url: URL) -> SignalAttachment in
// TODO use lazy DataProvider?
guard let dataSource = DataSourcePath.dataSource(with: url) else {
throw ShareViewControllerError.assertionError(description: "Unable to read attachment data")
}
dataSource.sourceFilename = url.lastPathComponent
// start with base utiType, but it might be something generic like "image"
var specificUTIType = utiType
if url.pathExtension.count > 0 {
// Determine a more specific utiType based on file extension
if let typeExtension = MIMETypeUtil.utiType(forFileExtension: url.pathExtension) {
specificUTIType = typeExtension
}
}
let attachment = SignalAttachment.attachment(dataSource: dataSource, dataUTI: specificUTIType)
return attachment
} }
} }
} }

Loading…
Cancel
Save