From 7b0e830f4c65558ca7ed0b45dcb4a39637761781 Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Mon, 26 Aug 2024 22:15:42 +0000 Subject: [PATCH] add interface to get file metadata w/ admin command Signed-off-by: Jason Volk --- src/admin/media/commands.rs | 8 ++++++++ src/admin/media/mod.rs | 7 ++++++- src/service/media/mod.rs | 14 +++++++++++++- 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/src/admin/media/commands.rs b/src/admin/media/commands.rs index 476f048b..3894d5df 100644 --- a/src/admin/media/commands.rs +++ b/src/admin/media/commands.rs @@ -257,3 +257,11 @@ pub(super) async fn delete_all_from_server( "Deleted {deleted_count} total files.", ))) } + +#[admin_command] +pub(super) async fn get_file_info(&self, mxc: OwnedMxcUri) -> Result { + let mxc: Mxc<'_> = mxc.as_str().try_into()?; + let metadata = self.services.media.get_metadata(&mxc); + + Ok(RoomMessageEventContent::notice_markdown(format!("```\n{metadata:#?}\n```"))) +} diff --git a/src/admin/media/mod.rs b/src/admin/media/mod.rs index 1120a63d..27d75f66 100644 --- a/src/admin/media/mod.rs +++ b/src/admin/media/mod.rs @@ -2,7 +2,7 @@ mod commands; use clap::Subcommand; use conduit::Result; -use ruma::{EventId, MxcUri, ServerName}; +use ruma::{EventId, MxcUri, OwnedMxcUri, ServerName}; use crate::admin_command_dispatch; @@ -55,4 +55,9 @@ pub(super) enum MediaCommand { #[arg(short, long)] force: bool, }, + + GetFileInfo { + /// The MXC URL to lookup info for. + mxc: OwnedMxcUri, + }, } diff --git a/src/service/media/mod.rs b/src/service/media/mod.rs index fc86b215..5b12a94c 100644 --- a/src/service/media/mod.rs +++ b/src/service/media/mod.rs @@ -31,7 +31,7 @@ pub struct FileMeta { pub struct Service { url_preview_mutex: MutexMap, - pub(crate) db: Data, + pub(super) db: Data, services: Services, } @@ -347,6 +347,18 @@ impl Service { Ok(file) } + #[inline] + pub fn get_metadata(&self, mxc: &Mxc<'_>) -> Option { + self.db + .search_file_metadata(mxc, 0, 0) + .map(|metadata| FileMeta { + content_disposition: metadata.content_disposition, + content_type: metadata.content_type, + content: None, + }) + .ok() + } + #[inline] #[must_use] pub fn get_media_file(&self, key: &[u8]) -> PathBuf { self.get_media_file_sha256(key) }