Refactoring

This commit is contained in:
Justin Mazzocchi 2021-03-04 23:48:40 -08:00
parent ba0ddb949a
commit 2dd3260659
No known key found for this signature in database
GPG key ID: E223E6937AAFB01C

View file

@ -9,6 +9,7 @@ import UniformTypeIdentifiers
enum MediaProcessingError: Error { enum MediaProcessingError: Error {
case invalidMimeType case invalidMimeType
case fileURLNotFound case fileURLNotFound
case imageNotFound
case unsupportedType case unsupportedType
case unableToCreateImageSource case unableToCreateImageSource
case unableToDownsample case unableToDownsample
@ -24,51 +25,16 @@ public extension MediaProcessingService {
let mimeType: String let mimeType: String
let dataPublisher: AnyPublisher<Data, Error> let dataPublisher: AnyPublisher<Data, Error>
if let uniformType = registeredTypes.first(where: { if let type = 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 uploadableMimeTypes.contains(mimeType)
}), let preferredMIMEType = uniformType.preferredMIMEType { }), let preferredMIMEType = type.preferredMIMEType {
mimeType = preferredMIMEType mimeType = preferredMIMEType
dataPublisher = Future<Data, Error> { promise in dataPublisher = fileRepresentationDataPublisher(itemProvider: itemProvider, type: type)
itemProvider.loadFileRepresentation(forTypeIdentifier: uniformType.identifier) { url, error in
if let error = error {
promise(.failure(error))
} else if let url = url {
promise(Result {
if uniformType.conforms(to: .image) && uniformType != .gif {
return try imageData(url: url, type: uniformType)
} else {
return try Data(contentsOf: url)
}
})
} else {
promise(.failure(MediaProcessingError.fileURLNotFound))
}
}
}
.eraseToAnyPublisher()
} else if registeredTypes == [UTType.image], let pngMIMEType = UTType.png.preferredMIMEType { // screenshot } else if registeredTypes == [UTType.image], let pngMIMEType = UTType.png.preferredMIMEType { // screenshot
mimeType = pngMIMEType mimeType = pngMIMEType
dataPublisher = Future<Data, Error> { promise in dataPublisher = UIImagePNGDataPublisher(itemProvider: itemProvider)
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() {
promise(Result {
let url = URL(fileURLWithPath: NSTemporaryDirectory(), isDirectory: true)
.appendingPathComponent(UUID().uuidString)
try data.write(to: url)
return try imageData(url: url, type: .png)
})
} else {
promise(.failure(MediaProcessingError.fileURLNotFound))
}
}
}
.eraseToAnyPublisher()
} else { } else {
return Fail(error: MediaProcessingError.invalidMimeType).eraseToAnyPublisher() return Fail(error: MediaProcessingError.invalidMimeType).eraseToAnyPublisher()
} }
@ -95,6 +61,50 @@ private extension MediaProcessingService {
kCGImageSourceThumbnailMaxPixelSize: 1280 kCGImageSourceThumbnailMaxPixelSize: 1280
] as CFDictionary ] as CFDictionary
static func fileRepresentationDataPublisher(itemProvider: NSItemProvider,
type: UTType) -> AnyPublisher<Data, Error> {
Future<Data, Error> { promise in
itemProvider.loadFileRepresentation(forTypeIdentifier: type.identifier) { url, error in
if let error = error {
promise(.failure(error))
} else if let url = url {
promise(Result {
if type.conforms(to: .image) && type != .gif {
return try imageData(url: url, type: type)
} else {
return try Data(contentsOf: url)
}
})
} else {
promise(.failure(MediaProcessingError.fileURLNotFound))
}
}
}
.eraseToAnyPublisher()
}
static func UIImagePNGDataPublisher(itemProvider: NSItemProvider) -> AnyPublisher<Data, Error> {
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() {
promise(Result {
let url = URL(fileURLWithPath: NSTemporaryDirectory(), isDirectory: true)
.appendingPathComponent(UUID().uuidString)
try data.write(to: url)
return try imageData(url: url, type: .png)
})
} else {
promise(.failure(MediaProcessingError.imageNotFound))
}
}
}
.eraseToAnyPublisher()
}
static func imageData(url: URL, type: UTType) throws -> Data { static func imageData(url: URL, type: UTType) throws -> Data {
guard let source = CGImageSourceCreateWithURL(url as CFURL, Self.imageSourceOptions) else { guard let source = CGImageSourceCreateWithURL(url as CFURL, Self.imageSourceOptions) else {
throw MediaProcessingError.unableToCreateImageSource throw MediaProcessingError.unableToCreateImageSource