From 71a9a9ba32965fbdabcf405adee09d44aad6d22e Mon Sep 17 00:00:00 2001 From: Sjmarf <78750526+Sjmarf@users.noreply.github.com> Date: Sat, 2 Dec 2023 05:18:47 +0000 Subject: [PATCH] Fix image uploading on 0.19.0 (#789) --- Mlem/API/APIClient/APIClient+Pictrs.swift | 5 +++++ Mlem/Models/PictrsImageModel.swift | 2 +- Mlem/Repositories/PictrsRespository.swift | 13 +++++++++---- 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/Mlem/API/APIClient/APIClient+Pictrs.swift b/Mlem/API/APIClient/APIClient+Pictrs.swift index 6f01ecd81..0c3546c5d 100644 --- a/Mlem/API/APIClient/APIClient+Pictrs.swift +++ b/Mlem/API/APIClient/APIClient+Pictrs.swift @@ -36,7 +36,12 @@ extension APIClient { let boundary = UUID().uuidString request.setValue("multipart/form-data; boundary=\(boundary)", forHTTPHeaderField: "Content-Type") + + // This is required pre 0.19.0 + // TODO: 0.18 deprecation: possibly remove this? Haven't tested how >0.19 behaves without this, but I assume it's not required anymore since they're now requiring a different format instead try request.setValue("jwt=\(session.token)", forHTTPHeaderField: "Cookie") + // This is required post 0.19.0 + try request.setValue("Bearer \(session.token)", forHTTPHeaderField: "Authorization") let multiPartForm: MultiPartForm = try .init( mimeType: "image/png", diff --git a/Mlem/Models/PictrsImageModel.swift b/Mlem/Models/PictrsImageModel.swift index ea8500aa8..ab9d4f530 100644 --- a/Mlem/Models/PictrsImageModel.swift +++ b/Mlem/Models/PictrsImageModel.swift @@ -8,7 +8,7 @@ import SwiftUI struct ImageUploadResponse: Codable { - public let msg: String + public let msg: String? public let files: [PictrsFile]? } diff --git a/Mlem/Repositories/PictrsRespository.swift b/Mlem/Repositories/PictrsRespository.swift index e6f68cafc..7eee991e9 100644 --- a/Mlem/Repositories/PictrsRespository.swift +++ b/Mlem/Repositories/PictrsRespository.swift @@ -33,7 +33,7 @@ class PictrsRespository { imageModel.state = .uploaded(file: firstFile) updateCallback(imageModel) } else { - print("Upload failed: \(response.msg)") + print("Upload failed (1): \(response.msg)") imageModel.state = .failed(response.msg) updateCallback(imageModel) } @@ -43,10 +43,15 @@ class PictrsRespository { updateCallback(imageModel) } }, catch: { error in - print("Upload failed: \(error)") + print("Upload failed (2): \(error)") switch error { case let APIClientError.decoding(data, _): - imageModel.state = .failed(String(data: data, encoding: .utf8)) + let text = String(data: data, encoding: .utf8) + if text?.contains("413 Request Entity Too Large") ?? false { + imageModel.state = .failed("Image too large") + } else { + imageModel.state = .failed(text) + } default: imageModel.state = .failed(error.localizedDescription) } @@ -54,7 +59,7 @@ class PictrsRespository { updateCallback(imageModel) }) } catch { - print("Upload failed: \(error)") + print("Upload failed (3): \(error)") imageModel.state = .failed(error.localizedDescription) updateCallback(imageModel) }