From 062292bb11ea4b182ef9fe3be0a18822c766047a Mon Sep 17 00:00:00 2001 From: fmodf Date: Sun, 18 Aug 2024 01:21:15 +0200 Subject: [PATCH] wip --- .../Client/Client+MartinMessages.swift | 11 +- .../AppData/Store/ConversationStore.swift | 128 +++++++++++++++--- 2 files changed, 118 insertions(+), 21 deletions(-) diff --git a/ConversationsClassic/AppData/Client/Client+MartinMessages.swift b/ConversationsClassic/AppData/Client/Client+MartinMessages.swift index a5b44c0..a9714f4 100644 --- a/ConversationsClassic/AppData/Client/Client+MartinMessages.swift +++ b/ConversationsClassic/AppData/Client/Client+MartinMessages.swift @@ -37,9 +37,16 @@ final class ClientMartinMessagesManager { // Content type var contentType: MessageContentType = .text - if message.hints.contains(.noStore) { + if let oob = message.oob { + contentType = .attachment(.init( + type: oob.attachmentType, + localName: nil, + thumbnailName: nil, + remotePath: oob + )) + } else if message.hints.contains(.noStore) { contentType = .typing - // for now just skip it + // skip for now return } diff --git a/ConversationsClassic/AppData/Store/ConversationStore.swift b/ConversationsClassic/AppData/Store/ConversationStore.swift index b67d126..cf31770 100644 --- a/ConversationsClassic/AppData/Store/ConversationStore.swift +++ b/ConversationsClassic/AppData/Store/ConversationStore.swift @@ -45,12 +45,53 @@ extension ConversationStore { } func sendMedia(_ items: [GalleryItem]) async { - print("media!", items) - // guard !ids.isEmpty else { return } - // let items = galleryItems.filter { ids.contains($0.id) } - // for item in items { - // await client.uploadMedia(item.url) - // } + for item in items { + Task { + var message = Message.blank + message.from = roster.bareJid + message.to = roster.contactBareJid + + switch item.type { + case .photo: + guard let asset = PHAsset.fetchAssets(withLocalIdentifiers: [item.id], options: nil).firstObject else { return } + guard let photo = try? await PHImageManager.default().getPhoto(for: asset) else { return } + guard let data = photo.jpegData(compressionQuality: 1.0) else { return } + let localName = "\(message.id)_\(UUID().uuidString).jpg" + let localUrl = Const.fileFolder.appendingPathComponent(localName) + try? data.write(to: localUrl) + message.contentType = .attachment( + Attachment( + type: .image, + localName: localName, + thumbnailName: nil, + remotePath: nil + ) + ) + try? await message.save() + + case .video: + guard let asset = PHAsset.fetchAssets(withLocalIdentifiers: [item.id], options: nil).firstObject else { return } + guard let video = try? await PHImageManager.default().getVideo(for: asset) else { return } + // swiftlint:disable:next force_cast + let assetURL = video as! AVURLAsset + let url = assetURL.url + let localName = "\(message.id)_\(UUID().uuidString).mov" + let localUrl = Const.fileFolder.appendingPathComponent(localName) + try? FileManager.default.copyItem(at: url, to: localUrl) + message.contentType = .attachment( + Attachment( + type: .video, + localName: localName, + thumbnailName: nil, + remotePath: nil + ) + ) + try? await message.save() + } + + await upload(message) + } + } } func sendCaptured(_ data: Data, _ type: GalleryMediaType) async { @@ -108,19 +149,38 @@ extension ConversationStore { } func sendDocuments(_ data: [Data], _ extensions: [String]) async { - // do { - // let newMessageId = UUID().uuidString - // let fileId = UUID().uuidString - // let localName = "\(newMessageId)_\(fileId).\(ext)" - // try await FileStore.shared.storeForUploading(data, localName) - // - // } catch { - // print("error", error) - // } - print("documents!", data, extensions) - // - // - // + for (index, data) in data.enumerated() { + Task { + let newMessageId = UUID().uuidString + let fileId = UUID().uuidString + let localName = "\(newMessageId)_\(fileId).\(extensions[index])" + let localUrl = Const.fileFolder.appendingPathComponent(localName) + do { + try data.write(to: localUrl) + } catch { + print("FileProcessing: Error writing document: \(error)") + return + } + + var message = Message.blank + message.from = roster.bareJid + message.to = roster.contactBareJid + message.contentType = .attachment( + Attachment( + type: localName.attachmentType, + localName: localName, + thumbnailName: nil, + remotePath: nil + ) + ) + do { + try await message.save() + await upload(message) + } catch { + print("FileProcessing: Error saving document: \(error)") + } + } + } } func sendContact(_ jidStr: String) async { @@ -159,6 +219,36 @@ extension ConversationStore { try? await message.setStatus(.error) } } + + func downloadAttachment(_ message: Message) async { + guard case .attachment(let attachment) = message.contentType else { + return + } + guard let remotePath = attachment.remotePath, let remoteUrl = URL(string: remotePath) else { + return + } + do { + let localName = "\(message.id)_\(UUID().uuidString).\(remoteUrl.lastPathComponent)" + let localUrl = Const.fileFolder.appendingPathComponent(localName) + + // Download the file + let (tempUrl, _) = try await URLSession.shared.download(from: remoteUrl) + try FileManager.default.moveItem(at: tempUrl, to: localUrl) + + var message = message + message.contentType = .attachment( + Attachment( + type: attachment.type, + localName: localName, + thumbnailName: attachment.thumbnailName, + remotePath: remotePath + ) + ) + try await message.save() + } catch { + logIt(.error, "Can't download attachment: \(error)") + } + } } extension ConversationStore {