mv-experiment #1

Merged
fmodf merged 88 commits from mv-experiment into develop 2024-09-03 15:13:59 +00:00
2 changed files with 118 additions and 21 deletions
Showing only changes of commit 062292bb11 - Show all commits

View file

@ -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
}

View file

@ -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 {