Skip to main content

rs_plugin_common_interfaces/domain/
media.rs

1use std::str::FromStr;
2
3use crate::{RsRequest, url::RsLink};
4use serde::{Deserialize, Serialize};
5use serde_json::Value;
6use strum_macros::EnumString;
7
8use crate::domain::backup::BackupFile;
9
10pub const DEFAULT_MIME: &str = "application/octet-stream";
11
12#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
13#[serde(rename_all = "camelCase")]
14pub struct FileEpisode {
15    pub id: String,
16    #[serde(skip_serializing_if = "Option::is_none")]
17    pub season: Option<u32>,
18    #[serde(skip_serializing_if = "Option::is_none")]
19    pub episode: Option<u32>,
20    #[serde(skip_serializing_if = "Option::is_none")]
21    pub episode_to: Option<u32>,
22}
23
24impl FromStr for FileEpisode {
25    type Err = String;
26
27    fn from_str(s: &str) -> Result<Self, Self::Err> {
28        let splitted: Vec<&str> = s.split("|").collect();
29        if splitted.len() == 3 {
30            Ok(FileEpisode {
31                id: splitted[0].to_string(),
32                season: splitted[1].parse::<u32>().ok().and_then(|i| {
33                    if i == 0 {
34                        None
35                    } else {
36                        Some(i)
37                    }
38                }),
39                episode: splitted[2].parse::<u32>().ok().and_then(|i| {
40                    if i == 0 {
41                        None
42                    } else {
43                        Some(i)
44                    }
45                }),
46                episode_to: None,
47            })
48        } else if splitted.len() == 2 {
49            Ok(FileEpisode {
50                id: splitted[0].to_string(),
51                season: splitted[1].parse::<u32>().ok().and_then(|i| {
52                    if i == 0 {
53                        None
54                    } else {
55                        Some(i)
56                    }
57                }),
58                episode: None,
59                episode_to: None,
60            })
61        } else {
62            Ok(FileEpisode {
63                id: splitted[0].to_string(),
64                season: None,
65                episode: None,
66                episode_to: None,
67            })
68        }
69    }
70}
71
72#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
73pub struct MediaItemReference {
74    pub id: String,
75    #[serde(skip_serializing_if = "Option::is_none")]
76    pub conf: Option<u16>,
77}
78
79impl FromStr for MediaItemReference {
80    type Err = String;
81
82    fn from_str(s: &str) -> Result<Self, Self::Err> {
83        let splitted: Vec<&str> = s.split('|').collect();
84        if splitted.len() == 2 {
85            Ok(MediaItemReference {
86                id: splitted[0].to_string(),
87                conf: splitted[1].parse::<u16>().ok().and_then(|e| {
88                    if e == 100 {
89                        None
90                    } else {
91                        Some(e)
92                    }
93                }),
94            })
95        } else {
96            Ok(MediaItemReference {
97                id: splitted[0].to_string(),
98                conf: None,
99            })
100        }
101    }
102}
103
104#[derive(
105    Debug, Serialize, Deserialize, Clone, PartialEq, strum_macros::Display, EnumString, Default,
106)]
107#[strum(serialize_all = "camelCase")]
108#[serde(rename_all = "camelCase")]
109pub enum FileType {
110    Directory,
111    Photo,
112    Video,
113    Archive,
114    Album,
115    Book,
116    #[default]
117    Other,
118}
119
120#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Default)]
121#[serde(rename_all = "camelCase")]
122pub struct Media {
123    pub id: String,
124    #[serde(skip_serializing_if = "Option::is_none")]
125    pub source: Option<String>,
126    pub name: String,
127    #[serde(skip_serializing_if = "Option::is_none")]
128    pub description: Option<String>,
129
130    #[serde(rename = "type")]
131    pub kind: FileType,
132    pub mimetype: String,
133    pub size: Option<u64>,
134
135    #[serde(skip_serializing_if = "Option::is_none")]
136    pub params: Option<Value>,
137
138    pub added: Option<i64>,
139    pub modified: Option<i64>,
140    pub created: Option<i64>,
141
142    #[serde(skip_serializing_if = "Option::is_none")]
143    pub rating: Option<f32>,
144    #[serde(skip_serializing_if = "Option::is_none")]
145    pub avg_rating: Option<f32>,
146
147    #[serde(skip_serializing_if = "Option::is_none")]
148    pub md5: Option<String>,
149
150    #[serde(skip_serializing_if = "Option::is_none")]
151    pub width: Option<usize>,
152    #[serde(skip_serializing_if = "Option::is_none")]
153    pub height: Option<usize>,
154    #[serde(skip_serializing_if = "Option::is_none")]
155    pub phash: Option<String>,
156    #[serde(skip_serializing_if = "Option::is_none")]
157    pub thumbhash: Option<String>,
158    #[serde(skip_serializing_if = "Option::is_none")]
159    pub focal: Option<u64>,
160    #[serde(skip_serializing_if = "Option::is_none")]
161    pub iso: Option<u64>,
162    #[serde(skip_serializing_if = "Option::is_none")]
163    pub color_space: Option<String>,
164    #[serde(skip_serializing_if = "Option::is_none")]
165    pub icc: Option<String>,
166    #[serde(skip_serializing_if = "Option::is_none")]
167    pub mp: Option<u32>,
168    #[serde(skip_serializing_if = "Option::is_none")]
169    pub sspeed: Option<String>,
170    #[serde(skip_serializing_if = "Option::is_none")]
171    pub f_number: Option<f64>,
172    #[serde(skip_serializing_if = "Option::is_none")]
173    pub orientation: Option<usize>,
174
175    #[serde(skip_serializing_if = "Option::is_none")]
176    pub duration: Option<usize>,
177    #[serde(skip_serializing_if = "Option::is_none")]
178    pub acodecs: Option<Vec<String>>,
179    #[serde(skip_serializing_if = "Option::is_none")]
180    pub achan: Option<Vec<usize>>,
181    #[serde(skip_serializing_if = "Option::is_none")]
182    pub vcodecs: Option<Vec<String>>,
183    #[serde(skip_serializing_if = "Option::is_none")]
184    pub fps: Option<f64>,
185    #[serde(skip_serializing_if = "Option::is_none")]
186    pub bitrate: Option<u64>,
187
188    #[serde(skip_serializing_if = "Option::is_none")]
189    pub long: Option<f64>,
190    #[serde(skip_serializing_if = "Option::is_none")]
191    pub lat: Option<f64>,
192    #[serde(skip_serializing_if = "Option::is_none")]
193    pub model: Option<String>,
194
195    #[serde(skip_serializing_if = "Option::is_none")]
196    pub pages: Option<usize>,
197
198    #[serde(skip_serializing_if = "Option::is_none")]
199    pub progress: Option<usize>,
200    #[serde(skip_serializing_if = "Option::is_none")]
201    pub faces: Option<Vec<FaceEmbedding>>,
202    #[serde(skip_serializing_if = "Option::is_none")]
203    pub backups: Option<Vec<BackupFile>>,
204    #[serde(skip_serializing_if = "Option::is_none")]
205    pub thumb: Option<String>,
206    #[serde(skip_serializing_if = "Option::is_none")]
207    pub thumbv: Option<usize>,
208    #[serde(skip_serializing_if = "Option::is_none")]
209    pub thumbsize: Option<u64>,
210    #[serde(skip_serializing_if = "Option::is_none")]
211    pub iv: Option<String>,
212    #[serde(skip_serializing_if = "Option::is_none")]
213    pub origin: Option<RsLink>,
214    #[serde(skip_serializing_if = "Option::is_none")]
215    pub lang: Option<String>,
216    #[serde(skip_serializing_if = "Option::is_none")]
217    pub uploader: Option<String>,
218    #[serde(skip_serializing_if = "Option::is_none")]
219    pub uploadkey: Option<String>,
220
221    #[serde(skip_serializing_if = "Option::is_none")]
222    pub original_hash: Option<String>,
223    #[serde(skip_serializing_if = "Option::is_none")]
224    pub original_id: Option<String>,
225
226    #[serde(skip_serializing_if = "Option::is_none")]
227    pub face_recognition_error: Option<String>,
228}
229
230impl Media {
231    pub fn max_date(&self) -> i64 {
232        *[
233            self.created.unwrap_or(0),
234            self.added.unwrap_or(0),
235            self.modified.unwrap_or(0),
236        ]
237        .iter()
238        .max()
239        .unwrap_or(&0)
240    }
241
242    pub fn bytes_size(&self) -> Option<u64> {
243        if self.iv.is_none() {
244            self.size
245        } else {
246            //16 Bytes to store IV
247            //4 to store encrypted thumb size = T (can be 0)
248            //4 to store encrypted Info size = I (can be 0)
249            //32 to store thumb mimetype
250            //256 to store file mimetype
251            //T Bytes for the encrypted thumb
252            //I Bytes for the encrypted info
253            if let Some(file_size) = self.size {
254                Some(file_size + 16 + 4 + 4 + 32 + 256 + self.thumbsize.unwrap_or(0) + 0)
255            } else {
256                None
257            }
258        }
259    }
260}
261
262#[derive(Debug, Serialize, Deserialize, Clone, Default)]
263#[serde(rename_all = "camelCase")]
264pub struct RsGpsPosition {
265    pub lat: f64,
266    pub long: f64,
267}
268
269#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
270#[serde(rename_all = "camelCase")]
271pub struct FaceEmbedding {
272    pub id: String,
273    pub embedding: Vec<f32>,
274    pub media_ref: Option<String>,
275    pub bbox: Option<FaceBBox>,
276    pub confidence: Option<f32>,
277    pub pose: Option<(f32, f32, f32)>,
278    pub person_id: Option<String>,
279}
280
281/// A face found by `POST /libraries/{library}/medias/{id}/faces` (the media's faces
282/// detected again): its id once stored, how sure the detector is (0 to 1) and where it
283/// is. The person it may have been given to is read with the media's faces.
284#[derive(Debug, Serialize, Deserialize, Clone, Default, PartialEq)]
285#[serde(rename_all = "camelCase")]
286pub struct DetectedFaceResult {
287    pub face_id: Option<String>,
288    pub confidence: f32,
289    pub bbox: FaceBBox,
290}
291
292/// Body of `POST /libraries/{library}/people/faces/assign`: gives the faces to a person
293/// (taking them from the person they had, if any).
294#[derive(Debug, Serialize, Deserialize, Clone, Default, PartialEq)]
295#[serde(rename_all = "camelCase")]
296pub struct FaceAssign {
297    pub face_ids: Vec<String>,
298    pub person_id: String,
299}
300
301/// Body of `POST /libraries/{library}/people/faces/unassign`: takes the faces from their
302/// person.
303#[derive(Debug, Serialize, Deserialize, Clone, Default, PartialEq)]
304#[serde(rename_all = "camelCase")]
305pub struct FaceUnassign {
306    pub face_ids: Vec<String>,
307}
308
309#[derive(Debug, Serialize, Deserialize, Clone, Default, PartialEq)]
310#[serde(rename_all = "camelCase")]
311pub struct FaceBBox {
312    pub x1: f32,
313    pub y1: f32,
314    pub x2: f32,
315    pub y2: f32,
316    #[serde(skip_serializing_if = "Option::is_none")]
317    pub video_s: Option<f32>, // Seconds in video where face was detected
318    #[serde(skip_serializing_if = "Option::is_none")]
319    pub video_percent: Option<u32>, // Percent (0-100) of video where face was detected
320}
321
322
323
324
325
326
327
328#[derive(Debug, Serialize, Deserialize, Clone, Default, PartialEq)]
329#[serde(rename_all = "camelCase")]
330pub struct MediaForUpdate {
331    pub name: Option<String>,
332    pub description: Option<String>,
333    pub mimetype: Option<String>,
334    pub kind: Option<FileType>,
335    pub size: Option<u64>,
336
337    pub md5: Option<String>,
338
339    pub modified: Option<i64>,
340    pub created: Option<i64>,
341
342    pub width: Option<u32>,
343    pub height: Option<u32>,
344    pub orientation: Option<u8>,
345    pub color_space: Option<String>,
346    pub icc: Option<String>,
347    pub mp: Option<u32>,
348    pub vcodecs: Option<Vec<String>>,
349    pub acodecs: Option<Vec<String>>,
350    pub fps: Option<f64>,
351    pub bitrate: Option<u64>,
352    pub focal: Option<u64>,
353    pub iso: Option<u64>,
354    pub model: Option<String>,
355    pub sspeed: Option<String>,
356    pub f_number: Option<f64>,
357
358    pub pages: Option<usize>,
359
360    pub duration: Option<u64>,
361
362    pub progress: Option<usize>,
363
364    pub add_tags: Option<Vec<MediaItemReference>>,
365    pub remove_tags: Option<Vec<String>>,
366    pub tags_lookup: Option<Vec<String>>,
367
368    pub add_series: Option<Vec<FileEpisode>>,
369    pub remove_series: Option<Vec<FileEpisode>>,
370    pub series_lookup: Option<Vec<String>>,
371    pub season: Option<u32>,
372    pub episode: Option<u32>,
373
374    pub add_people: Option<Vec<MediaItemReference>>,
375    pub remove_people: Option<Vec<String>>,
376    pub people_lookup: Option<Vec<String>>,
377
378    pub long: Option<f64>,
379    pub lat: Option<f64>,
380    pub gps: Option<String>,
381
382    pub origin: Option<RsLink>,
383    pub origin_url: Option<String>,
384    #[serde(default)]
385    pub ignore_origin_duplicate: bool,
386
387    pub movie: Option<String>,
388    pub book: Option<String>,
389    /// Takes the media from this movie, if it is the media's one (`movie` wins).
390    #[serde(skip_serializing_if = "Option::is_none")]
391    pub remove_movie: Option<String>,
392    /// Takes the media from this book, if it is the media's one (`book` wins).
393    #[serde(skip_serializing_if = "Option::is_none")]
394    pub remove_book: Option<String>,
395
396    pub lang: Option<String>,
397
398    pub rating: Option<u16>,
399
400    pub thumbsize: Option<usize>,
401    pub iv: Option<String>,
402
403    pub uploader: Option<String>,
404    pub uploadkey: Option<String>,
405    pub upload_id: Option<String>,
406
407    pub original_hash: Option<String>,
408    pub original_id: Option<String>,
409}
410
411
412impl MediaForUpdate {
413    /// Merge `patch` into `self`.
414    ///
415    /// Semantics:
416    /// - For most `Option<T>` fields: overwrite only when `patch.field` is `Some`.
417    /// - For `movie` / `remove_movie` (and `book` / `remove_book`): the patch's pair
418    ///   replaces the one before when either is `Some`.
419    /// - For "list-like update" fields (`add_*`, `remove_*`, `*_lookup`): append vectors.
420    /// - For `ignore_origin_duplicate` (bool): OR (once true, stays true).
421    pub fn merge_from(&mut self, mut patch: Self) {
422        // Overwrite `dst` only if `src` is Some(..)
423        fn overwrite_if_some<T>(dst: &mut Option<T>, src: &mut Option<T>) {
424            if src.is_some() {
425                *dst = src.take();
426            }
427        }
428
429        // Append vectors when both sides are Some(vec).
430        // If `dst` is None, it becomes src (if any).
431        fn append_vec<T>(dst: &mut Option<Vec<T>>, src: &mut Option<Vec<T>>) {
432            match (dst.as_mut(), src.take()) {
433                (Some(d), Some(mut s)) => d.append(&mut s),
434                (None, Some(s)) => *dst = Some(s),
435                _ => {}
436            }
437        }
438
439        // ----- Scalar / metadata fields (overwrite-if-some) -----
440        overwrite_if_some(&mut self.name, &mut patch.name);
441        overwrite_if_some(&mut self.description, &mut patch.description);
442        overwrite_if_some(&mut self.mimetype, &mut patch.mimetype);
443        overwrite_if_some(&mut self.kind, &mut patch.kind);
444        overwrite_if_some(&mut self.size, &mut patch.size);
445
446        overwrite_if_some(&mut self.md5, &mut patch.md5);
447
448        overwrite_if_some(&mut self.modified, &mut patch.modified);
449        overwrite_if_some(&mut self.created, &mut patch.created);
450
451        overwrite_if_some(&mut self.width, &mut patch.width);
452        overwrite_if_some(&mut self.height, &mut patch.height);
453        overwrite_if_some(&mut self.orientation, &mut patch.orientation);
454        overwrite_if_some(&mut self.color_space, &mut patch.color_space);
455        overwrite_if_some(&mut self.icc, &mut patch.icc);
456        overwrite_if_some(&mut self.mp, &mut patch.mp);
457        overwrite_if_some(&mut self.vcodecs, &mut patch.vcodecs);
458        overwrite_if_some(&mut self.acodecs, &mut patch.acodecs);
459        overwrite_if_some(&mut self.fps, &mut patch.fps);
460        overwrite_if_some(&mut self.bitrate, &mut patch.bitrate);
461        overwrite_if_some(&mut self.focal, &mut patch.focal);
462        overwrite_if_some(&mut self.iso, &mut patch.iso);
463        overwrite_if_some(&mut self.model, &mut patch.model);
464        overwrite_if_some(&mut self.sspeed, &mut patch.sspeed);
465        overwrite_if_some(&mut self.f_number, &mut patch.f_number);
466
467        overwrite_if_some(&mut self.pages, &mut patch.pages);
468        overwrite_if_some(&mut self.duration, &mut patch.duration);
469        overwrite_if_some(&mut self.progress, &mut patch.progress);
470
471        overwrite_if_some(&mut self.season, &mut patch.season);
472        overwrite_if_some(&mut self.episode, &mut patch.episode);
473
474        overwrite_if_some(&mut self.long, &mut patch.long);
475        overwrite_if_some(&mut self.lat, &mut patch.lat);
476        overwrite_if_some(&mut self.gps, &mut patch.gps);
477
478        overwrite_if_some(&mut self.origin, &mut patch.origin);
479        overwrite_if_some(&mut self.origin_url, &mut patch.origin_url);
480
481        // A patch's movie, set or removed, replaces the one before: a later removal
482        // wins over an earlier set (which would win in the same update).
483        if patch.movie.is_some() || patch.remove_movie.is_some() {
484            self.movie = patch.movie.take();
485            self.remove_movie = patch.remove_movie.take();
486        }
487        if patch.book.is_some() || patch.remove_book.is_some() {
488            self.book = patch.book.take();
489            self.remove_book = patch.remove_book.take();
490        }
491
492        overwrite_if_some(&mut self.lang, &mut patch.lang);
493        overwrite_if_some(&mut self.rating, &mut patch.rating);
494
495        overwrite_if_some(&mut self.thumbsize, &mut patch.thumbsize);
496        overwrite_if_some(&mut self.iv, &mut patch.iv);
497
498        overwrite_if_some(&mut self.uploader, &mut patch.uploader);
499        overwrite_if_some(&mut self.uploadkey, &mut patch.uploadkey);
500        overwrite_if_some(&mut self.upload_id, &mut patch.upload_id);
501
502        overwrite_if_some(&mut self.original_hash, &mut patch.original_hash);
503        overwrite_if_some(&mut self.original_id, &mut patch.original_id);
504
505        // ----- List-like update fields (append) -----
506        append_vec(&mut self.add_tags, &mut patch.add_tags);
507        append_vec(&mut self.remove_tags, &mut patch.remove_tags);
508        append_vec(&mut self.tags_lookup, &mut patch.tags_lookup);
509
510        append_vec(&mut self.add_series, &mut patch.add_series);
511        append_vec(&mut self.remove_series, &mut patch.remove_series);
512        append_vec(&mut self.series_lookup, &mut patch.series_lookup);
513
514        append_vec(&mut self.add_people, &mut patch.add_people);
515        append_vec(&mut self.remove_people, &mut patch.remove_people);
516        append_vec(&mut self.people_lookup, &mut patch.people_lookup);
517
518        // ----- Non-Option field -----
519        self.ignore_origin_duplicate |= patch.ignore_origin_duplicate;
520    }
521
522    /// Convenience: returns a merged value without mutating the original.
523    pub fn merged(mut self, patch: Self) -> Self {
524        self.merge_from(patch);
525        self
526    }
527}
528
529
530impl From<Media> for MediaForUpdate {
531    fn from(value: Media) -> Self {
532        MediaForUpdate {
533            description: value.description,
534            add_people: None,
535            add_tags: None,
536            long: value.long,
537            lat: value.lat,
538            created: value.created,
539            origin: value.origin,
540            add_series: None,
541            pages: value.pages,
542            original_hash: value.original_hash.or(value.md5),
543            original_id: Some(value.original_id.unwrap_or(value.id)),
544            book: None,
545            ..Default::default()
546        }
547    }
548}
549
550impl From<RsRequest> for MediaForUpdate {
551    fn from(value: RsRequest) -> Self {
552        // Build add_series from albums (first entry) + season + episode if albums are provided
553        let (add_series, season, episode) = if let Some(albums) = &value.albums {
554            if let Some(serie_id) = albums.first() {
555                // Have direct serie ID - use add_series, don't need season/episode at top level
556                (
557                    Some(vec![FileEpisode {
558                        id: serie_id.clone(),
559                        season: value.season,
560                        episode: value.episode,
561                        episode_to: None,
562                    }]),
563                    None,
564                    None,
565                )
566            } else {
567                (None, value.season, value.episode)
568            }
569        } else {
570            // No direct album IDs - keep season/episode for series_lookup pairing
571            (None, value.season, value.episode)
572        };
573
574        MediaForUpdate {
575            name: value.filename_or_extract_from_url(),
576            mimetype: value.mime,
577            description: value.description,
578            ignore_origin_duplicate: value.ignore_origin_duplicate,
579            size: value.size,
580            // Use the new lookup fields for database text search
581            people_lookup: value.people_lookup,
582            tags_lookup: value.tags_lookup,
583            series_lookup: value.albums_lookup,
584            add_series,
585            movie: value.movie,
586            season,
587            episode,
588            ..Default::default()
589        }
590    }
591}
592
593
594// region:    --- Upload, events and queries
595
596#[derive(Debug, Serialize, Deserialize, Clone, Default)]
597pub struct MediaForAdd {
598    pub source: Option<String>,
599    pub name: String,
600    pub description: Option<String>,
601
602    #[serde(rename = "type")]
603    pub kind: FileType,
604    pub mimetype: String,
605    pub size: Option<u64>,
606
607    #[serde(skip_serializing_if = "Option::is_none")]
608    pub params: Option<Value>,
609
610    pub rating: Option<u8>,
611    pub md5: Option<String>,
612
613    pub width: Option<usize>,
614    pub height: Option<usize>,
615    pub phash: Option<String>,
616    pub thumbhash: Option<String>,
617    pub focal: Option<u64>,
618    pub iso: Option<u64>,
619    pub color_space: Option<String>,
620    pub icc: Option<String>,
621    pub mp: Option<u32>,
622    pub sspeed: Option<String>,
623    pub f_number: Option<f64>,
624    pub orientation: Option<usize>,
625
626    pub duration: Option<usize>,
627    pub acodecs: Option<Vec<String>>,
628    pub achan: Option<Vec<usize>>,
629    pub vcodecs: Option<Vec<String>>,
630    pub fps: Option<f64>,
631    pub bitrate: Option<u64>,
632
633    pub long: Option<f64>,
634    pub lat: Option<f64>,
635    pub model: Option<String>,
636
637    pub pages: Option<usize>,
638
639    pub progress: Option<usize>,
640    pub tags: Option<Vec<String>>,
641    pub series: Option<Vec<FileEpisode>>,
642    pub people: Option<Vec<String>>,
643    pub thumb: Option<String>,
644    pub thumbv: Option<usize>,
645    pub thumbsize: Option<usize>,
646    pub iv: Option<String>,
647    pub origin: Option<RsLink>,
648    pub movie: Option<String>,
649    pub book: Option<String>,
650    pub lang: Option<String>,
651    pub uploader: Option<String>,
652    pub uploadkey: Option<String>,
653
654    pub original_hash: Option<String>,
655    pub original_id: Option<String>,
656
657    pub created: Option<i64>,
658}
659
660impl From<Media> for MediaForAdd {
661    fn from(value: Media) -> Self {
662        MediaForAdd {
663            name: value.name,
664            description: value.description,
665            people: None,
666            tags: None,
667            long: value.long,
668            lat: value.lat,
669            created: value.created,
670            origin: value.origin,
671            series: None,
672            original_hash: value.md5,
673            original_id: Some(value.original_id.unwrap_or(value.id)),
674            book: None,
675            ..Default::default()
676        }
677    }
678}
679
680#[derive(Debug, Serialize, Deserialize, Clone)]
681#[serde(rename_all = "camelCase")]
682pub struct MediaWithAction {
683    pub action: crate::domain::ElementAction,
684    pub media: crate::domain::ItemWithRelations<Media>,
685}
686
687#[derive(Debug, Serialize, Deserialize, Clone)]
688#[serde(rename_all = "camelCase")]
689pub struct MediasMessage {
690    pub library: String,
691    pub medias: Vec<MediaWithAction>,
692}
693
694#[derive(Debug, Serialize, Deserialize, Clone, Default)]
695#[serde(rename_all = "camelCase")]
696pub struct UploadProgressMessage {
697    pub library: String,
698    pub progress: crate::domain::progress::RsProgress,
699    pub remaining_secondes: Option<u64>,
700}
701
702#[derive(Debug, Serialize, Deserialize, Clone, Default)]
703#[serde(rename_all = "camelCase")]
704pub struct ConvertProgress {
705    pub id: String,
706    pub filename: String,
707    pub converted_id: Option<String>,
708    pub done: bool,
709    pub percent: f64,
710    pub status: crate::video::RsVideoTranscodeStatus,
711    pub remaining_secondes: Option<u64>,
712    pub request: Option<crate::video::VideoConvertRequest>,
713}
714
715#[derive(Debug, Serialize, Deserialize, Clone)]
716#[serde(rename_all = "camelCase")]
717pub struct ConvertMessage {
718    pub library: String,
719    pub progress: ConvertProgress,
720}
721
722#[derive(Debug, Serialize, Deserialize, Clone)]
723#[serde(rename_all = "camelCase")]
724pub struct VideoMergeItem {
725    pub media_id: String,
726    pub request: crate::video::VideoConvertRequest,
727}
728
729#[derive(Debug, Serialize, Deserialize, Clone)]
730#[serde(rename_all = "camelCase")]
731pub struct VideoMergeRequest {
732    pub id: String,
733    pub items: Vec<VideoMergeItem>,
734}
735
736/// Sort direction of a list.
737#[derive(
738    Debug, Serialize, Deserialize, Clone, PartialEq, strum_macros::Display, EnumString, Default,
739)]
740#[strum(serialize_all = "UPPERCASE")]
741#[serde(rename_all = "UPPERCASE")]
742pub enum SqlOrder {
743    ASC,
744    #[default]
745    DESC,
746}
747
748#[derive(
749    Debug, Serialize, Deserialize, Clone, PartialEq, strum_macros::Display, EnumString, Default,
750)]
751#[strum(serialize_all = "camelCase")]
752#[serde(rename_all = "camelCase")]
753pub enum RsSort {
754    #[default]
755    Modified,
756    Added,
757    Created,
758    Rating,
759    Name,
760    Size,
761}
762
763#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Default)]
764#[serde(rename_all = "camelCase")]
765pub struct RsSortOrder {
766    pub order: SqlOrder,
767    pub sort: RsSort,
768}
769
770/// Filter of a media list.
771#[derive(Debug, Serialize, Deserialize, Clone, Default)]
772#[serde(rename_all = "camelCase")]
773pub struct MediaQuery {
774    #[serde(default)]
775    pub sort: RsSort,
776    #[serde(default)]
777    pub order: SqlOrder,
778
779    pub added_before: Option<i64>,
780    pub added_after: Option<i64>,
781
782    pub created_before: Option<i64>,
783    pub created_after: Option<i64>,
784
785    pub modified_before: Option<i64>,
786    pub modified_after: Option<i64>,
787
788    pub before: Option<i64>,
789    pub after: Option<i64>,
790    #[serde(default)]
791    pub tags: Vec<String>,
792    #[serde(default)]
793    pub tags_confidence: Option<u16>,
794    #[serde(default)]
795    pub people: Vec<String>,
796    #[serde(default)]
797    pub series: Vec<String>,
798    pub movie: Option<String>,
799    pub book: Option<String>,
800    pub limit: Option<usize>,
801    #[serde(default)]
802    pub types: Vec<FileType>,
803
804    pub text: Option<String>,
805
806    pub long: Option<f64>,
807    pub lat: Option<f64>,
808    pub distance: Option<f64>,
809    #[serde(default)]
810    pub gps_square: Vec<f64>,
811
812    pub min_duration: Option<u64>,
813    pub max_duration: Option<u64>,
814
815    pub min_rating: Option<f32>,
816    pub max_rating: Option<f32>,
817
818    pub min_size: Option<u64>,
819    pub max_size: Option<u64>,
820
821    pub vcodec: Option<String>,
822
823    pub uploadkey: Option<String>,
824
825    pub page_key: Option<String>,
826
827    /// For legacy if user put serialized query in filter field
828    pub filter: Option<String>,
829}
830
831impl MediaQuery {
832    pub fn new_empty() -> MediaQuery {
833        MediaQuery {
834            tags: vec![],
835            ..Default::default()
836        }
837    }
838    pub fn from_after(after: i64) -> MediaQuery {
839        MediaQuery {
840            after: Some(after),
841            ..Default::default()
842        }
843    }
844}
845
846#[derive(Debug, Serialize, Deserialize, Clone, Default)]
847#[serde(rename_all = "camelCase")]
848pub struct MediaFileQuery {
849    #[serde(default)]
850    pub unsupported_mime: Vec<String>,
851
852    pub page: Option<u32>,
853
854    #[serde(default)]
855    pub raw: bool,
856}
857
858#[cfg(test)]
859mod upload_tests {
860    use super::*;
861
862    #[test]
863    fn convert_progress_serializes_upload_eta_field_name() {
864        let progress = ConvertProgress {
865            id: "convert-1".to_string(),
866            filename: "video.mp4".to_string(),
867            converted_id: None,
868            done: false,
869            percent: 0.5,
870            status: crate::video::RsVideoTranscodeStatus::Processing,
871            remaining_secondes: Some(42),
872            request: None,
873        };
874
875        let json = serde_json::to_string(&progress).unwrap();
876        assert!(json.contains("remainingSecondes"));
877        assert!(!json.contains("estimatedRemainingSeconds"));
878    }
879}
880
881// endregion: --- Upload, events and queries