2021-03-18 12:42:26 +01:00
|
|
|
//
|
|
|
|
// APIService+Media.swift
|
|
|
|
// Mastodon
|
|
|
|
//
|
|
|
|
// Created by MainasuK Cirno on 2021-3-18.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
import Combine
|
|
|
|
import MastodonSDK
|
|
|
|
|
|
|
|
extension APIService {
|
2021-07-15 11:25:15 +02:00
|
|
|
|
2021-03-18 12:42:26 +01:00
|
|
|
func uploadMedia(
|
2021-07-15 11:25:15 +02:00
|
|
|
domain: String,
|
|
|
|
query: Mastodon.API.Media.UploadMediaQuery,
|
2021-07-20 10:40:04 +02:00
|
|
|
mastodonAuthenticationBox: MastodonAuthenticationBox,
|
2021-07-15 11:25:15 +02:00
|
|
|
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(
|
2021-03-18 12:42:26 +01:00
|
|
|
domain: String,
|
2021-06-14 23:09:50 +02:00
|
|
|
query: Mastodon.API.Media.UploadMediaQuery,
|
2021-07-20 10:40:04 +02:00
|
|
|
mastodonAuthenticationBox: MastodonAuthenticationBox
|
2021-03-18 12:42:26 +01:00
|
|
|
) -> AnyPublisher<Mastodon.Response.Content<Mastodon.Entity.Attachment>, Error> {
|
|
|
|
let authorization = mastodonAuthenticationBox.userAuthorization
|
|
|
|
|
|
|
|
return Mastodon.API.Media.uploadMedia(
|
|
|
|
session: session,
|
|
|
|
domain: domain,
|
|
|
|
query: query,
|
|
|
|
authorization: authorization
|
|
|
|
)
|
|
|
|
}
|
2021-07-15 11:25:15 +02:00
|
|
|
|
|
|
|
private func uploadMediaV2(
|
|
|
|
domain: String,
|
|
|
|
query: Mastodon.API.Media.UploadMediaQuery,
|
2021-07-20 10:40:04 +02:00
|
|
|
mastodonAuthenticationBox: MastodonAuthenticationBox
|
2021-07-15 11:25:15 +02:00
|
|
|
) -> 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()
|
|
|
|
}
|
2021-07-20 10:40:04 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-08-09 11:02:32 +02:00
|
|
|
extension APIService {
|
|
|
|
|
|
|
|
func getMedia(
|
|
|
|
attachmentID: Mastodon.Entity.Attachment.ID,
|
|
|
|
mastodonAuthenticationBox: MastodonAuthenticationBox
|
|
|
|
) -> AnyPublisher<Mastodon.Response.Content<Mastodon.Entity.Attachment>, Error> {
|
|
|
|
let authorization = mastodonAuthenticationBox.userAuthorization
|
|
|
|
|
|
|
|
return Mastodon.API.Media.getMedia(
|
|
|
|
session: session,
|
|
|
|
domain: mastodonAuthenticationBox.domain,
|
|
|
|
attachmentID: attachmentID,
|
|
|
|
authorization: authorization
|
|
|
|
)
|
|
|
|
.eraseToAnyPublisher()
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-07-20 10:40:04 +02:00
|
|
|
extension APIService {
|
2021-03-18 12:42:26 +01:00
|
|
|
|
2021-03-19 12:49:48 +01:00
|
|
|
func updateMedia(
|
|
|
|
domain: String,
|
|
|
|
attachmentID: Mastodon.Entity.Attachment.ID,
|
|
|
|
query: Mastodon.API.Media.UpdateMediaQuery,
|
2021-07-20 10:40:04 +02:00
|
|
|
mastodonAuthenticationBox: MastodonAuthenticationBox
|
2021-03-19 12:49:48 +01:00
|
|
|
) -> AnyPublisher<Mastodon.Response.Content<Mastodon.Entity.Attachment>, Error> {
|
|
|
|
let authorization = mastodonAuthenticationBox.userAuthorization
|
|
|
|
|
|
|
|
return Mastodon.API.Media.updateMedia(
|
|
|
|
session: session,
|
|
|
|
domain: domain,
|
|
|
|
attachmentID: attachmentID,
|
|
|
|
query: query,
|
|
|
|
authorization: authorization
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-03-18 12:42:26 +01:00
|
|
|
}
|