diff --git a/mediarepo-daemon/mediarepo-model/src/models/file.rs b/mediarepo-daemon/mediarepo-model/src/models/file.rs index b4a310a..00e8ff2 100644 --- a/mediarepo-daemon/mediarepo-model/src/models/file.rs +++ b/mediarepo-daemon/mediarepo-model/src/models/file.rs @@ -1,9 +1,130 @@ +use crate::models::file_type::FileType; +use crate::models::storage::Storage; +use chrono::NaiveDateTime; +use mediarepo_core::error::RepoResult; use mediarepo_database::entities::file; +use mediarepo_database::entities::file::ActiveModel as ActiveFile; use mediarepo_database::entities::file::Model as FileModel; -use sea_orm::DatabaseConnection; +use mediarepo_database::entities::hash; +use mediarepo_database::entities::hash::Model as HashModel; +use sea_orm::prelude::*; +use sea_orm::{DatabaseConnection, Set}; use tokio::fs; pub struct File { db: DatabaseConnection, model: FileModel, + hash: HashModel, +} + +impl File { + fn new(db: DatabaseConnection, model: FileModel, hash: HashModel) -> Self { + Self { db, model, hash } + } + + /// Fetches the file by id + pub async fn by_id(db: DatabaseConnection, id: u64) -> RepoResult> { + if let Some((model, Some(hash))) = file::Entity::find_by_id(id) + .find_also_related(hash::Entity) + .one(&db) + .await? + { + let file = File::new(db, model, hash); + Ok(Some(file)) + } else { + Ok(None) + } + } + + /// Returns the unique identifier of the file + pub fn id(&self) -> u64 { + self.model.id + } + + /// Returns the hash of the file (content identifier) + pub fn hash(&self) -> &String { + &self.hash.value + } + + /// Returns the type of the file + pub fn file_type(&self) -> FileType { + match self.model.file_type { + 1 => FileType::Image, + 2 => FileType::Video, + 3 => FileType::Audio, + _ => FileType::Unknown, + } + } + + /// Returns the optional name of the file + pub fn name(&self) -> &Option { + &self.model.name + } + + /// Returns the comment of the file + pub fn comment(&self) -> &Option { + &self.model.comment + } + + /// Returns the import time of the file + pub fn import_time(&self) -> &NaiveDateTime { + &self.model.import_time + } + + /// Returns the datetime when the file was created + pub fn creation_time(&self) -> &NaiveDateTime { + &self.model.creation_time + } + + /// Returns the last time the file was changed + pub fn change_time(&self) -> &NaiveDateTime { + &self.model.change_time + } + + /// Returns the storage where the file is stored + pub async fn storage(&self) -> RepoResult { + let storage = Storage::by_id(self.db.clone(), self.model.storage_id) + .await? + .expect("The FK storage_id doesn't exist?!"); + + Ok(storage) + } + + /// Changes the name of the file + pub async fn set_name(&mut self, name: S) -> RepoResult<()> { + let mut active_file = self.get_active_model(); + active_file.name = Set(Some(name.to_string())); + let active_file = active_file.update(&self.db).await?; + self.model.name = active_file.name.unwrap(); + + Ok(()) + } + + /// Changes the comment of the file + pub async fn set_comment(&mut self, comment: S) -> RepoResult<()> { + let mut active_file = self.get_active_model(); + active_file.comment = Set(Some(comment.to_string())); + let active_file = active_file.update(&self.db).await?; + self.model.comment = active_file.comment.unwrap(); + + Ok(()) + } + + /// Changes the type of the file + pub async fn set_file_type(&mut self, file_type: FileType) -> RepoResult<()> { + let mut active_file = self.get_active_model(); + active_file.file_type = Set(file_type as u32); + let active_file = active_file.update(&self.db).await?; + self.model.file_type = active_file.file_type.unwrap(); + + Ok(()) + } + + /// Returns the active model of the file with only the id set + fn get_active_model(&self) -> ActiveFile { + ActiveFile { + id: Set(self.id()), + ..Default::default() + } + } } diff --git a/mediarepo-daemon/mediarepo-model/src/models/file_type.rs b/mediarepo-daemon/mediarepo-model/src/models/file_type.rs new file mode 100644 index 0000000..74b2829 --- /dev/null +++ b/mediarepo-daemon/mediarepo-model/src/models/file_type.rs @@ -0,0 +1,6 @@ +pub enum FileType { + Unknown = 0, + Image = 1, + Video = 2, + Audio = 3, +} diff --git a/mediarepo-daemon/mediarepo-model/src/models/mod.rs b/mediarepo-daemon/mediarepo-model/src/models/mod.rs index c0a1caf..0d43719 100644 --- a/mediarepo-daemon/mediarepo-model/src/models/mod.rs +++ b/mediarepo-daemon/mediarepo-model/src/models/mod.rs @@ -1,2 +1,3 @@ pub mod file; +pub mod file_type; pub mod storage;