compress to JPEG before uploading

This commit is contained in:
Thomas Ricouard 2022-12-27 16:25:47 +01:00
parent 627173989e
commit 99dc57a023
2 changed files with 3 additions and 3 deletions

View file

@ -141,7 +141,7 @@ public class Client: ObservableObject, Equatable {
request.setValue("multipart/form-data; boundary=\(boundary)", forHTTPHeaderField: "Content-Type")
let httpBody = NSMutableData()
httpBody.append("--\(boundary)\r\n".data(using: .utf8)!)
httpBody.append("Content-Disposition: form-data; name=\"file\"; filename=\"file.png\"\r\n".data(using: .utf8)!)
httpBody.append("Content-Disposition: form-data; name=\"file\"; filename=\"file.jpg\"\r\n".data(using: .utf8)!)
httpBody.append("Content-Type: \(mimeType)\r\n".data(using: .utf8)!)
httpBody.append("\r\n".data(using: .utf8)!)
httpBody.append(data)

View file

@ -158,7 +158,7 @@ public class StatusEditorViewModel: ObservableObject {
for (index, media) in mediasCopy.enumerated() {
do {
if !Task.isCancelled,
let data = media.image?.pngData(),
let data = media.image?.jpegData(compressionQuality: 0.90),
let uploadedMedia = try await uploadMedia(data: data) {
mediasImages[index] = .init(image: nil, mediaAttachement: uploadedMedia, error: nil)
}
@ -172,7 +172,7 @@ public class StatusEditorViewModel: ObservableObject {
private func uploadMedia(data: Data) async throws -> MediaAttachement? {
guard let client else { return nil }
do {
return try await client.mediaUpload(mimeType: "image/png", data: data)
return try await client.mediaUpload(mimeType: "image/jpeg", data: data)
} catch {
return nil
}