Skip to content

Commit

Permalink
Fix image uploading on 0.19.0 (#789)
Browse files Browse the repository at this point in the history
  • Loading branch information
Sjmarf authored Dec 2, 2023
1 parent 88e2895 commit 71a9a9b
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
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

0 comments on commit 71a9a9b

Please sign in to comment.