You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
79 lines
2.3 KiB
Rust
79 lines
2.3 KiB
Rust
3 years ago
|
use crate::file::File;
|
||
|
use mediarepo_core::error::RepoResult;
|
||
3 years ago
|
use mediarepo_database::entities::content_descriptor;
|
||
3 years ago
|
use mediarepo_database::entities::file;
|
||
|
use sea_orm::prelude::*;
|
||
|
use sea_orm::{DatabaseConnection, Set};
|
||
3 years ago
|
use std::fmt::Debug;
|
||
3 years ago
|
|
||
|
pub struct Hash {
|
||
|
db: DatabaseConnection,
|
||
3 years ago
|
model: content_descriptor::Model,
|
||
3 years ago
|
}
|
||
|
|
||
|
impl Hash {
|
||
3 years ago
|
#[tracing::instrument(level = "trace")]
|
||
3 years ago
|
pub(crate) fn new(db: DatabaseConnection, model: content_descriptor::Model) -> Self {
|
||
3 years ago
|
Self { db, model }
|
||
|
}
|
||
|
|
||
|
/// Searches for the hash by id
|
||
3 years ago
|
#[tracing::instrument(level = "debug", skip(db))]
|
||
3 years ago
|
pub async fn by_id(db: DatabaseConnection, id: i64) -> RepoResult<Option<Self>> {
|
||
3 years ago
|
let hash = content_descriptor::Entity::find_by_id(id)
|
||
3 years ago
|
.one(&db)
|
||
|
.await?
|
||
|
.map(|model| Self::new(db, model));
|
||
|
|
||
|
Ok(hash)
|
||
|
}
|
||
|
|
||
3 years ago
|
/// Returns the hash by value
|
||
3 years ago
|
#[tracing::instrument(level = "debug", skip(db))]
|
||
3 years ago
|
pub async fn by_value<D: AsRef<[u8]> + Debug>(
|
||
3 years ago
|
db: DatabaseConnection,
|
||
3 years ago
|
descriptor: D,
|
||
3 years ago
|
) -> RepoResult<Option<Self>> {
|
||
3 years ago
|
let cid = content_descriptor::Entity::find()
|
||
|
.filter(content_descriptor::Column::Descriptor.eq(descriptor.as_ref()))
|
||
3 years ago
|
.one(&db)
|
||
|
.await?
|
||
|
.map(|model| Self::new(db, model));
|
||
|
|
||
3 years ago
|
Ok(cid)
|
||
3 years ago
|
}
|
||
|
|
||
3 years ago
|
/// Adds a new hash to the database
|
||
3 years ago
|
#[tracing::instrument(level = "debug", skip(db))]
|
||
3 years ago
|
pub async fn add(db: DatabaseConnection, descriptor: Vec<u8>) -> RepoResult<Self> {
|
||
|
let active_model = content_descriptor::ActiveModel {
|
||
|
descriptor: Set(descriptor),
|
||
3 years ago
|
..Default::default()
|
||
|
};
|
||
3 years ago
|
let model = active_model.insert(&db).await?;
|
||
3 years ago
|
|
||
3 years ago
|
Ok(Self::new(db, model))
|
||
3 years ago
|
}
|
||
|
|
||
|
pub fn id(&self) -> i64 {
|
||
|
self.model.id
|
||
|
}
|
||
|
|
||
3 years ago
|
pub fn descriptor(&self) -> &[u8] {
|
||
|
&self.model.descriptor[..]
|
||
3 years ago
|
}
|
||
|
|
||
|
/// Returns the file associated with the hash
|
||
3 years ago
|
#[tracing::instrument(level = "debug", skip(self))]
|
||
3 years ago
|
pub async fn file(&self) -> RepoResult<Option<File>> {
|
||
|
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)
|
||
|
}
|
||
|
}
|