fix: upload image not use V2 endpoint issue
This commit is contained in:
parent
ae1a153536
commit
bf351f8abb
|
@ -10,8 +10,21 @@ import Combine
|
|||
import MastodonSDK
|
||||
|
||||
extension APIService {
|
||||
|
||||
|
||||
func uploadMedia(
|
||||
domain: String,
|
||||
query: Mastodon.API.Media.UploadMediaQuery,
|
||||
mastodonAuthenticationBox: AuthenticationService.MastodonAuthenticationBox,
|
||||
needsFallback: Bool
|
||||
) -> AnyPublisher<Mastodon.Response.Content<Mastodon.Entity.Attachment>, Error> {
|
||||
if needsFallback {
|
||||
return uploadMediaV1(domain: domain, query: query, mastodonAuthenticationBox: mastodonAuthenticationBox)
|
||||
} else {
|
||||
return uploadMediaV2(domain: domain, query: query, mastodonAuthenticationBox: mastodonAuthenticationBox)
|
||||
}
|
||||
}
|
||||
|
||||
private func uploadMediaV1(
|
||||
domain: String,
|
||||
query: Mastodon.API.Media.UploadMediaQuery,
|
||||
mastodonAuthenticationBox: AuthenticationService.MastodonAuthenticationBox
|
||||
|
@ -25,6 +38,22 @@ extension APIService {
|
|||
authorization: authorization
|
||||
)
|
||||
}
|
||||
|
||||
private func uploadMediaV2(
|
||||
domain: String,
|
||||
query: Mastodon.API.Media.UploadMediaQuery,
|
||||
mastodonAuthenticationBox: AuthenticationService.MastodonAuthenticationBox
|
||||
) -> AnyPublisher<Mastodon.Response.Content<Mastodon.Entity.Attachment>, Error> {
|
||||
let authorization = mastodonAuthenticationBox.userAuthorization
|
||||
|
||||
return Mastodon.API.V2.Media.uploadMedia(
|
||||
session: session,
|
||||
domain: domain,
|
||||
query: query,
|
||||
authorization: authorization
|
||||
)
|
||||
.eraseToAnyPublisher()
|
||||
}
|
||||
|
||||
func updateMedia(
|
||||
domain: String,
|
||||
|
|
|
@ -7,6 +7,7 @@
|
|||
|
||||
import os.log
|
||||
import Foundation
|
||||
import Combine
|
||||
import GameplayKit
|
||||
import MastodonSDK
|
||||
|
||||
|
@ -43,8 +44,10 @@ extension MastodonAttachmentService.UploadState {
|
|||
}
|
||||
|
||||
class Uploading: MastodonAttachmentService.UploadState {
|
||||
var needsFallback = false
|
||||
|
||||
override func isValidNextState(_ stateClass: AnyClass) -> Bool {
|
||||
return stateClass == Fail.self || stateClass == Finish.self
|
||||
return stateClass == Fail.self || stateClass == Finish.self || stateClass == Uploading.self
|
||||
}
|
||||
|
||||
override func didEnter(from previousState: GKState?) {
|
||||
|
@ -61,30 +64,43 @@ extension MastodonAttachmentService.UploadState {
|
|||
description: description,
|
||||
focus: nil
|
||||
)
|
||||
|
||||
|
||||
// and needs clone the `query` if needs retry
|
||||
service.context.apiService.uploadMedia(
|
||||
domain: authenticationBox.domain,
|
||||
query: query,
|
||||
mastodonAuthenticationBox: authenticationBox
|
||||
mastodonAuthenticationBox: authenticationBox,
|
||||
needsFallback: needsFallback
|
||||
)
|
||||
.receive(on: DispatchQueue.main)
|
||||
.sink { completion in
|
||||
.sink { [weak self] completion in
|
||||
guard let self = self else { return }
|
||||
switch completion {
|
||||
case .failure(let error):
|
||||
os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s: upload attachment fail: %s", ((#file as NSString).lastPathComponent), #line, #function, error.localizedDescription)
|
||||
service.error.send(error)
|
||||
stateMachine.enter(Fail.self)
|
||||
if let apiError = error as? Mastodon.API.Error,
|
||||
apiError.httpResponseStatus == .notFound,
|
||||
self.needsFallback == false
|
||||
{
|
||||
self.needsFallback = true
|
||||
stateMachine.enter(Uploading.self)
|
||||
os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s: upload attachment fallback to V1", ((#file as NSString).lastPathComponent), #line, #function, error.localizedDescription)
|
||||
} else {
|
||||
os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s: upload attachment fail: %s", ((#file as NSString).lastPathComponent), #line, #function, error.localizedDescription)
|
||||
service.error.send(error)
|
||||
stateMachine.enter(Fail.self)
|
||||
}
|
||||
case .finished:
|
||||
os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s: upload attachment success", ((#file as NSString).lastPathComponent), #line, #function)
|
||||
break
|
||||
}
|
||||
} receiveValue: { response in
|
||||
os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s: upload attachment %s success: %s", ((#file as NSString).lastPathComponent), #line, #function, response.value.id, response.value.url)
|
||||
os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s: upload attachment %s success: %s", ((#file as NSString).lastPathComponent), #line, #function, response.value.id, response.value.url ?? "<nil>")
|
||||
service.attachment.value = response.value
|
||||
stateMachine.enter(Finish.self)
|
||||
}
|
||||
.store(in: &service.disposeBag)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class Fail: MastodonAttachmentService.UploadState {
|
||||
|
|
|
@ -104,7 +104,12 @@ extension Mastodon.API.Media {
|
|||
|
||||
return SerialStream(streams: streams)
|
||||
}
|
||||
|
||||
public var clone: UploadMediaQuery {
|
||||
UploadMediaQuery(file: file, thumbnail: thumbnail, description: description, focus: focus)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue