2022-11-18 04:39:26 +00:00
|
|
|
use crate::{db::Db, error::Error};
|
2022-01-17 22:54:45 +00:00
|
|
|
use activitystreams::iri_string::types::IriString;
|
2020-03-26 03:26:45 +00:00
|
|
|
use uuid::Uuid;
|
|
|
|
|
2021-09-18 17:55:39 +00:00
|
|
|
#[derive(Clone, Debug)]
|
2021-02-10 04:05:06 +00:00
|
|
|
pub struct MediaCache {
|
2020-03-26 18:21:05 +00:00
|
|
|
db: Db,
|
2020-03-26 03:26:45 +00:00
|
|
|
}
|
|
|
|
|
2021-02-10 04:05:06 +00:00
|
|
|
impl MediaCache {
|
2021-02-10 04:17:20 +00:00
|
|
|
pub(crate) fn new(db: Db) -> Self {
|
2021-02-10 04:05:06 +00:00
|
|
|
MediaCache { db }
|
2020-03-26 03:26:45 +00:00
|
|
|
}
|
|
|
|
|
2022-11-16 01:56:13 +00:00
|
|
|
#[tracing::instrument(level = "debug", name = "Get media uuid", skip_all, fields(url = url.to_string().as_str()))]
|
2022-01-17 22:54:45 +00:00
|
|
|
pub(crate) async fn get_uuid(&self, url: IriString) -> Result<Option<Uuid>, Error> {
|
2021-02-10 04:05:06 +00:00
|
|
|
self.db.media_id(url).await
|
2020-03-26 03:26:45 +00:00
|
|
|
}
|
|
|
|
|
2022-11-16 01:56:13 +00:00
|
|
|
#[tracing::instrument(level = "debug", name = "Get media url", skip(self))]
|
2022-01-17 22:54:45 +00:00
|
|
|
pub(crate) async fn get_url(&self, uuid: Uuid) -> Result<Option<IriString>, Error> {
|
2021-02-10 04:05:06 +00:00
|
|
|
self.db.media_url(uuid).await
|
|
|
|
}
|
|
|
|
|
2022-11-01 20:57:33 +00:00
|
|
|
#[tracing::instrument(name = "Store media url", skip_all, fields(url = url.to_string().as_str()))]
|
2022-01-17 22:54:45 +00:00
|
|
|
pub(crate) async fn store_url(&self, url: IriString) -> Result<Uuid, Error> {
|
2020-03-26 03:26:45 +00:00
|
|
|
let uuid = Uuid::new_v4();
|
2020-03-26 18:21:05 +00:00
|
|
|
|
2021-02-10 04:05:06 +00:00
|
|
|
self.db.save_url(url, uuid).await?;
|
2020-03-26 18:21:05 +00:00
|
|
|
|
|
|
|
Ok(uuid)
|
2020-03-26 03:26:45 +00:00
|
|
|
}
|
|
|
|
}
|