2021-03-18 12:42:26 +01:00
|
|
|
//
|
|
|
|
// MastodonAttachmentService.swift
|
|
|
|
// Mastodon
|
|
|
|
//
|
|
|
|
// Created by MainasuK Cirno on 2021-3-17.
|
|
|
|
//
|
|
|
|
|
2021-05-31 10:42:49 +02:00
|
|
|
import os.log
|
2021-03-18 12:42:26 +01:00
|
|
|
import UIKit
|
|
|
|
import Combine
|
|
|
|
import PhotosUI
|
|
|
|
import GameplayKit
|
2021-05-31 10:42:49 +02:00
|
|
|
import MobileCoreServices
|
2021-03-18 12:42:26 +01:00
|
|
|
import MastodonSDK
|
2021-07-19 11:12:45 +02:00
|
|
|
import MastodonUI
|
2021-03-18 12:42:26 +01:00
|
|
|
|
2021-05-08 05:03:34 +02:00
|
|
|
protocol MastodonAttachmentServiceDelegate: AnyObject {
|
2021-03-18 12:42:26 +01:00
|
|
|
func mastodonAttachmentService(_ service: MastodonAttachmentService, uploadStateDidChange state: MastodonAttachmentService.UploadState?)
|
|
|
|
}
|
|
|
|
|
|
|
|
final class MastodonAttachmentService {
|
|
|
|
|
|
|
|
var disposeBag = Set<AnyCancellable>()
|
|
|
|
weak var delegate: MastodonAttachmentServiceDelegate?
|
|
|
|
|
|
|
|
let identifier = UUID()
|
2021-05-31 12:03:31 +02:00
|
|
|
|
2021-03-18 12:42:26 +01:00
|
|
|
// input
|
|
|
|
let context: AppContext
|
2021-07-20 10:40:04 +02:00
|
|
|
var authenticationBox: MastodonAuthenticationBox?
|
2021-05-31 10:42:49 +02:00
|
|
|
let file = CurrentValueSubject<Mastodon.Query.MediaAttachment?, Never>(nil)
|
|
|
|
let description = CurrentValueSubject<String?, Never>(nil)
|
2021-03-18 12:42:26 +01:00
|
|
|
|
|
|
|
// output
|
2021-05-31 10:42:49 +02:00
|
|
|
let thumbnailImage = CurrentValueSubject<UIImage?, Never>(nil)
|
2021-03-18 12:42:26 +01:00
|
|
|
let attachment = CurrentValueSubject<Mastodon.Entity.Attachment?, Never>(nil)
|
|
|
|
let error = CurrentValueSubject<Error?, Never>(nil)
|
|
|
|
|
|
|
|
private(set) lazy var uploadStateMachine: GKStateMachine = {
|
|
|
|
// exclude timeline middle fetcher state
|
|
|
|
let stateMachine = GKStateMachine(states: [
|
|
|
|
UploadState.Initial(service: self),
|
|
|
|
UploadState.Uploading(service: self),
|
2021-08-09 11:02:32 +02:00
|
|
|
UploadState.Processing(service: self),
|
2021-03-18 12:42:26 +01:00
|
|
|
UploadState.Fail(service: self),
|
|
|
|
UploadState.Finish(service: self),
|
|
|
|
])
|
|
|
|
stateMachine.enter(UploadState.Initial.self)
|
|
|
|
return stateMachine
|
|
|
|
}()
|
|
|
|
lazy var uploadStateMachineSubject = CurrentValueSubject<MastodonAttachmentService.UploadState?, Never>(nil)
|
|
|
|
|
|
|
|
init(
|
|
|
|
context: AppContext,
|
|
|
|
pickerResult: PHPickerResult,
|
2021-07-20 10:40:04 +02:00
|
|
|
initialAuthenticationBox: MastodonAuthenticationBox?
|
2021-03-18 12:42:26 +01:00
|
|
|
) {
|
|
|
|
self.context = context
|
2021-06-02 08:59:39 +02:00
|
|
|
self.authenticationBox = initialAuthenticationBox
|
2021-03-18 12:42:26 +01:00
|
|
|
// end init
|
|
|
|
|
2021-03-19 12:49:48 +01:00
|
|
|
setupServiceObserver()
|
2021-03-18 12:42:26 +01:00
|
|
|
|
2021-05-31 10:42:49 +02:00
|
|
|
Just(pickerResult)
|
|
|
|
.flatMap { result -> AnyPublisher<Mastodon.Query.MediaAttachment?, Error> in
|
|
|
|
if result.itemProvider.hasRepresentationConforming(toTypeIdentifier: UTType.image.identifier, fileOptions: []) {
|
2021-07-19 11:12:45 +02:00
|
|
|
return ItemProviderLoader.loadImageData(from: result).eraseToAnyPublisher()
|
2021-05-31 10:42:49 +02:00
|
|
|
}
|
|
|
|
if result.itemProvider.hasRepresentationConforming(toTypeIdentifier: UTType.movie.identifier, fileOptions: []) {
|
2021-07-19 11:12:45 +02:00
|
|
|
return ItemProviderLoader.loadVideoData(from: result).eraseToAnyPublisher()
|
2021-05-31 10:42:49 +02:00
|
|
|
}
|
|
|
|
return Fail(error: AttachmentError.invalidAttachmentType).eraseToAnyPublisher()
|
|
|
|
}
|
2021-03-18 12:42:26 +01:00
|
|
|
.sink { [weak self] completion in
|
|
|
|
guard let self = self else { return }
|
|
|
|
switch completion {
|
|
|
|
case .failure(let error):
|
|
|
|
self.error.value = error
|
2021-03-22 11:40:32 +01:00
|
|
|
self.uploadStateMachine.enter(UploadState.Fail.self)
|
2021-03-18 12:42:26 +01:00
|
|
|
case .finished:
|
|
|
|
break
|
|
|
|
}
|
2021-05-31 10:42:49 +02:00
|
|
|
} receiveValue: { [weak self] file in
|
2021-03-18 12:42:26 +01:00
|
|
|
guard let self = self else { return }
|
2021-05-31 10:42:49 +02:00
|
|
|
self.file.value = file
|
2021-03-22 11:40:32 +01:00
|
|
|
self.uploadStateMachine.enter(UploadState.Initial.self)
|
2021-03-18 12:42:26 +01:00
|
|
|
}
|
|
|
|
.store(in: &disposeBag)
|
|
|
|
}
|
|
|
|
|
2021-03-19 12:49:48 +01:00
|
|
|
init(
|
|
|
|
context: AppContext,
|
|
|
|
image: UIImage,
|
2021-07-20 10:40:04 +02:00
|
|
|
initialAuthenticationBox: MastodonAuthenticationBox?
|
2021-03-19 12:49:48 +01:00
|
|
|
) {
|
|
|
|
self.context = context
|
2021-06-02 08:59:39 +02:00
|
|
|
self.authenticationBox = initialAuthenticationBox
|
2021-03-19 12:49:48 +01:00
|
|
|
// end init
|
|
|
|
|
|
|
|
setupServiceObserver()
|
|
|
|
|
2021-05-31 10:42:49 +02:00
|
|
|
file.value = .jpeg(image.jpegData(compressionQuality: 0.75))
|
2021-03-22 11:40:32 +01:00
|
|
|
uploadStateMachine.enter(UploadState.Initial.self)
|
2021-03-19 12:49:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
init(
|
|
|
|
context: AppContext,
|
2021-05-31 12:03:31 +02:00
|
|
|
documentURL: URL,
|
2021-07-20 10:40:04 +02:00
|
|
|
initialAuthenticationBox: MastodonAuthenticationBox?
|
2021-03-19 12:49:48 +01:00
|
|
|
) {
|
|
|
|
self.context = context
|
2021-06-02 08:59:39 +02:00
|
|
|
self.authenticationBox = initialAuthenticationBox
|
2021-03-19 12:49:48 +01:00
|
|
|
// end init
|
|
|
|
|
|
|
|
setupServiceObserver()
|
|
|
|
|
2021-05-31 12:03:31 +02:00
|
|
|
Just(documentURL)
|
|
|
|
.flatMap { documentURL -> AnyPublisher<Mastodon.Query.MediaAttachment, Error> in
|
|
|
|
return MastodonAttachmentService.loadAttachment(url: documentURL)
|
|
|
|
}
|
|
|
|
.sink { [weak self] completion in
|
|
|
|
guard let self = self else { return }
|
|
|
|
switch completion {
|
|
|
|
case .failure(let error):
|
|
|
|
self.error.value = error
|
|
|
|
self.uploadStateMachine.enter(UploadState.Fail.self)
|
|
|
|
case .finished:
|
|
|
|
break
|
|
|
|
}
|
|
|
|
} receiveValue: { [weak self] file in
|
|
|
|
guard let self = self else { return }
|
|
|
|
self.file.value = file
|
|
|
|
self.uploadStateMachine.enter(UploadState.Initial.self)
|
|
|
|
}
|
|
|
|
.store(in: &disposeBag)
|
|
|
|
|
2021-03-22 11:40:32 +01:00
|
|
|
uploadStateMachine.enter(UploadState.Initial.self)
|
2021-03-19 12:49:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private func setupServiceObserver() {
|
|
|
|
uploadStateMachineSubject
|
|
|
|
.sink { [weak self] state in
|
|
|
|
guard let self = self else { return }
|
|
|
|
self.delegate?.mastodonAttachmentService(self, uploadStateDidChange: state)
|
|
|
|
}
|
|
|
|
.store(in: &disposeBag)
|
2021-05-31 12:03:31 +02:00
|
|
|
|
|
|
|
|
|
|
|
file
|
|
|
|
.map { file -> UIImage? in
|
|
|
|
guard let file = file else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
switch file {
|
|
|
|
case .jpeg(let data), .png(let data):
|
|
|
|
return data.flatMap { UIImage(data: $0) }
|
|
|
|
case .gif:
|
|
|
|
// TODO:
|
|
|
|
return nil
|
|
|
|
case .other(let url, _, _):
|
|
|
|
guard let url = url, FileManager.default.fileExists(atPath: url.path) else { return nil }
|
|
|
|
let asset = AVURLAsset(url: url)
|
|
|
|
let assetImageGenerator = AVAssetImageGenerator(asset: asset)
|
|
|
|
assetImageGenerator.appliesPreferredTrackTransform = true // fix orientation
|
|
|
|
do {
|
|
|
|
let cgImage = try assetImageGenerator.copyCGImage(at: CMTimeMake(value: 0, timescale: 1), actualTime: nil)
|
|
|
|
let image = UIImage(cgImage: cgImage)
|
|
|
|
return image
|
|
|
|
} catch {
|
|
|
|
os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s: thumbnail generate fail: %s", ((#file as NSString).lastPathComponent), #line, #function, error.localizedDescription)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.assign(to: \.value, on: thumbnailImage)
|
|
|
|
.store(in: &disposeBag)
|
2021-03-19 12:49:48 +01:00
|
|
|
}
|
|
|
|
|
2021-05-31 10:42:49 +02:00
|
|
|
deinit {
|
|
|
|
os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s", ((#file as NSString).lastPathComponent), #line, #function)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
extension MastodonAttachmentService {
|
|
|
|
enum AttachmentError: Error {
|
|
|
|
case invalidAttachmentType
|
|
|
|
case attachmentTooLarge
|
|
|
|
}
|
2021-03-18 12:42:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
extension MastodonAttachmentService {
|
|
|
|
// FIXME: needs reset state for multiple account posting support
|
2021-07-20 10:40:04 +02:00
|
|
|
func uploading(mastodonAuthenticationBox: MastodonAuthenticationBox) -> Bool {
|
2021-03-18 12:42:26 +01:00
|
|
|
authenticationBox = mastodonAuthenticationBox
|
|
|
|
return uploadStateMachine.enter(UploadState.self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extension MastodonAttachmentService: Equatable, Hashable {
|
|
|
|
|
|
|
|
static func == (lhs: MastodonAttachmentService, rhs: MastodonAttachmentService) -> Bool {
|
|
|
|
return lhs.identifier == rhs.identifier
|
|
|
|
}
|
|
|
|
|
|
|
|
func hash(into hasher: inout Hasher) {
|
|
|
|
hasher.combine(identifier)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2021-05-31 12:03:31 +02:00
|
|
|
|
|
|
|
extension MastodonAttachmentService {
|
|
|
|
|
|
|
|
private static func createWorkingQueue() -> DispatchQueue {
|
2021-06-11 22:37:54 +02:00
|
|
|
return DispatchQueue(label: "org.joinmastodon.app.MastodonAttachmentService.\(UUID().uuidString)")
|
2021-05-31 12:03:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static func loadAttachment(url: URL) -> AnyPublisher<Mastodon.Query.MediaAttachment, Error> {
|
|
|
|
guard let uti = UTType(filenameExtension: url.pathExtension) else {
|
|
|
|
return Fail(error: AttachmentError.invalidAttachmentType).eraseToAnyPublisher()
|
|
|
|
}
|
|
|
|
|
|
|
|
if uti.conforms(to: .image) {
|
|
|
|
return loadImageAttachment(url: url)
|
|
|
|
} else if uti.conforms(to: .movie) {
|
|
|
|
return loadVideoAttachment(url: url)
|
|
|
|
} else {
|
|
|
|
return Fail(error: AttachmentError.invalidAttachmentType).eraseToAnyPublisher()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static func loadImageAttachment(url: URL) -> AnyPublisher<Mastodon.Query.MediaAttachment, Error> {
|
|
|
|
Future<Mastodon.Query.MediaAttachment, Error> { promise in
|
|
|
|
createWorkingQueue().async {
|
|
|
|
do {
|
|
|
|
guard url.startAccessingSecurityScopedResource() else { return }
|
|
|
|
defer { url.stopAccessingSecurityScopedResource() }
|
|
|
|
let imageData = try Data(contentsOf: url)
|
|
|
|
promise(.success(.jpeg(imageData)))
|
|
|
|
} catch {
|
|
|
|
os_log("%{public}s[%{public}ld], %{public}s: %s", ((#file as NSString).lastPathComponent), #line, #function, error.localizedDescription)
|
|
|
|
promise(.failure(error))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.eraseToAnyPublisher()
|
|
|
|
}
|
|
|
|
|
|
|
|
static func loadVideoAttachment(url: URL) -> AnyPublisher<Mastodon.Query.MediaAttachment, Error> {
|
|
|
|
Future<Mastodon.Query.MediaAttachment, Error> { promise in
|
|
|
|
createWorkingQueue().async {
|
|
|
|
guard url.startAccessingSecurityScopedResource() else { return }
|
|
|
|
defer { url.stopAccessingSecurityScopedResource() }
|
|
|
|
|
|
|
|
let fileName = UUID().uuidString
|
|
|
|
let tempDirectoryURL = FileManager.default.temporaryDirectory
|
|
|
|
let fileURL = tempDirectoryURL.appendingPathComponent(fileName).appendingPathExtension(url.pathExtension)
|
|
|
|
do {
|
|
|
|
try FileManager.default.createDirectory(at: tempDirectoryURL, withIntermediateDirectories: true, attributes: nil)
|
|
|
|
try FileManager.default.copyItem(at: url, to: fileURL)
|
|
|
|
let file = Mastodon.Query.MediaAttachment.other(fileURL, fileExtension: fileURL.pathExtension, mimeType: UTType.movie.preferredMIMEType ?? "video/mp4")
|
|
|
|
promise(.success(file))
|
|
|
|
} catch {
|
|
|
|
promise(.failure(error))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.eraseToAnyPublisher()
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|