Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Compress image attachments on iOS

laurensprieme5dda77122b7
diff --git a/mobile/native/darwin/Apps/Ensu/Ensu/Chat/ChatViewModel.swift b/mobile/native/darwin/Apps/Ensu/Ensu/Chat/ChatViewModel.swift
index 0684b5c302..d2ef3b671d 100644
--- a/mobile/native/darwin/Apps/Ensu/Ensu/Chat/ChatViewModel.swift
+++ b/mobile/native/darwin/Apps/Ensu/Ensu/Chat/ChatViewModel.swift
@@ -1088,11 +1088,12 @@ final class ChatViewModel: ObservableObject {
guard let self else { return }
do {
let id = UUID()
- let url = try self.writeAttachment(data: data, attachmentId: id)
+ let compressed = try compressAttachmentImage(data: data)
+ let url = try self.writeAttachment(data: compressed, attachmentId: id)
let attachment = ChatAttachment(
id: id,
- name: fileName ?? "photo.jpg",
- size: Int64(data.count),
+ name: self.normalizedJpegAttachmentName(fileName),
+ size: Int64(compressed.count),
kind: .image,
url: url,
isUploading: false
@@ -1107,6 +1108,19 @@ final class ChatViewModel: ObservableObject {
}
}
+ private nonisolated func normalizedJpegAttachmentName(_ fileName: String?) -> String {
+ let raw = fileName?
+ .replacingOccurrences(of: "\0", with: "")
+ .replacingOccurrences(of: "\\", with: "/")
+ .split(separator: "/")
+ .last
+ .map(String.init)?
+ .trimmingCharacters(in: .whitespacesAndNewlines)
+ let cleaned = raw?.isEmpty == false ? raw! : "photo"
+ let base = (cleaned as NSString).deletingPathExtension
+ return "\(base.isEmpty ? "photo" : base).jpg"
+ }
+
func addDocumentAttachment(url: URL) {
guard !isGenerating && !isDownloading && !isAttachmentDownloadBlocked else { return }
isProcessingAttachments = true