mv-experiment #1
|
@ -99,7 +99,10 @@ extension Client {
|
||||||
try await chat.send(message: msg)
|
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 {
|
guard let data = try? Data(contentsOf: localPath) else {
|
||||||
throw ClientStoreError.noData
|
throw ClientStoreError.noData
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,4 +6,7 @@ enum ClientStoreError: Error {
|
||||||
case fileStoreError
|
case fileStoreError
|
||||||
case noData
|
case noData
|
||||||
case fileTooBig
|
case fileTooBig
|
||||||
|
case invalidContentType
|
||||||
|
case invalidPath
|
||||||
|
case invalidLocalName
|
||||||
}
|
}
|
||||||
|
|
|
@ -68,8 +68,16 @@ extension ConversationStore {
|
||||||
func sendCaptured(_ data: Data, _ type: GalleryMediaType) async {
|
func sendCaptured(_ data: Data, _ type: GalleryMediaType) async {
|
||||||
// save locally and make message
|
// save locally and make message
|
||||||
let messageId = UUID().uuidString
|
let messageId = UUID().uuidString
|
||||||
|
let localName: String
|
||||||
|
let msgType: AttachmentType
|
||||||
do {
|
do {
|
||||||
let (localName, msgType) = try await FileStore.shared.storeCaptured(messageId: messageId, data, type)
|
(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(
|
let message = Message(
|
||||||
id: UUID().uuidString,
|
id: UUID().uuidString,
|
||||||
type: .chat,
|
type: .chat,
|
||||||
|
@ -90,13 +98,15 @@ extension ConversationStore {
|
||||||
thread: nil,
|
thread: nil,
|
||||||
oobUrl: nil
|
oobUrl: nil
|
||||||
)
|
)
|
||||||
|
do {
|
||||||
try await message.save()
|
try await message.save()
|
||||||
} catch {
|
} catch {
|
||||||
logIt(.error, "Can't save file for uploading: \(error)")
|
logIt(.error, "Can't save message: \(error)")
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// upload and save
|
// upload and save
|
||||||
upload(message: message)
|
await upload(message)
|
||||||
}
|
}
|
||||||
|
|
||||||
func sendDocuments(_ data: [Data], _ extensions: [String]) async {
|
func sendDocuments(_ data: [Data], _ extensions: [String]) async {
|
||||||
|
@ -123,22 +133,30 @@ extension ConversationStore {
|
||||||
await sendMessage("geo:\(lat),\(lon)")
|
await sendMessage("geo:\(lat),\(lon)")
|
||||||
}
|
}
|
||||||
|
|
||||||
private func upload(message: Message) async {
|
private func upload(_ message: Message) async {
|
||||||
let remotePath: String
|
|
||||||
do {
|
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(
|
message.contentType = .attachment(
|
||||||
Attachment(
|
Attachment(
|
||||||
type: msgType,
|
type: attachment.type,
|
||||||
localName: localName,
|
localName: attachment.localName,
|
||||||
thumbnailName: nil,
|
thumbnailName: nil,
|
||||||
remotePath: remotePath
|
remotePath: remotePath
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
try await message.save()
|
try await message.save()
|
||||||
|
try await client.sendMessage(message)
|
||||||
|
try await message.setStatus(.sent)
|
||||||
} catch {
|
} catch {
|
||||||
message.status = .error
|
try? await message.setStatus(.error)
|
||||||
try? await message.save()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue