Refactoring

This commit is contained in:
Justin Mazzocchi 2021-03-04 23:36:17 -08:00
parent 5fcc8c780e
commit ba0ddb949a
No known key found for this signature in database
GPG key ID: E223E6937AAFB01C

View file

@ -35,11 +35,13 @@ public extension MediaProcessingService {
if let error = error { if let error = error {
promise(.failure(error)) promise(.failure(error))
} else if let url = url { } else if let url = url {
if uniformType.conforms(to: .image) && uniformType != .gif { promise(Result {
promise(imageData(url: url, type: uniformType)) if uniformType.conforms(to: .image) && uniformType != .gif {
} else { return try imageData(url: url, type: uniformType)
promise(Result { try Data(contentsOf: url) }) } else {
} return try Data(contentsOf: url)
}
})
} else { } else {
promise(.failure(MediaProcessingError.fileURLNotFound)) promise(.failure(MediaProcessingError.fileURLNotFound))
} }
@ -53,16 +55,14 @@ public extension MediaProcessingService {
if let error = error { if let error = error {
promise(.failure(error)) promise(.failure(error))
} else if let image = item as? UIImage, let data = image.pngData() { } else if let image = item as? UIImage, let data = image.pngData() {
do { promise(Result {
let url = URL(fileURLWithPath: NSTemporaryDirectory(), isDirectory: true) let url = URL(fileURLWithPath: NSTemporaryDirectory(), isDirectory: true)
.appendingPathComponent(UUID().uuidString) .appendingPathComponent(UUID().uuidString)
try data.write(to: url) try data.write(to: url)
promise(imageData(url: url, type: .png)) return try imageData(url: url, type: .png)
} catch { })
promise(.failure(error))
}
} else { } else {
promise(.failure(MediaProcessingError.fileURLNotFound)) promise(.failure(MediaProcessingError.fileURLNotFound))
} }
@ -95,24 +95,24 @@ private extension MediaProcessingService {
kCGImageSourceThumbnailMaxPixelSize: 1280 kCGImageSourceThumbnailMaxPixelSize: 1280
] as CFDictionary ] as CFDictionary
static func imageData(url: URL, type: UTType) -> Result<Data, Error> { 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 {
return .failure(MediaProcessingError.unableToCreateImageSource) throw MediaProcessingError.unableToCreateImageSource
} }
guard let image = CGImageSourceCreateThumbnailAtIndex(source, 0, thumbnailOptions) else { guard let image = CGImageSourceCreateThumbnailAtIndex(source, 0, thumbnailOptions) else {
return .failure(MediaProcessingError.unableToDownsample) throw MediaProcessingError.unableToDownsample
} }
let data = NSMutableData() let data = NSMutableData()
guard let imageDestination = CGImageDestinationCreateWithData(data, type.identifier as CFString, 1, nil) else { guard let imageDestination = CGImageDestinationCreateWithData(data, type.identifier as CFString, 1, nil) else {
return .failure(MediaProcessingError.unableToCreateImageDataDestination) throw MediaProcessingError.unableToCreateImageDataDestination
} }
CGImageDestinationAddImage(imageDestination, image, nil) CGImageDestinationAddImage(imageDestination, image, nil)
CGImageDestinationFinalize(imageDestination) CGImageDestinationFinalize(imageDestination)
return .success(data as Data) return data as Data
} }
} }