thunderstore_api/models/v2/
user_media.rs

1////////////////////////////////////////////////////////////////////////////////
2// This Source Code Form is subject to the terms of the Mozilla Public         /
3// License, v. 2.0. If a copy of the MPL was not distributed with this         /
4// file, You can obtain one at https://mozilla.org/MPL/2.0/.                   /
5////////////////////////////////////////////////////////////////////////////////
6
7use uuid::Uuid;
8
9#[derive(Clone, Debug, PartialEq, Eq, Default, Serialize, Deserialize)]
10pub struct UserMedia {
11    #[serde(skip_serializing_if = "Option::is_none")]
12    pub uuid: Option<Uuid>,
13    pub filename: String,
14    pub size: u32,
15    #[serde(skip_serializing_if = "Option::is_none")]
16    pub datetime_created: Option<String>,
17    #[serde(skip_serializing_if = "Option::is_none")]
18    pub expiry: Option<String>,
19    #[serde(skip_serializing_if = "Option::is_none")]
20    pub status: Option<Status>,
21}
22
23impl UserMedia {
24    #[must_use]
25    pub fn new(filename: String, size: u32) -> UserMedia {
26        UserMedia {
27            uuid: None,
28            filename,
29            size,
30            datetime_created: None,
31            expiry: None,
32            status: None,
33        }
34    }
35}
36
37///
38#[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd, Hash, Serialize, Deserialize)]
39pub enum Status {
40    #[serde(rename = "initial")]
41    Initial,
42    #[serde(rename = "upload_created")]
43    UploadCreated,
44    #[serde(rename = "upload_error")]
45    UploadError,
46    #[serde(rename = "upload_complete")]
47    UploadComplete,
48    #[serde(rename = "upload_aborted")]
49    UploadAborted,
50}
51
52impl Default for Status {
53    fn default() -> Status {
54        Self::Initial
55    }
56}
57
58#[derive(Clone, Debug, PartialEq, Eq, Default, Serialize, Deserialize)]
59pub struct InitiateUploadParams {
60    pub filename: String,
61    pub file_size_bytes: u32,
62}
63
64impl InitiateUploadParams {
65    #[must_use]
66    pub fn new(filename: String, file_size_bytes: u32) -> InitiateUploadParams {
67        InitiateUploadParams {
68            filename,
69            file_size_bytes,
70        }
71    }
72}
73
74#[derive(Clone, Debug, PartialEq, Eq, Default, Serialize, Deserialize)]
75pub struct InitiateUploadResponse {
76    pub user_media: UserMedia,
77    pub upload_urls: Vec<UploadPartUrl>,
78}
79
80impl InitiateUploadResponse {
81    #[must_use]
82    pub fn new(user_media: UserMedia, upload_urls: Vec<UploadPartUrl>) -> InitiateUploadResponse {
83        InitiateUploadResponse {
84            user_media,
85            upload_urls,
86        }
87    }
88}
89
90#[derive(Clone, Debug, PartialEq, Eq, Default, Serialize, Deserialize)]
91pub struct FinishUploadParams {
92    pub parts: Vec<CompletedPart>,
93}
94
95impl FinishUploadParams {
96    #[must_use]
97    pub fn new(parts: Vec<CompletedPart>) -> FinishUploadParams {
98        FinishUploadParams { parts }
99    }
100}
101
102#[derive(Clone, Debug, PartialEq, Eq, Default, Serialize, Deserialize)]
103pub struct UploadPartUrl {
104    pub part_number: i32,
105    pub url: String,
106    pub offset: i32,
107    pub length: i32,
108}
109
110impl UploadPartUrl {
111    #[must_use]
112    pub fn new(part_number: i32, url: String, offset: i32, length: i32) -> UploadPartUrl {
113        UploadPartUrl {
114            part_number,
115            url,
116            offset,
117            length,
118        }
119    }
120}
121
122#[derive(Clone, Debug, PartialEq, Eq, Default, Serialize, Deserialize)]
123pub struct CompletedPart {
124    #[serde(rename = "ETag")]
125    pub e_tag: String,
126    #[serde(rename = "PartNumber")]
127    pub part_number: i32,
128}
129
130impl CompletedPart {
131    #[must_use]
132    pub fn new(e_tag: String, part_number: i32) -> CompletedPart {
133        CompletedPart { e_tag, part_number }
134    }
135}