use OwnedMxcUri in media service

Not using `MxcData` because it borrows it's fields, and so we wouldn't
be able to return an owned `MxcData` from functions that read the db.
This commit is contained in:
Benjamin Lee 2024-09-14 20:43:56 -07:00
parent e2cba15ed2
commit 7672cc8473
No known key found for this signature in database
GPG key ID: FB9624E2885D55A4
5 changed files with 25 additions and 23 deletions

View file

@ -159,7 +159,7 @@ pub(crate) async fn create_content_route(
services() services()
.media .media
.create( .create(
mxc.to_string(), mxc.clone().into(),
body.filename body.filename
.clone() .clone()
.map(|filename| ContentDisposition { .map(|filename| ContentDisposition {
@ -350,7 +350,7 @@ pub(crate) async fn get_remote_content(
services() services()
.media .media
.create( .create(
mxc.to_string(), mxc.clone().into(),
response.content.content_disposition.as_ref(), response.content.content_disposition.as_ref(),
response.content.content_type.clone(), response.content.content_type.clone(),
&response.content.file, &response.content.file,
@ -455,7 +455,7 @@ async fn get_content_route_ruma(
.. ..
}, },
file, file,
)) = services().media.get(mxc.to_string()).await? )) = services().media.get(mxc.clone().into()).await?
{ {
Ok(authenticated_media_client::get_content::v1::Response { Ok(authenticated_media_client::get_content::v1::Response {
file, file,
@ -581,7 +581,7 @@ async fn get_content_as_filename_route_ruma(
.. ..
}, },
file, file,
)) = services().media.get(mxc.to_string()).await? )) = services().media.get(mxc.clone().into()).await?
{ {
Ok(authenticated_media_client::get_content_as_filename::v1::Response { Ok(authenticated_media_client::get_content_as_filename::v1::Response {
file, file,
@ -842,8 +842,10 @@ async fn get_content_thumbnail_route_ruma(
.. ..
}, },
file, file,
)) = )) = services()
services().media.get_thumbnail(mxc.to_string(), width, height).await? .media
.get_thumbnail(mxc.clone().into(), width, height)
.await?
{ {
return Ok(make_response(file, content_type)); return Ok(make_response(file, content_type));
} }
@ -872,7 +874,7 @@ async fn get_content_thumbnail_route_ruma(
services() services()
.media .media
.upload_thumbnail( .upload_thumbnail(
mxc.to_string(), mxc.clone().into(),
None, None,
resp.content.content_type.clone(), resp.content.content_type.clone(),
width, width,
@ -901,10 +903,8 @@ async fn get_content_thumbnail_route_ruma(
.. ..
}, },
file, file,
)) = services() )) =
.media services().media.get_thumbnail(mxc.into(), width, height).await?
.get_thumbnail(mxc.to_string(), width, height)
.await?
{ {
return Ok(make_response(file, content_type)); return Ok(make_response(file, content_type));
} }

View file

@ -2062,7 +2062,7 @@ pub(crate) async fn media_download_route(
content_type, content_type,
}, },
file, file,
)) = services().media.get(mxc.to_string()).await? )) = services().media.get(mxc.into()).await?
else { else {
return Err(Error::BadRequest( return Err(Error::BadRequest(
ErrorKind::NotYetUploaded, ErrorKind::NotYetUploaded,
@ -2109,7 +2109,7 @@ pub(crate) async fn media_thumbnail_route(
.. ..
}, },
file, file,
)) = services().media.get_thumbnail(mxc.to_string(), width, height).await? )) = services().media.get_thumbnail(mxc.into(), width, height).await?
else { else {
return Err(Error::BadRequest( return Err(Error::BadRequest(
ErrorKind::NotYetUploaded, ErrorKind::NotYetUploaded,

View file

@ -1,4 +1,4 @@
use ruma::api::client::error::ErrorKind; use ruma::{api::client::error::ErrorKind, OwnedMxcUri};
use crate::{ use crate::{
database::KeyValueDatabase, database::KeyValueDatabase,
@ -88,7 +88,7 @@ impl TryFrom<&MediaFileKey> for MediaFileKeyParts {
impl service::media::Data for KeyValueDatabase { impl service::media::Data for KeyValueDatabase {
fn create_file_metadata( fn create_file_metadata(
&self, &self,
mxc: String, mxc: OwnedMxcUri,
width: u32, width: u32,
height: u32, height: u32,
meta: &FileMeta, meta: &FileMeta,
@ -121,7 +121,7 @@ impl service::media::Data for KeyValueDatabase {
fn search_file_metadata( fn search_file_metadata(
&self, &self,
mxc: String, mxc: OwnedMxcUri,
width: u32, width: u32,
height: u32, height: u32,
) -> Result<(FileMeta, MediaFileKey)> { ) -> Result<(FileMeta, MediaFileKey)> {

View file

@ -1,7 +1,7 @@
use std::io::Cursor; use std::io::Cursor;
use image::imageops::FilterType; use image::imageops::FilterType;
use ruma::http_headers::ContentDisposition; use ruma::{http_headers::ContentDisposition, OwnedMxcUri};
use tokio::{ use tokio::{
fs::File, fs::File,
io::{AsyncReadExt, AsyncWriteExt}, io::{AsyncReadExt, AsyncWriteExt},
@ -46,7 +46,7 @@ impl Service {
#[tracing::instrument(skip(self, file))] #[tracing::instrument(skip(self, file))]
pub(crate) async fn create( pub(crate) async fn create(
&self, &self,
mxc: String, mxc: OwnedMxcUri,
content_disposition: Option<&ContentDisposition>, content_disposition: Option<&ContentDisposition>,
content_type: Option<String>, content_type: Option<String>,
file: &[u8], file: &[u8],
@ -69,7 +69,7 @@ impl Service {
#[tracing::instrument(skip(self, file))] #[tracing::instrument(skip(self, file))]
pub(crate) async fn upload_thumbnail( pub(crate) async fn upload_thumbnail(
&self, &self,
mxc: String, mxc: OwnedMxcUri,
content_disposition: Option<String>, content_disposition: Option<String>,
content_type: Option<String>, content_type: Option<String>,
width: u32, width: u32,
@ -93,7 +93,7 @@ impl Service {
#[tracing::instrument(skip(self))] #[tracing::instrument(skip(self))]
pub(crate) async fn get( pub(crate) async fn get(
&self, &self,
mxc: String, mxc: OwnedMxcUri,
) -> Result<Option<(FileMeta, Vec<u8>)>> { ) -> Result<Option<(FileMeta, Vec<u8>)>> {
if let Ok((meta, key)) = self.db.search_file_metadata(mxc, 0, 0) { if let Ok((meta, key)) = self.db.search_file_metadata(mxc, 0, 0) {
let path = services().globals.get_media_file(&key); let path = services().globals.get_media_file(&key);
@ -224,7 +224,7 @@ impl Service {
#[tracing::instrument(skip(self))] #[tracing::instrument(skip(self))]
pub(crate) async fn get_thumbnail( pub(crate) async fn get_thumbnail(
&self, &self,
mxc: String, mxc: OwnedMxcUri,
width: u32, width: u32,
height: u32, height: u32,
) -> Result<Option<(FileMeta, Vec<u8>)>> { ) -> Result<Option<(FileMeta, Vec<u8>)>> {

View file

@ -1,10 +1,12 @@
use ruma::OwnedMxcUri;
use super::{FileMeta, MediaFileKey}; use super::{FileMeta, MediaFileKey};
use crate::Result; use crate::Result;
pub(crate) trait Data: Send + Sync { pub(crate) trait Data: Send + Sync {
fn create_file_metadata( fn create_file_metadata(
&self, &self,
mxc: String, mxc: OwnedMxcUri,
width: u32, width: u32,
height: u32, height: u32,
meta: &FileMeta, meta: &FileMeta,
@ -12,7 +14,7 @@ pub(crate) trait Data: Send + Sync {
fn search_file_metadata( fn search_file_metadata(
&self, &self,
mxc: String, mxc: OwnedMxcUri,
width: u32, width: u32,
height: u32, height: u32,
) -> Result<(FileMeta, MediaFileKey)>; ) -> Result<(FileMeta, MediaFileKey)>;