Allow pasting and drag/dropping video attachments on compose screen
This commit is contained in:
parent
bebf47f05c
commit
c224d11417
|
@ -27,6 +27,8 @@ final class CompositionAttachment: NSObject, Codable {
|
||||||
}
|
}
|
||||||
|
|
||||||
private let imageType = kUTTypeImage as String
|
private let imageType = kUTTypeImage as String
|
||||||
|
private let mp4Type = kUTTypeMPEG4 as String
|
||||||
|
private let quickTimeType = kUTTypeQuickTimeMovie as String
|
||||||
private let dataType = kUTTypeData as String
|
private let dataType = kUTTypeData as String
|
||||||
|
|
||||||
extension CompositionAttachment: NSItemProviderWriting {
|
extension CompositionAttachment: NSItemProviderWriting {
|
||||||
|
@ -61,7 +63,10 @@ extension CompositionAttachment: NSItemProviderWriting {
|
||||||
|
|
||||||
extension CompositionAttachment: NSItemProviderReading {
|
extension CompositionAttachment: NSItemProviderReading {
|
||||||
static var readableTypeIdentifiersForItemProvider: [String] {
|
static var readableTypeIdentifiersForItemProvider: [String] {
|
||||||
[typeIdentifier] + UIImage.readableTypeIdentifiersForItemProvider + NSURL.readableTypeIdentifiersForItemProvider
|
// todo: is there a better way of handling movies than manually adding all possible UTI types?
|
||||||
|
// just using kUTTypeMovie doesn't work, because we need the actually type in order to get the file extension
|
||||||
|
// without the file extension, getting the thumbnail and exporting the video for attachment upload fails
|
||||||
|
[typeIdentifier] + UIImage.readableTypeIdentifiersForItemProvider + [mp4Type, quickTimeType] + NSURL.readableTypeIdentifiersForItemProvider
|
||||||
}
|
}
|
||||||
|
|
||||||
static func object(withItemProviderData data: Data, typeIdentifier: String) throws -> Self {
|
static func object(withItemProviderData data: Data, typeIdentifier: String) throws -> Self {
|
||||||
|
@ -69,6 +74,13 @@ extension CompositionAttachment: NSItemProviderReading {
|
||||||
return try PropertyListDecoder().decode(Self.self, from: data)
|
return try PropertyListDecoder().decode(Self.self, from: data)
|
||||||
} else if UIImage.readableTypeIdentifiersForItemProvider.contains(typeIdentifier), let image = try? UIImage.object(withItemProviderData: data, typeIdentifier: typeIdentifier) {
|
} else if UIImage.readableTypeIdentifiersForItemProvider.contains(typeIdentifier), let image = try? UIImage.object(withItemProviderData: data, typeIdentifier: typeIdentifier) {
|
||||||
return CompositionAttachment(data: .image(image)) as! Self
|
return CompositionAttachment(data: .image(image)) as! Self
|
||||||
|
} else if typeIdentifier == mp4Type || typeIdentifier == quickTimeType {
|
||||||
|
let temporaryDirectoryURL = URL(fileURLWithPath: NSTemporaryDirectory(), isDirectory: true)
|
||||||
|
let temporaryFileName = ProcessInfo().globallyUniqueString
|
||||||
|
let fileExt = UTTypeCopyPreferredTagWithClass(typeIdentifier as CFString, kUTTagClassFilenameExtension)!
|
||||||
|
let temporaryFileURL = temporaryDirectoryURL.appendingPathComponent(temporaryFileName).appendingPathExtension(fileExt.takeUnretainedValue() as String)
|
||||||
|
try data.write(to: temporaryFileURL)
|
||||||
|
return CompositionAttachment(data: .video(temporaryFileURL)) as! Self
|
||||||
} else if NSURL.readableTypeIdentifiersForItemProvider.contains(typeIdentifier), let url = try? NSURL.object(withItemProviderData: data, typeIdentifier: typeIdentifier) as URL {
|
} else if NSURL.readableTypeIdentifiersForItemProvider.contains(typeIdentifier), let url = try? NSURL.object(withItemProviderData: data, typeIdentifier: typeIdentifier) as URL {
|
||||||
return CompositionAttachment(data: .video(url)) as! Self
|
return CompositionAttachment(data: .video(url)) as! Self
|
||||||
} else {
|
} else {
|
||||||
|
|
Loading…
Reference in New Issue