Skip to main content

rs_plugin_common_interfaces/domain/
backup.rs

1use serde::{Deserialize, Serialize};
2
3#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)]
4#[serde(rename_all = "camelCase")]
5pub struct BackupFile {
6    pub backup: String,
7    pub library: Option<String>,
8    pub file: String,
9    pub id: String,
10    pub path: String,
11    pub hash: String,
12    pub sourcehash: String,
13    pub size: u64,
14    pub modified: i64,
15    pub added: i64,
16    pub iv: Option<String>,
17    pub thumb_size: Option<u64>,
18    pub info_size: Option<u64>,
19    pub error: Option<String>,
20}
21
22// region:    --- Backups
23
24#[derive(Debug, Serialize, Deserialize, Clone)]
25#[serde(rename_all = "camelCase")]
26pub struct Backup {
27    pub id: String,
28    pub name: String,
29    pub source: String,
30    pub plugin: Option<String>,
31    pub credentials: Option<String>,
32    pub library: Option<String>,
33    pub path: String,
34    /// Standard five-field cron expression evaluated in UTC.
35    pub schedule: Option<String>,
36    pub filter: Option<crate::domain::media::MediaQuery>,
37    pub last: Option<i64>,
38    pub password: Option<String>,
39    pub size: u64,
40    pub max_versions: u32,
41    pub max_database_versions: u32,
42}
43
44#[derive(Debug, Serialize, Deserialize, Clone)]
45#[serde(rename_all = "camelCase")]
46pub struct BackupError {
47    pub id: String,
48    pub backup: String,
49    pub library: String,
50    pub file: String,
51    pub date: i64,
52    pub error: String,
53}
54
55#[derive(
56    Debug,
57    Serialize,
58    Deserialize,
59    Clone,
60    PartialEq,
61    strum_macros::Display,
62    strum_macros::EnumString,
63    Default,
64)]
65#[strum(serialize_all = "camelCase")]
66#[serde(rename_all = "camelCase")]
67pub enum BackupStatus {
68    InProgress,
69    Done,
70    Error,
71    #[default]
72    Idle,
73}
74
75#[derive(Debug, Serialize, Deserialize, Clone)]
76#[serde(rename_all = "camelCase")]
77pub struct BackupProcessStatus {
78    pub library: Option<String>,
79    pub backup: String,
80    pub status: BackupStatus,
81    pub time: i64,
82    pub total: u64,
83    pub current: u64,
84    pub total_size: u64,
85    pub current_size: u64,
86
87    pub estimated_remaining_seconds: Option<u64>,
88}
89
90#[derive(Debug, Serialize, Deserialize, Clone)]
91#[serde(rename_all = "camelCase")]
92pub struct BackupWithStatus {
93    pub backup: Backup,
94    pub status: Option<BackupProcessStatus>,
95}
96
97impl BackupProcessStatus {
98    /// A backup in progress, at `time` (milliseconds since the Unix epoch).
99    pub fn new_from_backup_at(
100        backup: &Backup,
101        time: i64,
102        total: u64,
103        current: u64,
104        total_size: u64,
105        current_size: u64,
106    ) -> Self {
107        BackupProcessStatus {
108            library: backup.library.clone(),
109            backup: backup.id.clone(),
110            status: BackupStatus::InProgress,
111            time,
112            total,
113            current,
114            total_size,
115            current_size,
116
117            estimated_remaining_seconds: None,
118        }
119    }
120
121    pub fn new_from_backup_idle(backup: &Backup) -> Self {
122        Self::new_from_backup_with_status(backup, BackupStatus::Idle)
123    }
124
125    pub fn new_from_backup_done(backup: &Backup) -> Self {
126        Self::new_from_backup_with_status(backup, BackupStatus::Done)
127    }
128
129    pub fn new_from_backup_error(backup: &Backup) -> Self {
130        Self::new_from_backup_with_status(backup, BackupStatus::Error)
131    }
132
133    fn new_from_backup_with_status(backup: &Backup, status: BackupStatus) -> Self {
134        BackupProcessStatus {
135            library: backup.library.clone(),
136            backup: backup.id.clone(),
137            status,
138            time: 0,
139            total: 0,
140            current: 0,
141            total_size: 0,
142            current_size: 0,
143
144            estimated_remaining_seconds: None,
145        }
146    }
147}
148
149#[derive(Debug, Serialize, Deserialize, Clone)]
150#[serde(rename_all = "camelCase")]
151pub struct BackupFileProgress {
152    pub name: Option<String>,
153    pub backup: String,
154    pub library: Option<String>,
155    pub file: String,
156    pub id: String,
157    pub size: Option<u64>,
158    pub status: BackupStatus,
159    pub progress: u64,
160    pub error: Option<String>,
161
162    pub estimated_remaining_seconds: Option<u64>,
163}
164
165impl BackupFileProgress {
166    pub fn new_from(
167        backup: &Backup,
168        media: &crate::domain::media::Media,
169        id: String,
170        status: BackupStatus,
171        progress: u64,
172        error: Option<String>,
173    ) -> Self {
174        BackupFileProgress {
175            name: Some(media.name.clone()),
176            backup: backup.id.clone(),
177            library: backup.library.clone(),
178            file: media.id.clone(),
179            status,
180            id,
181            size: media.size,
182            progress,
183            error,
184            estimated_remaining_seconds: None,
185        }
186    }
187
188    #[allow(clippy::too_many_arguments)]
189    pub fn new(
190        file: String,
191        backup: String,
192        library: Option<String>,
193        name: Option<String>,
194        size: Option<u64>,
195        id: String,
196        status: BackupStatus,
197        progress: u64,
198        error: Option<String>,
199    ) -> Self {
200        BackupFileProgress {
201            name,
202            backup,
203            library,
204            file,
205            status,
206            id,
207            size,
208            progress,
209            error,
210            estimated_remaining_seconds: None,
211        }
212    }
213
214    pub fn new_light(
215        backup_id: String,
216        file_id: String,
217        id: String,
218        size: Option<u64>,
219        status: BackupStatus,
220        progress: u64,
221        error: Option<String>,
222    ) -> Self {
223        BackupFileProgress {
224            name: None,
225            backup: backup_id,
226            library: None,
227            file: file_id,
228            status,
229            id,
230            size,
231            progress,
232            error,
233            estimated_remaining_seconds: None,
234        }
235    }
236}
237
238#[derive(Debug, Serialize, Deserialize, Clone)]
239#[serde(rename_all = "camelCase")]
240pub struct BackupMessage {
241    pub action: crate::domain::ElementAction,
242    pub backup: BackupWithStatus,
243}
244
245// endregion: --- Backups