media: add MediaFileKey wrapper

One more win in the fight against the Vec<u8>s
This commit is contained in:
Lambda 2024-08-11 17:11:48 +00:00
parent 84850a163d
commit 1ccb1e572b
5 changed files with 38 additions and 13 deletions

View file

@ -35,6 +35,7 @@ use trust_dns_resolver::TokioAsyncResolver;
use crate::{
api::server_server::FedDest,
observability::FilterReloadHandles,
service::media::MediaFileKey,
services,
utils::on_demand_hashmap::{OnDemandHashMap, TokenSet},
Config, Error, Result,
@ -506,11 +507,11 @@ impl Service {
r
}
pub(crate) fn get_media_file(&self, key: &[u8]) -> PathBuf {
pub(crate) fn get_media_file(&self, key: &MediaFileKey) -> PathBuf {
let mut r = PathBuf::new();
r.push(self.config.database.path.clone());
r.push("media");
r.push(general_purpose::URL_SAFE_NO_PAD.encode(key));
r.push(general_purpose::URL_SAFE_NO_PAD.encode(key.as_bytes()));
r
}

View file

@ -24,6 +24,19 @@ pub(crate) struct FileMeta {
pub(crate) content_type: Option<String>,
}
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub(crate) struct MediaFileKey(Vec<u8>);
impl MediaFileKey {
pub(crate) fn new(key: Vec<u8>) -> Self {
Self(key)
}
pub(crate) fn as_bytes(&self) -> &[u8] {
&self.0
}
}
pub(crate) struct Service {
pub(crate) db: &'static dyn Data,
}

View file

@ -1,3 +1,4 @@
use super::MediaFileKey;
use crate::Result;
pub(crate) trait Data: Send + Sync {
@ -8,7 +9,7 @@ pub(crate) trait Data: Send + Sync {
height: u32,
content_disposition: Option<&str>,
content_type: Option<&str>,
) -> Result<Vec<u8>>;
) -> Result<MediaFileKey>;
/// Returns `content_disposition`, `content_type` and the `metadata` key.
fn search_file_metadata(
@ -16,5 +17,5 @@ pub(crate) trait Data: Send + Sync {
mxc: String,
width: u32,
height: u32,
) -> Result<(Option<String>, Option<String>, Vec<u8>)>;
) -> Result<(Option<String>, Option<String>, MediaFileKey)>;
}