IceCubesApp/Packages/Status/Sources/Status/Editor/Components/StatusEditorUTTypeSupported.swift

220 lines
6.5 KiB
Swift
Raw Normal View History

2023-01-27 19:36:40 +00:00
import AVFoundation
2023-01-15 15:39:08 +00:00
import Foundation
import PhotosUI
import SwiftUI
2023-01-17 10:36:01 +00:00
import UIKit
import UniformTypeIdentifiers
2023-01-15 15:39:08 +00:00
@MainActor
enum StatusEditorUTTypeSupported: String, CaseIterable {
case url = "public.url"
case text = "public.text"
2023-01-15 17:30:25 +00:00
case plaintext = "public.plain-text"
2023-01-15 15:39:08 +00:00
case image = "public.image"
case jpeg = "public.jpeg"
case png = "public.png"
case tiff = "public.tiff"
2023-02-26 05:45:57 +00:00
case video = "public.video"
case movie = "public.movie"
case mp4 = "public.mpeg-4"
case gif = "public.gif"
case gif2 = "com.compuserve.gif"
2023-01-26 17:50:05 +00:00
case quickTimeMovie = "com.apple.quicktime-movie"
2023-02-26 05:45:57 +00:00
2023-02-15 05:49:53 +00:00
case uiimage = "com.apple.uikit.image"
2023-02-26 05:45:57 +00:00
// Have to implement this manually here due to compiler not implicitly
// inserting `nonisolated`, which leads to a warning:
//
// Main actor-isolated static property 'allCases' cannot be used to
// satisfy nonisolated protocol requirement
//
nonisolated public static var allCases: [StatusEditorUTTypeSupported] {
[.url, .text, .plaintext, .image, .jpeg, .png, .tiff, .video,
.movie, .mp4, .gif, .gif2, .quickTimeMovie, .uiimage]
}
2023-01-22 05:38:30 +00:00
static func types() -> [UTType] {
[.url, .text, .plainText, .image, .jpeg, .png, .tiff, .video, .mpeg4Movie, .gif, .movie, .quickTimeMovie]
}
2023-02-26 05:45:57 +00:00
var isVideo: Bool {
switch self {
case .video, .movie, .mp4, .quickTimeMovie:
return true
default:
return false
}
}
2023-02-26 05:45:57 +00:00
var isGif: Bool {
switch self {
case .gif, .gif2:
return true
default:
return false
}
}
2023-02-26 05:45:57 +00:00
2023-01-15 15:39:08 +00:00
func loadItemContent(item: NSItemProvider) async throws -> Any? {
// Many warnings here about non-sendable type `[AnyHashable: Any]?` crossing
// actor boundaries. Many Radars have been filed.
2023-01-15 15:39:08 +00:00
let result = try await item.loadItem(forTypeIdentifier: rawValue)
if isVideo, let transferable = await getVideoTransferable(item: item) {
return transferable
} else if isGif, let transferable = await getGifTransferable(item: item) {
return transferable
}
2023-02-15 05:49:53 +00:00
if self == .jpeg || self == .png || self == .tiff || self == .image || self == .uiimage {
if let image = result as? UIImage {
return image
} else if let imageURL = result as? URL,
2023-02-18 06:26:48 +00:00
let data = try? Data(contentsOf: imageURL),
let image = UIImage(data: data)
2023-02-12 15:29:41 +00:00
{
return image
} else if let data = result as? Data,
2023-02-12 15:29:41 +00:00
let image = UIImage(data: data)
{
return image
2023-02-12 15:29:41 +00:00
} else if let transferable = await getImageTansferable(item: item) {
return transferable
}
2023-01-15 15:39:08 +00:00
}
if let url = result as? URL {
return url.absoluteString
} else if let text = result as? String {
return text
} else if let image = result as? UIImage {
return image
} else {
return nil
}
}
2023-02-26 05:45:57 +00:00
private func getVideoTransferable(item: NSItemProvider) async -> MovieFileTranseferable? {
return await withCheckedContinuation { continuation in
_ = item.loadTransferable(type: MovieFileTranseferable.self) { result in
switch result {
case let .success(success):
continuation.resume(with: .success(success))
case .failure:
continuation.resume(with: .success(nil))
}
}
}
}
2023-02-26 05:45:57 +00:00
private func getGifTransferable(item: NSItemProvider) async -> GifFileTranseferable? {
return await withCheckedContinuation { continuation in
_ = item.loadTransferable(type: GifFileTranseferable.self) { result in
switch result {
case let .success(success):
continuation.resume(with: .success(success))
case .failure:
continuation.resume(with: .success(nil))
}
}
}
}
2023-02-26 05:45:57 +00:00
private func getImageTansferable(item: NSItemProvider) async -> ImageFileTranseferable? {
return await withCheckedContinuation { continuation in
_ = item.loadTransferable(type: ImageFileTranseferable.self) { result in
switch result {
case let .success(success):
continuation.resume(with: .success(success))
case .failure:
continuation.resume(with: .success(nil))
}
}
}
}
}
struct MovieFileTranseferable: Transferable {
private let url: URL
var compressedVideoURL: URL? {
get async {
2023-01-27 19:36:40 +00:00
await withCheckedContinuation { continuation in
let urlAsset = AVURLAsset(url: url, options: nil)
2023-02-10 17:49:03 +00:00
guard let exportSession = AVAssetExportSession(asset: urlAsset, presetName: AVAssetExportPreset1920x1080) else {
continuation.resume(returning: nil)
return
}
let outputURL = URL.temporaryDirectory.appending(path: "\(UUID().uuidString).\(url.pathExtension)")
exportSession.outputURL = outputURL
exportSession.outputFileType = .mp4
exportSession.shouldOptimizeForNetworkUse = true
2023-01-27 19:36:40 +00:00
exportSession.exportAsynchronously { () in
continuation.resume(returning: outputURL)
}
}
}
}
2023-02-26 05:45:57 +00:00
static var transferRepresentation: some TransferRepresentation {
FileRepresentation(contentType: .movie) { movie in
SentTransferredFile(movie.url)
} importing: { received in
2023-02-12 15:29:41 +00:00
Self(url: localURLFor(received: received))
}
}
}
struct ImageFileTranseferable: Transferable {
let url: URL
2023-02-26 05:45:57 +00:00
lazy var data: Data? = try? Data(contentsOf: url)
lazy var image: UIImage? = UIImage(data: data ?? Data())
2023-02-26 05:45:57 +00:00
static var transferRepresentation: some TransferRepresentation {
FileRepresentation(contentType: .image) { image in
SentTransferredFile(image.url)
} importing: { received in
2023-02-12 15:29:41 +00:00
Self(url: localURLFor(received: received))
}
}
}
struct GifFileTranseferable: Transferable {
let url: URL
2023-02-26 05:45:57 +00:00
var data: Data? {
try? Data(contentsOf: url)
}
2023-02-26 05:45:57 +00:00
static var transferRepresentation: some TransferRepresentation {
FileRepresentation(contentType: .gif) { gif in
SentTransferredFile(gif.url)
} importing: { received in
2023-02-12 15:29:41 +00:00
Self(url: localURLFor(received: received))
}
}
}
2023-02-12 15:29:41 +00:00
private func localURLFor(received: ReceivedTransferredFile) -> URL {
let copy = URL.temporaryDirectory.appending(path: "\(UUID().uuidString).\(received.file.pathExtension)")
try? FileManager.default.copyItem(at: received.file, to: copy)
return copy
}
public extension URL {
func mimeType() -> String {
if let mimeType = UTType(filenameExtension: pathExtension)?.preferredMIMEType {
return mimeType
} else {
return "application/octet-stream"
}
}
2023-01-15 15:39:08 +00:00
}
extension UIImage {
func resized(to size: CGSize) -> UIImage {
UIGraphicsImageRenderer(size: size).image { _ in
draw(in: CGRect(origin: .zero, size: size))
}
}
}