Ensure media is processed properly
This commit is contained in:
parent
7ff10d999e
commit
5fcc8c780e
|
@ -21,51 +21,59 @@ 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)
|
||||||
|
|
||||||
let uniformType: UTType
|
|
||||||
let mimeType: String
|
let mimeType: String
|
||||||
|
let dataPublisher: AnyPublisher<Data, Error>
|
||||||
|
|
||||||
if let uniformTypeWithUploadableMimeType = registeredTypes.first(where: {
|
if let uniformType = 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 preferredMIMEType = uniformType.preferredMIMEType {
|
||||||
uniformType = uniformTypeWithUploadableMimeType
|
|
||||||
mimeType = preferredMIMEType
|
mimeType = preferredMIMEType
|
||||||
} else if registeredTypes == [UTType.image], let pngMIMEType = UTType.png.preferredMIMEType {
|
dataPublisher = Future<Data, Error> { promise in
|
||||||
uniformType = .image
|
itemProvider.loadFileRepresentation(forTypeIdentifier: uniformType.identifier) { url, error in
|
||||||
|
if let error = error {
|
||||||
|
promise(.failure(error))
|
||||||
|
} else if let url = url {
|
||||||
|
if uniformType.conforms(to: .image) && uniformType != .gif {
|
||||||
|
promise(imageData(url: url, type: uniformType))
|
||||||
|
} else {
|
||||||
|
promise(Result { try Data(contentsOf: url) })
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
promise(.failure(MediaProcessingError.fileURLNotFound))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
.eraseToAnyPublisher()
|
||||||
|
} else if registeredTypes == [UTType.image], let pngMIMEType = UTType.png.preferredMIMEType { // screenshot
|
||||||
mimeType = pngMIMEType
|
mimeType = pngMIMEType
|
||||||
|
dataPublisher = Future<Data, Error> { promise in
|
||||||
|
itemProvider.loadItem(forTypeIdentifier: UTType.image.identifier, options: nil) { item, error in
|
||||||
|
if let error = error {
|
||||||
|
promise(.failure(error))
|
||||||
|
} else if let image = item as? UIImage, let data = image.pngData() {
|
||||||
|
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 {
|
||||||
|
promise(.failure(MediaProcessingError.fileURLNotFound))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
.eraseToAnyPublisher()
|
||||||
} else {
|
} else {
|
||||||
return Fail(error: MediaProcessingError.invalidMimeType).eraseToAnyPublisher()
|
return Fail(error: MediaProcessingError.invalidMimeType).eraseToAnyPublisher()
|
||||||
}
|
}
|
||||||
|
|
||||||
return Future<Data, Error> { promise in
|
return dataPublisher.map { (data: $0, mimeType: mimeType) }.eraseToAnyPublisher()
|
||||||
itemProvider.loadItem(forTypeIdentifier: uniformType.identifier, options: nil) { item, error in
|
|
||||||
if let error = error {
|
|
||||||
promise(.failure(error))
|
|
||||||
} else if let url = item as? URL {
|
|
||||||
if uniformType.conforms(to: .image) && uniformType != .gif {
|
|
||||||
promise(imageData(url: url, type: uniformType))
|
|
||||||
} else {
|
|
||||||
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 {
|
|
||||||
promise(.failure(MediaProcessingError.fileURLNotFound))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
.map { (data: $0, mimeType: mimeType) }
|
|
||||||
.eraseToAnyPublisher()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue