2018-09-11 14:52:21 +00:00
|
|
|
//
|
|
|
|
// Attachment.swift
|
|
|
|
// Pachyderm
|
|
|
|
//
|
|
|
|
// Created by Shadowfacts on 9/9/18.
|
|
|
|
// Copyright © 2018 Shadowfacts. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
|
2020-04-11 19:33:39 +00:00
|
|
|
public class Attachment: Codable {
|
2018-09-11 14:52:21 +00:00
|
|
|
public let id: String
|
|
|
|
public let kind: Kind
|
|
|
|
public let url: URL
|
|
|
|
public let remoteURL: URL?
|
2020-07-04 02:13:49 +00:00
|
|
|
public let previewURL: URL?
|
2018-09-11 14:52:21 +00:00
|
|
|
public let meta: Metadata?
|
2018-09-17 23:22:37 +00:00
|
|
|
public let description: String?
|
2020-09-12 14:48:27 +00:00
|
|
|
public let blurHash: String?
|
2018-09-11 14:52:21 +00:00
|
|
|
|
2018-09-17 23:22:37 +00:00
|
|
|
public static func update(_ attachment: Attachment, focus: (Float, Float)?, description: String?) -> Request<Attachment> {
|
2020-09-15 03:25:26 +00:00
|
|
|
return Request<Attachment>(method: .put, path: "/api/v1/media/\(attachment.id)", body: FormDataBody([
|
2018-09-17 23:22:37 +00:00
|
|
|
"description" => (description ?? attachment.description),
|
2018-09-11 14:52:21 +00:00
|
|
|
"focus" => focus
|
|
|
|
], nil))
|
|
|
|
}
|
|
|
|
|
2019-01-14 00:45:57 +00:00
|
|
|
required public init(from decoder: Decoder) throws {
|
|
|
|
let container = try decoder.container(keyedBy: CodingKeys.self)
|
|
|
|
self.id = try container.decode(String.self, forKey: .id)
|
|
|
|
self.kind = try container.decode(Kind.self, forKey: .kind)
|
2020-04-12 16:52:51 +00:00
|
|
|
self.url = try container.decode(URL.self, forKey: .url)
|
2020-07-04 02:13:49 +00:00
|
|
|
self.previewURL = try? container.decode(URL?.self, forKey: .previewURL)
|
|
|
|
self.remoteURL = try? container.decode(URL?.self, forKey: .remoteURL)
|
|
|
|
self.meta = try? container.decode(Metadata?.self, forKey: .meta)
|
|
|
|
self.description = try? container.decode(String?.self, forKey: .description)
|
2020-09-12 14:48:27 +00:00
|
|
|
self.blurHash = try? container.decode(String?.self, forKey: .blurHash)
|
2019-01-14 00:45:57 +00:00
|
|
|
}
|
|
|
|
|
2018-09-11 14:52:21 +00:00
|
|
|
private enum CodingKeys: String, CodingKey {
|
|
|
|
case id
|
|
|
|
case kind = "type"
|
|
|
|
case url
|
|
|
|
case remoteURL = "remote_url"
|
|
|
|
case previewURL = "preview_url"
|
|
|
|
case meta
|
|
|
|
case description
|
2020-09-12 14:48:27 +00:00
|
|
|
case blurHash = "blurhash"
|
2018-09-11 14:52:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extension Attachment {
|
2020-04-11 19:33:39 +00:00
|
|
|
public enum Kind: String, Codable {
|
2018-09-11 14:52:21 +00:00
|
|
|
case image
|
|
|
|
case video
|
|
|
|
case gifv
|
|
|
|
case audio
|
|
|
|
case unknown
|
2022-02-17 03:12:56 +00:00
|
|
|
|
|
|
|
public init(from decoder: Decoder) throws {
|
|
|
|
let container = try decoder.singleValueContainer()
|
|
|
|
switch try container.decode(String.self) {
|
|
|
|
// gotosocial uses "gif" for gif images
|
|
|
|
case "image", "gif":
|
|
|
|
self = .image
|
|
|
|
case "video":
|
|
|
|
self = .video
|
|
|
|
case "gifv":
|
|
|
|
self = .gifv
|
|
|
|
case "audio":
|
|
|
|
self = .audio
|
|
|
|
default:
|
|
|
|
self = .unknown
|
|
|
|
}
|
|
|
|
}
|
2018-09-11 14:52:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extension Attachment {
|
2020-09-12 14:48:27 +00:00
|
|
|
public struct Metadata: Codable {
|
2018-09-11 14:52:21 +00:00
|
|
|
public let length: String?
|
|
|
|
public let duration: Float?
|
|
|
|
public let audioEncoding: String?
|
|
|
|
public let audioBitrate: String?
|
|
|
|
public let audioChannels: String?
|
|
|
|
public let fps: Float?
|
|
|
|
public let width: Int?
|
|
|
|
public let height: Int?
|
|
|
|
public let size: String?
|
|
|
|
public let aspect: Float?
|
|
|
|
|
|
|
|
public let small: ImageMetadata?
|
|
|
|
public let original: ImageMetadata?
|
|
|
|
|
|
|
|
private enum CodingKeys: String, CodingKey {
|
|
|
|
case length
|
|
|
|
case duration
|
|
|
|
case audioEncoding = "audio_encode"
|
|
|
|
case audioBitrate = "audio_bitrate"
|
|
|
|
case audioChannels = "audio_channels"
|
|
|
|
case fps
|
|
|
|
case width
|
|
|
|
case height
|
|
|
|
case size
|
|
|
|
case aspect
|
|
|
|
case small
|
|
|
|
case original
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-12 14:48:27 +00:00
|
|
|
public struct ImageMetadata: Codable {
|
2018-09-11 14:52:21 +00:00
|
|
|
public let width: Int?
|
|
|
|
public let height: Int?
|
|
|
|
public let size: String?
|
|
|
|
public let aspect: Float?
|
|
|
|
|
|
|
|
private enum CodingKeys: String, CodingKey {
|
|
|
|
case width
|
|
|
|
case height
|
|
|
|
case size
|
|
|
|
case aspect
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|