another.im-ios/ConversationsClassic/AppCore/Middlewares/FileMiddleware.swift
2024-07-13 03:29:46 +02:00

55 lines
2.4 KiB
Swift

import Combine
import Foundation
import UIKit
final class FileMiddleware {
static let shared = FileMiddleware()
func middleware(state _: AppState, action: AppAction) -> AnyPublisher<AppAction, Never> {
switch action {
case .conversationAction(.attachmentsUpdated(let attachments)):
return Future { promise in
for attachment in attachments where attachment.localPath == nil && attachment.remotePath != nil {
DispatchQueue.main.async {
// swiftlint:disable:next force_unwrapping
store.dispatch(.fileAction(.downloadAttachmentFile(id: attachment.id, remotePath: attachment.remotePath!)))
}
}
promise(.success(.empty))
}.eraseToAnyPublisher()
case .fileAction(.downloadAttachmentFile(let id, let remotePath)):
return Future { promise in
let localUrl = FileProcessing.fileFolder.appendingPathComponent(id).appendingPathExtension(remotePath.pathExtension)
DownloadManager.shared.enqueueDownload(from: remotePath, to: localUrl) { error in
DispatchQueue.main.async {
if let error {
store.dispatch(.fileAction(.downloadingAttachmentFileFailed(id: id, reason: error.localizedDescription)))
} else {
store.dispatch(.fileAction(.attachmentFileDownloaded(id: id, localUrl: localUrl)))
}
}
}
promise(.success(.empty))
}.eraseToAnyPublisher()
case .fileAction(.attachmentFileDownloaded(let id, let localUrl)):
return Just(.fileAction(.createAttachmentThumbnail(id: id, localUrl: localUrl)))
.eraseToAnyPublisher()
case .fileAction(.createAttachmentThumbnail(let id, let localUrl)):
return Future { promise in
if let thumbnailUrl = FileProcessing.shared.createThumbnail(id: id, localUrl: localUrl) {
promise(.success(.fileAction(.attachmentThumbnailCreated(id: id, thumbnailUrl: thumbnailUrl))))
} else {
promise(.success(.empty))
}
}
.eraseToAnyPublisher()
default:
return Empty().eraseToAnyPublisher()
}
}
}