From 3d65a09e9b4845c5af7fb2c6bb048df5a35194f2 Mon Sep 17 00:00:00 2001 From: trivernis Date: Wed, 13 Oct 2021 20:30:36 +0200 Subject: [PATCH] Add hash model Signed-off-by: trivernis --- mediarepo-daemon/mediarepo-model/src/file.rs | 2 +- mediarepo-daemon/mediarepo-model/src/hash.rs | 75 +++++++++++++++++++ mediarepo-daemon/mediarepo-model/src/lib.rs | 1 + .../mediarepo-model/src/thumbnail.rs | 25 ++++++- 4 files changed, 101 insertions(+), 2 deletions(-) create mode 100644 mediarepo-daemon/mediarepo-model/src/hash.rs diff --git a/mediarepo-daemon/mediarepo-model/src/file.rs b/mediarepo-daemon/mediarepo-model/src/file.rs index b6fc7d7..81e06c9 100644 --- a/mediarepo-daemon/mediarepo-model/src/file.rs +++ b/mediarepo-daemon/mediarepo-model/src/file.rs @@ -19,7 +19,7 @@ pub struct File { } impl File { - fn new(db: DatabaseConnection, model: FileModel, hash: HashModel) -> Self { + pub(crate) fn new(db: DatabaseConnection, model: FileModel, hash: HashModel) -> Self { Self { db, model, hash } } diff --git a/mediarepo-daemon/mediarepo-model/src/hash.rs b/mediarepo-daemon/mediarepo-model/src/hash.rs new file mode 100644 index 0000000..536a590 --- /dev/null +++ b/mediarepo-daemon/mediarepo-model/src/hash.rs @@ -0,0 +1,75 @@ +use crate::file::File; +use crate::thumbnail::Thumbnail; +use mediarepo_core::error::RepoResult; +use mediarepo_database::entities::file; +use mediarepo_database::entities::hash; +use mediarepo_database::entities::thumbnail; +use sea_orm::prelude::*; +use sea_orm::{DatabaseConnection, Set}; + +pub struct Hash { + db: DatabaseConnection, + model: hash::Model, +} + +impl Hash { + pub(crate) fn new(db: DatabaseConnection, model: hash::Model) -> Self { + Self { db, model } + } + + /// Searches for the hash by id + pub async fn by_id(db: DatabaseConnection, id: i64) -> RepoResult> { + let hash = hash::Entity::find_by_id(id) + .one(&db) + .await? + .map(|model| Self::new(db, model)); + + Ok(hash) + } + + /// Adds a new hash to the database + pub async fn add(db: DatabaseConnection, value: String) -> RepoResult { + let active_model = hash::ActiveModel { + value: Set(value), + ..Default::default() + }; + let active_model: hash::ActiveModel = active_model.insert(&db).await?; + let hash = Self::by_id(db, active_model.id.unwrap()) + .await? + .expect("Inserted value does not exist"); + + Ok(hash) + } + + pub fn id(&self) -> i64 { + self.model.id + } + + pub fn value(&self) -> &String { + &self.model.value + } + + /// Returns the file associated with the hash + pub async fn file(&self) -> RepoResult> { + let file = self + .model + .find_related(file::Entity) + .one(&self.db) + .await? + .map(|file_model| File::new(self.db.clone(), file_model, self.model.clone())); + + Ok(file) + } + + /// Returns the the thumbnail associated with the hash + pub async fn thumbnail(&self) -> RepoResult> { + let thumbnail = self + .model + .find_related(thumbnail::Entity) + .one(&self.db) + .await? + .map(|thumb_model| Thumbnail::new(self.db.clone(), thumb_model, self.model.clone())); + + Ok(thumbnail) + } +} diff --git a/mediarepo-daemon/mediarepo-model/src/lib.rs b/mediarepo-daemon/mediarepo-model/src/lib.rs index e5883e5..05d3c70 100644 --- a/mediarepo-daemon/mediarepo-model/src/lib.rs +++ b/mediarepo-daemon/mediarepo-model/src/lib.rs @@ -1,5 +1,6 @@ pub mod file; pub mod file_type; +pub mod hash; pub mod repo; pub mod storage; pub mod thumbnail; diff --git a/mediarepo-daemon/mediarepo-model/src/thumbnail.rs b/mediarepo-daemon/mediarepo-model/src/thumbnail.rs index 08da218..0e90a81 100644 --- a/mediarepo-daemon/mediarepo-model/src/thumbnail.rs +++ b/mediarepo-daemon/mediarepo-model/src/thumbnail.rs @@ -3,7 +3,7 @@ use mediarepo_core::error::RepoResult; use mediarepo_database::entities::hash; use mediarepo_database::entities::thumbnail; use sea_orm::prelude::*; -use sea_orm::DatabaseConnection; +use sea_orm::{DatabaseConnection, Set}; pub struct Thumbnail { db: DatabaseConnection, @@ -31,6 +31,29 @@ impl Thumbnail { } } + /// Inserts a thumbnail into the database + pub async fn add( + db: DatabaseConnection, + hash_id: i64, + file_id: i64, + height: i32, + width: i32, + ) -> RepoResult { + let active_model = thumbnail::ActiveModel { + hash_id: Set(hash_id), + file_id: Set(file_id), + height: Set(height), + width: Set(width), + ..Default::default() + }; + let active_model: thumbnail::ActiveModel = active_model.insert(&db).await?; + let thumbnail = Self::by_id(db, active_model.id.unwrap()) + .await? + .expect("Inserted thumbnail does not exist"); + + Ok(thumbnail) + } + /// Returns all thumbnails for a given file pub async fn for_file_id(db: DatabaseConnection, file_id: i64) -> RepoResult> { let thumb_models: Vec<(thumbnail::Model, Option)> = thumbnail::Entity::find()