Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix image uploading on 0.19.0 #789

Merged
merged 4 commits into from
Dec 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Mlem/API/APIClient/APIClient+Pictrs.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion Mlem/Models/PictrsImageModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import SwiftUI

struct ImageUploadResponse: Codable {
public let msg: String
public let msg: String?
public let files: [PictrsFile]?
}

Expand Down
13 changes: 9 additions & 4 deletions Mlem/Repositories/PictrsRespository.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -43,18 +43,23 @@ 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)
}

updateCallback(imageModel)
})
} catch {
print("Upload failed: \(error)")
print("Upload failed (3): \(error)")
imageModel.state = .failed(error.localizedDescription)
updateCallback(imageModel)
}
Expand Down