From dcd275b2795830cf77d6436b1b89ee15c3d017ac Mon Sep 17 00:00:00 2001 From: fmodf Date: Sat, 17 Aug 2024 23:33:14 +0200 Subject: [PATCH] wip --- .../AppData/Client/Client.swift | 5 +- .../AppData/Store/ClientStoreError.swift | 3 + .../AppData/Store/ConversationStore.swift | 78 ++++++++++++------- 3 files changed, 55 insertions(+), 31 deletions(-) diff --git a/ConversationsClassic/AppData/Client/Client.swift b/ConversationsClassic/AppData/Client/Client.swift index 08852a9..cda7f62 100644 --- a/ConversationsClassic/AppData/Client/Client.swift +++ b/ConversationsClassic/AppData/Client/Client.swift @@ -99,7 +99,10 @@ extension Client { try await chat.send(message: msg) } - func uploadFile(_ localPath: URL) async throws -> String { + func uploadFile(_ localPath: String) async throws -> String { + guard let localPath = URL(string: localPath) else { + throw ClientStoreError.invalidPath + } guard let data = try? Data(contentsOf: localPath) else { throw ClientStoreError.noData } diff --git a/ConversationsClassic/AppData/Store/ClientStoreError.swift b/ConversationsClassic/AppData/Store/ClientStoreError.swift index feb0db8..92d4aff 100644 --- a/ConversationsClassic/AppData/Store/ClientStoreError.swift +++ b/ConversationsClassic/AppData/Store/ClientStoreError.swift @@ -6,4 +6,7 @@ enum ClientStoreError: Error { case fileStoreError case noData case fileTooBig + case invalidContentType + case invalidPath + case invalidLocalName } diff --git a/ConversationsClassic/AppData/Store/ConversationStore.swift b/ConversationsClassic/AppData/Store/ConversationStore.swift index fc71be7..58f2319 100644 --- a/ConversationsClassic/AppData/Store/ConversationStore.swift +++ b/ConversationsClassic/AppData/Store/ConversationStore.swift @@ -68,35 +68,45 @@ extension ConversationStore { func sendCaptured(_ data: Data, _ type: GalleryMediaType) async { // save locally and make message let messageId = UUID().uuidString + let localName: String + let msgType: AttachmentType do { - let (localName, msgType) = try await FileStore.shared.storeCaptured(messageId: messageId, data, type) - let message = Message( - id: UUID().uuidString, - type: .chat, - date: Date(), - contentType: .attachment( - Attachment( - type: msgType, - localName: localName, - thumbnailName: nil, - remotePath: nil - ) - ), - status: .pending, - from: roster.bareJid, - to: roster.contactBareJid, - body: nil, - subject: nil, - thread: nil, - oobUrl: nil - ) - try await message.save() + (localName, msgType) = try await FileStore.shared.storeCaptured(messageId: messageId, data, type) } catch { logIt(.error, "Can't save file for uploading: \(error)") + return + } + + // save message + let message = Message( + id: UUID().uuidString, + type: .chat, + date: Date(), + contentType: .attachment( + Attachment( + type: msgType, + localName: localName, + thumbnailName: nil, + remotePath: nil + ) + ), + status: .pending, + from: roster.bareJid, + to: roster.contactBareJid, + body: nil, + subject: nil, + thread: nil, + oobUrl: nil + ) + do { + try await message.save() + } catch { + logIt(.error, "Can't save message: \(error)") + return } // upload and save - upload(message: message) + await upload(message) } func sendDocuments(_ data: [Data], _ extensions: [String]) async { @@ -123,22 +133,30 @@ extension ConversationStore { await sendMessage("geo:\(lat),\(lon)") } - private func upload(message: Message) async { - let remotePath: String + private func upload(_ message: Message) async { do { - remotePath = try await client.uploadFile(localPath) + try await message.setStatus(.pending) + var message = message + guard case .attachment(let attachment) = message.contentType else { + throw ClientStoreError.invalidContentType + } + guard let localName = attachment.localName else { + throw ClientStoreError.invalidLocalName + } + let remotePath = try await client.uploadFile(localName) message.contentType = .attachment( Attachment( - type: msgType, - localName: localName, + type: attachment.type, + localName: attachment.localName, thumbnailName: nil, remotePath: remotePath ) ) try await message.save() + try await client.sendMessage(message) + try await message.setStatus(.sent) } catch { - message.status = .error - try? await message.save() + try? await message.setStatus(.error) } } }