mirror of
https://github.com/oxen-io/session-ios.git
synced 2023-12-13 21:30:14 +01:00
Include filename, support sharing all other image types
// FREEBIE
This commit is contained in:
parent
3eceb86371
commit
3036337a5a
1 changed files with 40 additions and 19 deletions
|
@ -382,41 +382,62 @@ public class ShareViewController: UINavigationController, SAELoadViewDelegate, S
|
|||
}
|
||||
|
||||
// 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 {
|
||||
let error = ShareViewControllerError.assertionError(description: "No item provider in input item attachments")
|
||||
return Promise(error: error)
|
||||
}
|
||||
Logger.info("\(self.logTag) attachment: \(itemProvider)")
|
||||
|
||||
guard itemProvider.hasItemConformingToTypeIdentifier(kUTTypeJPEG as String) else {
|
||||
let error = ShareViewControllerError.assertionError(description: "only supporting jpegs for now")
|
||||
// TODO support other utiTypes
|
||||
let utiType = kUTTypeImage as String
|
||||
|
||||
guard itemProvider.hasItemConformingToTypeIdentifier(utiType) else {
|
||||
let error = ShareViewControllerError.assertionError(description: "only supporting images for now")
|
||||
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 {
|
||||
reject(error!)
|
||||
return
|
||||
}
|
||||
|
||||
guard let image = previewImage as? UIImage else {
|
||||
let unexpectedTypeError = ShareViewControllerError.assertionError(description: "unexpected item type: \(String(describing: previewImage))")
|
||||
guard let url = provider as? URL else {
|
||||
let unexpectedTypeError = ShareViewControllerError.assertionError(description: "unexpected item type: \(String(describing: provider))")
|
||||
reject(unexpectedTypeError)
|
||||
return
|
||||
}
|
||||
|
||||
fulfill(image)
|
||||
fulfill(url)
|
||||
})
|
||||
|
||||
return promise.then { (image: UIImage) in
|
||||
return SignalAttachment.imageAttachment(image: image, dataUTI: kUTTypeJPEG as String, filename: "from-share-extension-2345")
|
||||
// TODO accept other data types
|
||||
// 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…
Reference in a new issue