mirror of
https://github.com/metabolist/metatext.git
synced 2024-11-22 16:21:00 +00:00
Fix sharing screenshots
This commit is contained in:
parent
92e515e2b4
commit
7a28cf90b0
1 changed files with 25 additions and 5 deletions
|
@ -3,6 +3,7 @@
|
||||||
import Combine
|
import Combine
|
||||||
import Foundation
|
import Foundation
|
||||||
import ImageIO
|
import ImageIO
|
||||||
|
import UIKit
|
||||||
import UniformTypeIdentifiers
|
import UniformTypeIdentifiers
|
||||||
|
|
||||||
enum MediaProcessingError: Error {
|
enum MediaProcessingError: Error {
|
||||||
|
@ -20,25 +21,44 @@ public extension MediaProcessingService {
|
||||||
static func dataAndMimeType(itemProvider: NSItemProvider) -> AnyPublisher<(data: Data, mimeType: String), Error> {
|
static func dataAndMimeType(itemProvider: NSItemProvider) -> AnyPublisher<(data: Data, mimeType: String), Error> {
|
||||||
let registeredTypes = itemProvider.registeredTypeIdentifiers.compactMap(UTType.init)
|
let registeredTypes = itemProvider.registeredTypeIdentifiers.compactMap(UTType.init)
|
||||||
|
|
||||||
guard let uniformType = registeredTypes.first(where: {
|
let uniformType: UTType
|
||||||
|
let mimeType: String
|
||||||
|
|
||||||
|
if let uniformTypeWithUploadableMimeType = registeredTypes.first(where: {
|
||||||
guard let mimeType = $0.preferredMIMEType else { return false }
|
guard let mimeType = $0.preferredMIMEType else { return false }
|
||||||
|
|
||||||
return Self.uploadableMimeTypes.contains(mimeType)
|
return Self.uploadableMimeTypes.contains(mimeType)
|
||||||
}),
|
}), let preferredMIMEType = uniformTypeWithUploadableMimeType.preferredMIMEType {
|
||||||
let mimeType = uniformType.preferredMIMEType else {
|
uniformType = uniformTypeWithUploadableMimeType
|
||||||
|
mimeType = preferredMIMEType
|
||||||
|
} else if registeredTypes == [UTType.image], let pngMIMEType = UTType.png.preferredMIMEType {
|
||||||
|
uniformType = .image
|
||||||
|
mimeType = pngMIMEType
|
||||||
|
} else {
|
||||||
return Fail(error: MediaProcessingError.invalidMimeType).eraseToAnyPublisher()
|
return Fail(error: MediaProcessingError.invalidMimeType).eraseToAnyPublisher()
|
||||||
}
|
}
|
||||||
|
|
||||||
return Future<Data, Error> { promise in
|
return Future<Data, Error> { promise in
|
||||||
itemProvider.loadFileRepresentation(forTypeIdentifier: uniformType.identifier) { url, error in
|
itemProvider.loadItem(forTypeIdentifier: uniformType.identifier, options: nil) { item, error in
|
||||||
if let error = error {
|
if let error = error {
|
||||||
promise(.failure(error))
|
promise(.failure(error))
|
||||||
} else if let url = url {
|
} else if let url = item as? URL {
|
||||||
if uniformType.conforms(to: .image) && uniformType != .gif {
|
if uniformType.conforms(to: .image) && uniformType != .gif {
|
||||||
promise(imageData(url: url, type: uniformType))
|
promise(imageData(url: url, type: uniformType))
|
||||||
} else {
|
} else {
|
||||||
promise(Result { try Data(contentsOf: url) })
|
promise(Result { try Data(contentsOf: url) })
|
||||||
}
|
}
|
||||||
|
} else if let image = item as? UIImage, let data = image.pngData() { // screenshots
|
||||||
|
do {
|
||||||
|
let url = URL(fileURLWithPath: NSTemporaryDirectory(), isDirectory: true)
|
||||||
|
.appendingPathComponent(UUID().uuidString)
|
||||||
|
|
||||||
|
try data.write(to: url)
|
||||||
|
|
||||||
|
promise(imageData(url: url, type: .png))
|
||||||
|
} catch {
|
||||||
|
promise(.failure(error))
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
promise(.failure(MediaProcessingError.fileURLNotFound))
|
promise(.failure(MediaProcessingError.fileURLNotFound))
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue