mirror of
https://gitlab.computer.surgery/matrix/grapevine.git
synced 2025-12-18 08:11:24 +01:00
Serialize creation of media/thumbnails
This way, no two tasks will put effort into creating the same exact media at the same time.
This commit is contained in:
parent
d7028b13b2
commit
87348ccffb
4 changed files with 452 additions and 203 deletions
|
|
@ -1,6 +1,7 @@
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
use axum::response::IntoResponse;
|
use axum::response::IntoResponse;
|
||||||
|
use either::Either;
|
||||||
use http::{
|
use http::{
|
||||||
header::{CONTENT_DISPOSITION, CONTENT_SECURITY_POLICY, CONTENT_TYPE},
|
header::{CONTENT_DISPOSITION, CONTENT_SECURITY_POLICY, CONTENT_TYPE},
|
||||||
HeaderName, HeaderValue, Method,
|
HeaderName, HeaderValue, Method,
|
||||||
|
|
@ -20,7 +21,7 @@ use ruma::{
|
||||||
use tracing::{debug, error, info, warn};
|
use tracing::{debug, error, info, warn};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
service::media::{FileMeta, MxcData},
|
service::media::{FileMeta, MediaCreationToken, MxcData, ThumbnailResult},
|
||||||
services, utils, Ar, Error, Ra, Result,
|
services, utils, Ar, Error, Ra, Result,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -154,10 +155,15 @@ pub(crate) async fn create_content_route(
|
||||||
let media_id = utils::random_string(MXC_LENGTH);
|
let media_id = utils::random_string(MXC_LENGTH);
|
||||||
let mxc = MxcData::new(services().globals.server_name(), &media_id)?;
|
let mxc = MxcData::new(services().globals.server_name(), &media_id)?;
|
||||||
|
|
||||||
|
// this is a fresh MXC, there ought to not be anyone else creating it at the
|
||||||
|
// same time
|
||||||
|
let Either::Right(token) = services().media.get(mxc).await? else {
|
||||||
|
return Err(Error::bad_database("Media for fresh mxc already exists"));
|
||||||
|
};
|
||||||
services()
|
services()
|
||||||
.media
|
.media
|
||||||
.create(
|
.create(
|
||||||
mxc,
|
token,
|
||||||
body.filename
|
body.filename
|
||||||
.clone()
|
.clone()
|
||||||
.map(|filename| ContentDisposition {
|
.map(|filename| ContentDisposition {
|
||||||
|
|
@ -319,9 +325,9 @@ async fn get_remote_content_via_legacy_api(
|
||||||
|
|
||||||
#[tracing::instrument]
|
#[tracing::instrument]
|
||||||
pub(crate) async fn get_remote_content(
|
pub(crate) async fn get_remote_content(
|
||||||
mxc: MxcData<'_>,
|
token: MediaCreationToken,
|
||||||
) -> Result<RemoteResponse, Error> {
|
) -> Result<RemoteResponse, Error> {
|
||||||
let fed_result = get_remote_content_via_federation_api(mxc).await;
|
let fed_result = get_remote_content_via_federation_api(token.mxc()).await;
|
||||||
|
|
||||||
let response = match fed_result {
|
let response = match fed_result {
|
||||||
Ok(response) => {
|
Ok(response) => {
|
||||||
|
|
@ -338,7 +344,7 @@ pub(crate) async fn get_remote_content(
|
||||||
back to deprecated API"
|
back to deprecated API"
|
||||||
);
|
);
|
||||||
|
|
||||||
get_remote_content_via_legacy_api(mxc).await?
|
get_remote_content_via_legacy_api(token.mxc()).await?
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
return Err(e);
|
return Err(e);
|
||||||
|
|
@ -348,7 +354,7 @@ pub(crate) async fn get_remote_content(
|
||||||
services()
|
services()
|
||||||
.media
|
.media
|
||||||
.create(
|
.create(
|
||||||
mxc,
|
token,
|
||||||
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,
|
||||||
|
|
@ -447,26 +453,37 @@ async fn get_content_route_ruma(
|
||||||
) -> Result<authenticated_media_client::get_content::v1::Response> {
|
) -> Result<authenticated_media_client::get_content::v1::Response> {
|
||||||
let mxc = MxcData::new(&body.server_name, &body.media_id)?;
|
let mxc = MxcData::new(&body.server_name, &body.media_id)?;
|
||||||
|
|
||||||
if let Some((
|
let token = match services().media.get(mxc).await? {
|
||||||
|
Either::Left((
|
||||||
FileMeta {
|
FileMeta {
|
||||||
content_type,
|
content_type,
|
||||||
..
|
..
|
||||||
},
|
},
|
||||||
file,
|
file,
|
||||||
)) = services().media.get(mxc).await?
|
)) => {
|
||||||
{
|
return Ok(authenticated_media_client::get_content::v1::Response {
|
||||||
Ok(authenticated_media_client::get_content::v1::Response {
|
|
||||||
file,
|
file,
|
||||||
content_disposition: Some(content_disposition_for(
|
content_disposition: Some(content_disposition_for(
|
||||||
content_type.as_deref(),
|
content_type.as_deref(),
|
||||||
None,
|
None,
|
||||||
)),
|
)),
|
||||||
content_type,
|
content_type,
|
||||||
})
|
});
|
||||||
} else if &*body.server_name != services().globals.server_name()
|
}
|
||||||
&& allow_remote == AllowRemote::Yes
|
|
||||||
|
Either::Right(token) => token,
|
||||||
|
};
|
||||||
|
|
||||||
|
if &*body.server_name == services().globals.server_name()
|
||||||
|
|| allow_remote != AllowRemote::Yes
|
||||||
{
|
{
|
||||||
let remote_response = get_remote_content(mxc).await?;
|
return Err(Error::BadRequest(
|
||||||
|
ErrorKind::NotYetUploaded,
|
||||||
|
"Media not found.",
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
let remote_response = get_remote_content(token).await?;
|
||||||
Ok(authenticated_media_client::get_content::v1::Response {
|
Ok(authenticated_media_client::get_content::v1::Response {
|
||||||
file: remote_response.content.file,
|
file: remote_response.content.file,
|
||||||
content_disposition: Some(content_disposition_for(
|
content_disposition: Some(content_disposition_for(
|
||||||
|
|
@ -475,9 +492,6 @@ async fn get_content_route_ruma(
|
||||||
)),
|
)),
|
||||||
content_type: remote_response.content.content_type,
|
content_type: remote_response.content.content_type,
|
||||||
})
|
})
|
||||||
} else {
|
|
||||||
Err(Error::BadRequest(ErrorKind::NotYetUploaded, "Media not found."))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// # `GET /_matrix/media/r0/download/{serverName}/{mediaId}/{fileName}`
|
/// # `GET /_matrix/media/r0/download/{serverName}/{mediaId}/{fileName}`
|
||||||
|
|
@ -571,30 +585,36 @@ async fn get_content_as_filename_route_ruma(
|
||||||
body: Ar<authenticated_media_client::get_content_as_filename::v1::Request>,
|
body: Ar<authenticated_media_client::get_content_as_filename::v1::Request>,
|
||||||
allow_remote: AllowRemote,
|
allow_remote: AllowRemote,
|
||||||
) -> Result<authenticated_media_client::get_content_as_filename::v1::Response> {
|
) -> Result<authenticated_media_client::get_content_as_filename::v1::Response> {
|
||||||
|
use authenticated_media_client::get_content_as_filename::v1::Response;
|
||||||
|
|
||||||
let mxc = MxcData::new(&body.server_name, &body.media_id)?;
|
let mxc = MxcData::new(&body.server_name, &body.media_id)?;
|
||||||
|
|
||||||
if let Some((
|
let token = match services().media.get(mxc).await? {
|
||||||
|
Either::Left((
|
||||||
FileMeta {
|
FileMeta {
|
||||||
content_type,
|
content_type,
|
||||||
..
|
..
|
||||||
},
|
},
|
||||||
file,
|
file,
|
||||||
)) = services().media.get(mxc).await?
|
)) => {
|
||||||
{
|
return Ok(Response {
|
||||||
Ok(authenticated_media_client::get_content_as_filename::v1::Response {
|
|
||||||
file,
|
file,
|
||||||
content_disposition: Some(content_disposition_for(
|
content_disposition: Some(content_disposition_for(
|
||||||
content_type.as_deref(),
|
content_type.as_deref(),
|
||||||
Some(body.filename.clone()),
|
Some(body.filename.clone()),
|
||||||
)),
|
)),
|
||||||
content_type,
|
content_type,
|
||||||
})
|
});
|
||||||
} else if &*body.server_name != services().globals.server_name()
|
}
|
||||||
|
Either::Right(token) => token,
|
||||||
|
};
|
||||||
|
|
||||||
|
if &*body.server_name != services().globals.server_name()
|
||||||
&& allow_remote == AllowRemote::Yes
|
&& allow_remote == AllowRemote::Yes
|
||||||
{
|
{
|
||||||
let remote_response = get_remote_content(mxc).await?;
|
let remote_response = get_remote_content(token).await?;
|
||||||
|
|
||||||
Ok(authenticated_media_client::get_content_as_filename::v1::Response {
|
Ok(Response {
|
||||||
content_disposition: Some(content_disposition_for(
|
content_disposition: Some(content_disposition_for(
|
||||||
remote_response.content.content_type.as_deref(),
|
remote_response.content.content_type.as_deref(),
|
||||||
Some(body.filename.clone()),
|
Some(body.filename.clone()),
|
||||||
|
|
@ -834,30 +854,50 @@ async fn get_content_thumbnail_route_ruma(
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
if let Some((
|
let lookup_result =
|
||||||
|
services().media.get_thumbnail(mxc, width, height).await?;
|
||||||
|
|
||||||
|
if let ThumbnailResult::Data(
|
||||||
FileMeta {
|
FileMeta {
|
||||||
content_type,
|
content_type,
|
||||||
..
|
..
|
||||||
},
|
},
|
||||||
file,
|
file,
|
||||||
)) = services().media.get_thumbnail(mxc, width, height).await?
|
) = lookup_result
|
||||||
{
|
{
|
||||||
return Ok(make_response(file, content_type));
|
return Ok(make_response(file, content_type));
|
||||||
}
|
}
|
||||||
|
|
||||||
if &*body.server_name != services().globals.server_name()
|
if &*body.server_name == services().globals.server_name()
|
||||||
&& allow_remote == AllowRemote::Yes
|
|| allow_remote != AllowRemote::Yes
|
||||||
{
|
{
|
||||||
|
return Err(Error::BadRequest(
|
||||||
|
ErrorKind::NotYetUploaded,
|
||||||
|
"Media not found.",
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
let source_token = match lookup_result {
|
||||||
|
ThumbnailResult::Data(..) => unreachable!(),
|
||||||
|
ThumbnailResult::NeedSource(token) => {
|
||||||
|
// we need to fetch the whole media
|
||||||
|
token
|
||||||
|
}
|
||||||
|
ThumbnailResult::NeedThumbnail(thumbnail_token, source_token) => {
|
||||||
|
// try to fetch thumbnail from remote, falling back to fetching the
|
||||||
|
// whole media anyway
|
||||||
|
|
||||||
let get_thumbnail_response = get_remote_thumbnail(
|
let get_thumbnail_response = get_remote_thumbnail(
|
||||||
&body.server_name,
|
&body.server_name,
|
||||||
authenticated_media_fed::get_content_thumbnail::v1::Request {
|
authenticated_media_fed::get_content_thumbnail::v1::Request {
|
||||||
height: body.height,
|
height: thumbnail_token.height().into(),
|
||||||
width: body.width,
|
width: thumbnail_token.width().into(),
|
||||||
method: body.method.clone(),
|
method: body.method.clone(),
|
||||||
media_id: body.media_id.clone(),
|
media_id: body.media_id.clone(),
|
||||||
timeout_ms: Duration::from_secs(20),
|
timeout_ms: Duration::from_secs(20),
|
||||||
// we don't support animated thumbnails, so don't try requesting
|
// we don't support animated thumbnails, so don't try
|
||||||
// one - we're allowed to ignore the client's request for an
|
// requesting one - we're allowed to
|
||||||
|
// ignore the client's request for an
|
||||||
// animated thumbnail
|
// animated thumbnail
|
||||||
animated: Some(false),
|
animated: Some(false),
|
||||||
},
|
},
|
||||||
|
|
@ -868,12 +908,10 @@ async fn get_content_thumbnail_route_ruma(
|
||||||
Ok(resp) => {
|
Ok(resp) => {
|
||||||
services()
|
services()
|
||||||
.media
|
.media
|
||||||
.upload_thumbnail(
|
.create_thumbnail(
|
||||||
mxc,
|
thumbnail_token,
|
||||||
None,
|
None,
|
||||||
resp.content.content_type.clone(),
|
resp.content.content_type.clone(),
|
||||||
width,
|
|
||||||
height,
|
|
||||||
&resp.content.file,
|
&resp.content.file,
|
||||||
)
|
)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
@ -883,28 +921,32 @@ async fn get_content_thumbnail_route_ruma(
|
||||||
resp.content.content_type,
|
resp.content.content_type,
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
Err(error) => warn!(
|
Err(error) => {
|
||||||
|
warn!(
|
||||||
%error,
|
%error,
|
||||||
"Failed to fetch thumbnail via federation, trying to fetch \
|
"Failed to fetch thumbnail via federation, trying to fetch \
|
||||||
original media and create thumbnail ourselves"
|
original media and create thumbnail ourselves"
|
||||||
),
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
get_remote_content(mxc).await?;
|
source_token
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
if let Some((
|
get_remote_content(source_token).await?;
|
||||||
|
|
||||||
|
if let ThumbnailResult::Data(
|
||||||
FileMeta {
|
FileMeta {
|
||||||
content_type,
|
content_type,
|
||||||
..
|
..
|
||||||
},
|
},
|
||||||
file,
|
file,
|
||||||
)) = services().media.get_thumbnail(mxc, width, height).await?
|
) = services().media.get_thumbnail(mxc, width, height).await?
|
||||||
{
|
{
|
||||||
return Ok(make_response(file, content_type));
|
return Ok(make_response(file, content_type));
|
||||||
}
|
}
|
||||||
|
|
||||||
error!("Source media doesn't exist even after fetching it from remote");
|
error!("Source media doesn't exist even after fetching it from remote");
|
||||||
}
|
|
||||||
|
|
||||||
Err(Error::BadRequest(ErrorKind::NotYetUploaded, "Media not found."))
|
Err(Error::BadRequest(ErrorKind::NotYetUploaded, "Media not found."))
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -10,6 +10,7 @@ use std::{
|
||||||
use axum::{response::IntoResponse, Json};
|
use axum::{response::IntoResponse, Json};
|
||||||
use axum_extra::headers::{Authorization, HeaderMapExt};
|
use axum_extra::headers::{Authorization, HeaderMapExt};
|
||||||
use base64::Engine as _;
|
use base64::Engine as _;
|
||||||
|
use either::Either;
|
||||||
use get_profile_information::v1::ProfileField;
|
use get_profile_information::v1::ProfileField;
|
||||||
use ruma::{
|
use ruma::{
|
||||||
api::{
|
api::{
|
||||||
|
|
@ -71,7 +72,7 @@ use crate::{
|
||||||
api::client_server::{self, claim_keys_helper, get_keys_helper},
|
api::client_server::{self, claim_keys_helper, get_keys_helper},
|
||||||
observability::{FoundIn, Lookup, METRICS},
|
observability::{FoundIn, Lookup, METRICS},
|
||||||
service::{
|
service::{
|
||||||
media::MxcData,
|
media::{MxcData, ThumbnailResult},
|
||||||
pdu::{gen_event_id_canonical_json, PduBuilder},
|
pdu::{gen_event_id_canonical_json, PduBuilder},
|
||||||
},
|
},
|
||||||
services,
|
services,
|
||||||
|
|
@ -2047,7 +2048,7 @@ pub(crate) async fn media_download_route(
|
||||||
body: Ar<authenticated_media::get_content::v1::Request>,
|
body: Ar<authenticated_media::get_content::v1::Request>,
|
||||||
) -> Result<Ra<authenticated_media::get_content::v1::Response>> {
|
) -> Result<Ra<authenticated_media::get_content::v1::Response>> {
|
||||||
let mxc = MxcData::new(services().globals.server_name(), &body.media_id)?;
|
let mxc = MxcData::new(services().globals.server_name(), &body.media_id)?;
|
||||||
let Some((
|
let Either::Left((
|
||||||
crate::service::media::FileMeta {
|
crate::service::media::FileMeta {
|
||||||
content_disposition,
|
content_disposition,
|
||||||
content_type,
|
content_type,
|
||||||
|
|
@ -2094,13 +2095,13 @@ pub(crate) async fn media_thumbnail_route(
|
||||||
Error::BadRequest(ErrorKind::InvalidParam, "Height is invalid.")
|
Error::BadRequest(ErrorKind::InvalidParam, "Height is invalid.")
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
let Some((
|
let ThumbnailResult::Data(
|
||||||
crate::service::media::FileMeta {
|
crate::service::media::FileMeta {
|
||||||
content_type,
|
content_type,
|
||||||
..
|
..
|
||||||
},
|
},
|
||||||
file,
|
file,
|
||||||
)) = services().media.get_thumbnail(mxc, width, height).await?
|
) = services().media.get_thumbnail(mxc, width, height).await?
|
||||||
else {
|
else {
|
||||||
return Err(Error::BadRequest(
|
return Err(Error::BadRequest(
|
||||||
ErrorKind::NotYetUploaded,
|
ErrorKind::NotYetUploaded,
|
||||||
|
|
|
||||||
|
|
@ -145,9 +145,7 @@ impl Services {
|
||||||
account_data: db,
|
account_data: db,
|
||||||
admin: admin::Service::build(),
|
admin: admin::Service::build(),
|
||||||
key_backups: db,
|
key_backups: db,
|
||||||
media: media::Service {
|
media: media::Service::new(db),
|
||||||
db,
|
|
||||||
},
|
|
||||||
sending: sending::Service::build(db, &config),
|
sending: sending::Service::build(db, &config),
|
||||||
|
|
||||||
globals: globals::Service::load(db, config, reload_handles)?,
|
globals: globals::Service::load(db, config, reload_handles)?,
|
||||||
|
|
|
||||||
|
|
@ -1,17 +1,22 @@
|
||||||
use std::{fmt, io::Cursor};
|
use std::{fmt, io::Cursor};
|
||||||
|
|
||||||
|
use either::Either;
|
||||||
use image::imageops::FilterType;
|
use image::imageops::FilterType;
|
||||||
use ruma::{
|
use ruma::{
|
||||||
api::client::error::ErrorKind, http_headers::ContentDisposition, MxcUri,
|
api::client::error::ErrorKind, http_headers::ContentDisposition, MxcUri,
|
||||||
MxcUriError, OwnedMxcUri,
|
MxcUriError, OwnedMxcUri, OwnedServerName,
|
||||||
};
|
};
|
||||||
use tokio::{
|
use tokio::{
|
||||||
fs::File,
|
fs::File,
|
||||||
io::{AsyncReadExt, AsyncWriteExt},
|
io::{AsyncReadExt, AsyncWriteExt},
|
||||||
};
|
};
|
||||||
use tracing::{debug, warn};
|
use tracing::{debug, instrument, warn};
|
||||||
|
|
||||||
use crate::{services, Error, Result};
|
use crate::{
|
||||||
|
services,
|
||||||
|
utils::on_demand_hashmap::{KeyToken, TokenSet},
|
||||||
|
Error, Result,
|
||||||
|
};
|
||||||
|
|
||||||
mod data;
|
mod data;
|
||||||
|
|
||||||
|
|
@ -91,16 +96,158 @@ impl<'a> TryFrom<&'a MxcUri> for MxcData<'a> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
|
||||||
|
struct ThumbnailDimensions {
|
||||||
|
width: u32,
|
||||||
|
height: u32,
|
||||||
|
should_crop: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ThumbnailDimensions {
|
||||||
|
/// Returns None when the server should send the original file.
|
||||||
|
fn new(width: u32, height: u32) -> Option<Self> {
|
||||||
|
match (width, height) {
|
||||||
|
(0..=32, 0..=32) => Some(Self {
|
||||||
|
width: 32,
|
||||||
|
height: 32,
|
||||||
|
should_crop: true,
|
||||||
|
}),
|
||||||
|
(0..=96, 0..=96) => Some(Self {
|
||||||
|
width: 96,
|
||||||
|
height: 96,
|
||||||
|
should_crop: true,
|
||||||
|
}),
|
||||||
|
(0..=320, 0..=240) => Some(Self {
|
||||||
|
width: 320,
|
||||||
|
height: 240,
|
||||||
|
should_crop: false,
|
||||||
|
}),
|
||||||
|
(0..=640, 0..=480) => Some(Self {
|
||||||
|
width: 640,
|
||||||
|
height: 480,
|
||||||
|
should_crop: false,
|
||||||
|
}),
|
||||||
|
(0..=800, 0..=600) => Some(Self {
|
||||||
|
width: 800,
|
||||||
|
height: 600,
|
||||||
|
should_crop: false,
|
||||||
|
}),
|
||||||
|
_ => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Eq, Hash, PartialEq)]
|
||||||
|
struct MediaCreationData {
|
||||||
|
server_name: OwnedServerName,
|
||||||
|
media_id: String,
|
||||||
|
thumbnail_dimensions: Option<ThumbnailDimensions>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl MediaCreationData {
|
||||||
|
fn mxc(&self) -> MxcData<'_> {
|
||||||
|
MxcData {
|
||||||
|
server_name: &self.server_name,
|
||||||
|
media_id: &self.media_id,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl fmt::Debug for MediaCreationData {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
|
write!(f, "{:?}", self.mxc())?;
|
||||||
|
if let Some(ThumbnailDimensions {
|
||||||
|
width,
|
||||||
|
height,
|
||||||
|
..
|
||||||
|
}) = self.thumbnail_dimensions
|
||||||
|
{
|
||||||
|
write!(f, " @ {width}x{height}")?;
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Used to serialize media creation.
|
||||||
|
///
|
||||||
|
/// There can only be up to one holder of a `MediaCreationToken` for a given mxc
|
||||||
|
/// at any one time.
|
||||||
|
///
|
||||||
|
/// Returned by [`Service::get()`] when the file is not available locally.
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub(crate) struct MediaCreationToken(KeyToken<MediaCreationData>);
|
||||||
|
|
||||||
|
impl MediaCreationToken {
|
||||||
|
pub(crate) fn mxc(&self) -> MxcData<'_> {
|
||||||
|
self.0.mxc()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Used to serialize thumbnail creation.
|
||||||
|
///
|
||||||
|
/// There can only be up to one holder of a `ThumbnailCreationToken` for a given
|
||||||
|
/// mxc and resolution at any one time.
|
||||||
|
///
|
||||||
|
/// Returned by [`Service::get_thumbnail()`] when the thumbnail is not available
|
||||||
|
/// locally.
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub(crate) struct ThumbnailCreationToken(KeyToken<MediaCreationData>);
|
||||||
|
|
||||||
|
impl ThumbnailCreationToken {
|
||||||
|
pub(crate) fn width(&self) -> u32 {
|
||||||
|
self.0
|
||||||
|
.thumbnail_dimensions
|
||||||
|
.as_ref()
|
||||||
|
.expect("thumbnail creation token should have thumbnail dimensions")
|
||||||
|
.width
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn height(&self) -> u32 {
|
||||||
|
self.0
|
||||||
|
.thumbnail_dimensions
|
||||||
|
.as_ref()
|
||||||
|
.expect("thumbnail creation token should have thumbnail dimensions")
|
||||||
|
.height
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) enum ThumbnailResult {
|
||||||
|
/// Thumbnail available, corresponding metadata and file content returned.
|
||||||
|
Data(FileMeta, Vec<u8>),
|
||||||
|
/// Source file for thumbnail is required.
|
||||||
|
NeedSource(MediaCreationToken),
|
||||||
|
/// Thumbnail data is required (or can be generated from source file, once
|
||||||
|
/// present)
|
||||||
|
NeedThumbnail(ThumbnailCreationToken, MediaCreationToken),
|
||||||
|
}
|
||||||
|
|
||||||
pub(crate) struct Service {
|
pub(crate) struct Service {
|
||||||
pub(crate) db: &'static dyn Data,
|
pub(crate) db: &'static dyn Data,
|
||||||
|
|
||||||
|
creation_tokens: TokenSet<MediaCreationData>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Service {
|
impl Service {
|
||||||
/// Uploads a file.
|
pub(crate) fn new(db: &'static dyn Data) -> Service {
|
||||||
#[tracing::instrument(skip(self, file))]
|
Self {
|
||||||
pub(crate) async fn create(
|
db,
|
||||||
|
creation_tokens: TokenSet::new("media_creation".to_owned()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[instrument(skip(self))]
|
||||||
|
async fn lock_creation(
|
||||||
&self,
|
&self,
|
||||||
mxc: MxcData<'_>,
|
creation_data: MediaCreationData,
|
||||||
|
) -> KeyToken<MediaCreationData> {
|
||||||
|
self.creation_tokens.lock_key(creation_data).await
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tracing::instrument(skip(self, file))]
|
||||||
|
async fn create_inner(
|
||||||
|
&self,
|
||||||
|
creation_data: &MediaCreationData,
|
||||||
content_disposition: Option<&ContentDisposition>,
|
content_disposition: Option<&ContentDisposition>,
|
||||||
content_type: Option<String>,
|
content_type: Option<String>,
|
||||||
file: &[u8],
|
file: &[u8],
|
||||||
|
|
@ -110,8 +257,23 @@ impl Service {
|
||||||
.map(ContentDisposition::to_string),
|
.map(ContentDisposition::to_string),
|
||||||
content_type,
|
content_type,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let key = if let Some(ThumbnailDimensions {
|
||||||
|
width,
|
||||||
|
height,
|
||||||
|
..
|
||||||
|
}) = creation_data.thumbnail_dimensions
|
||||||
|
{
|
||||||
|
self.db.create_file_metadata(
|
||||||
|
creation_data.mxc(),
|
||||||
|
width,
|
||||||
|
height,
|
||||||
|
&meta,
|
||||||
|
)?
|
||||||
|
} else {
|
||||||
// Width, Height = 0 if it's not a thumbnail
|
// Width, Height = 0 if it's not a thumbnail
|
||||||
let key = self.db.create_file_metadata(mxc, 0, 0, &meta)?;
|
self.db.create_file_metadata(creation_data.mxc(), 0, 0, &meta)?
|
||||||
|
};
|
||||||
|
|
||||||
let path = services().globals.get_media_file(&key);
|
let path = services().globals.get_media_file(&key);
|
||||||
let mut f = File::create(path).await?;
|
let mut f = File::create(path).await?;
|
||||||
|
|
@ -119,41 +281,54 @@ impl Service {
|
||||||
Ok(meta)
|
Ok(meta)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Uploads or replaces a file.
|
||||||
|
pub(crate) async fn create(
|
||||||
|
&self,
|
||||||
|
MediaCreationToken(creation_data): MediaCreationToken,
|
||||||
|
content_disposition: Option<&ContentDisposition>,
|
||||||
|
content_type: Option<String>,
|
||||||
|
file: &[u8],
|
||||||
|
) -> Result<FileMeta> {
|
||||||
|
self.create_inner(
|
||||||
|
&creation_data,
|
||||||
|
content_disposition,
|
||||||
|
content_type,
|
||||||
|
file,
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
}
|
||||||
|
|
||||||
/// Uploads or replaces a file thumbnail.
|
/// Uploads or replaces a file thumbnail.
|
||||||
#[tracing::instrument(skip(self, file))]
|
pub(crate) async fn create_thumbnail(
|
||||||
pub(crate) async fn upload_thumbnail(
|
|
||||||
&self,
|
&self,
|
||||||
mxc: MxcData<'_>,
|
ThumbnailCreationToken(creation_data): ThumbnailCreationToken,
|
||||||
content_disposition: Option<String>,
|
content_disposition: Option<&ContentDisposition>,
|
||||||
content_type: Option<String>,
|
content_type: Option<String>,
|
||||||
width: u32,
|
|
||||||
height: u32,
|
|
||||||
file: &[u8],
|
file: &[u8],
|
||||||
) -> Result<FileMeta> {
|
) -> Result<FileMeta> {
|
||||||
let meta = FileMeta {
|
self.create_inner(
|
||||||
|
&creation_data,
|
||||||
content_disposition,
|
content_disposition,
|
||||||
content_type,
|
content_type,
|
||||||
};
|
file,
|
||||||
let key = self.db.create_file_metadata(mxc, width, height, &meta)?;
|
)
|
||||||
|
.await
|
||||||
let path = services().globals.get_media_file(&key);
|
|
||||||
let mut f = File::create(path).await?;
|
|
||||||
f.write_all(file).await?;
|
|
||||||
|
|
||||||
Ok(meta)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Downloads a file.
|
|
||||||
#[tracing::instrument(skip(self))]
|
#[tracing::instrument(skip(self))]
|
||||||
pub(crate) async fn get(
|
// internal function, and not actually that complex
|
||||||
|
#[allow(clippy::type_complexity)]
|
||||||
|
async fn get_inner(
|
||||||
&self,
|
&self,
|
||||||
mxc: MxcData<'_>,
|
mxc: MxcData<'_>,
|
||||||
) -> Result<Option<(FileMeta, Vec<u8>)>> {
|
thumbnail_dimensions: Option<ThumbnailDimensions>,
|
||||||
|
) -> Result<Either<(FileMeta, Vec<u8>), KeyToken<MediaCreationData>>> {
|
||||||
|
let try_get = || async {
|
||||||
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);
|
||||||
let mut file_data = Vec::new();
|
let mut file_data = Vec::new();
|
||||||
let Ok(mut file) = File::open(path).await else {
|
let Ok(mut file) = File::open(path).await else {
|
||||||
return Ok(None);
|
return Ok::<_, Error>(None);
|
||||||
};
|
};
|
||||||
|
|
||||||
file.read_to_end(&mut file_data).await?;
|
file.read_to_end(&mut file_data).await?;
|
||||||
|
|
@ -162,24 +337,48 @@ impl Service {
|
||||||
} else {
|
} else {
|
||||||
Ok(None)
|
Ok(None)
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
if let Some(ret) = try_get().await? {
|
||||||
|
return Ok(Either::Left(ret));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns width, height of the thumbnail and whether it should be cropped.
|
let token = self
|
||||||
/// Returns None when the server should send the original file.
|
.lock_creation(MediaCreationData {
|
||||||
fn thumbnail_properties(
|
server_name: mxc.server_name.to_owned(),
|
||||||
width: u32,
|
media_id: mxc.media_id.to_owned(),
|
||||||
height: u32,
|
thumbnail_dimensions,
|
||||||
) -> Option<(u32, u32, bool)> {
|
})
|
||||||
match (width, height) {
|
.await;
|
||||||
(0..=32, 0..=32) => Some((32, 32, true)),
|
|
||||||
(0..=96, 0..=96) => Some((96, 96, true)),
|
// check again in case it has been created in the meantime
|
||||||
(0..=320, 0..=240) => Some((320, 240, false)),
|
if let Some(ret) = try_get().await? {
|
||||||
(0..=640, 0..=480) => Some((640, 480, false)),
|
Ok(Either::Left(ret))
|
||||||
(0..=800, 0..=600) => Some((800, 600, false)),
|
} else {
|
||||||
_ => None,
|
Ok(Either::Right(token))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Loads a file from local storage.
|
||||||
|
pub(crate) async fn get(
|
||||||
|
&self,
|
||||||
|
mxc: MxcData<'_>,
|
||||||
|
) -> Result<Either<(FileMeta, Vec<u8>), MediaCreationToken>> {
|
||||||
|
Ok(self.get_inner(mxc, None).await?.map_right(MediaCreationToken))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Loads a thumbnail from local storage.
|
||||||
|
async fn get_thumbnail_local(
|
||||||
|
&self,
|
||||||
|
mxc: MxcData<'_>,
|
||||||
|
dimensions: ThumbnailDimensions,
|
||||||
|
) -> Result<Either<(FileMeta, Vec<u8>), ThumbnailCreationToken>> {
|
||||||
|
Ok(self
|
||||||
|
.get_inner(mxc, Some(dimensions))
|
||||||
|
.await?
|
||||||
|
.map_right(ThumbnailCreationToken))
|
||||||
|
}
|
||||||
|
|
||||||
/// Generates a thumbnail from the given image file contents. Returns
|
/// Generates a thumbnail from the given image file contents. Returns
|
||||||
/// `Ok(None)` if the input image should be used as-is.
|
/// `Ok(None)` if the input image should be used as-is.
|
||||||
#[tracing::instrument(
|
#[tracing::instrument(
|
||||||
|
|
@ -262,7 +461,7 @@ impl Service {
|
||||||
Ok(Some(thumbnail_bytes))
|
Ok(Some(thumbnail_bytes))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Downloads a file's thumbnail.
|
/// Loads or generates a file's thumbnail.
|
||||||
///
|
///
|
||||||
/// Here's an example on how it works:
|
/// Here's an example on how it works:
|
||||||
///
|
///
|
||||||
|
|
@ -281,30 +480,36 @@ impl Service {
|
||||||
mxc: MxcData<'_>,
|
mxc: MxcData<'_>,
|
||||||
width: u32,
|
width: u32,
|
||||||
height: u32,
|
height: u32,
|
||||||
) -> Result<Option<(FileMeta, Vec<u8>)>> {
|
) -> Result<ThumbnailResult> {
|
||||||
// 0, 0 because that's the original file
|
let Some(dimensions) = ThumbnailDimensions::new(width, height) else {
|
||||||
let (width, height, crop) =
|
// image should be used as-is
|
||||||
Self::thumbnail_properties(width, height).unwrap_or((0, 0, false));
|
return match self.get(mxc).await? {
|
||||||
|
Either::Left((meta, file)) => {
|
||||||
if let Ok((meta, key)) =
|
Ok(ThumbnailResult::Data(meta, file))
|
||||||
self.db.search_file_metadata(mxc, width, height)
|
|
||||||
{
|
|
||||||
debug!("Using saved thumbnail");
|
|
||||||
let path = services().globals.get_media_file(&key);
|
|
||||||
let mut file = Vec::new();
|
|
||||||
File::open(path).await?.read_to_end(&mut file).await?;
|
|
||||||
|
|
||||||
return Ok(Some((meta, file.clone())));
|
|
||||||
}
|
}
|
||||||
|
Either::Right(token) => Ok(ThumbnailResult::NeedSource(token)),
|
||||||
let Ok((meta, key)) = self.db.search_file_metadata(mxc, 0, 0) else {
|
};
|
||||||
debug!("Original image not found, can't generate thumbnail");
|
|
||||||
return Ok(None);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
let path = services().globals.get_media_file(&key);
|
let thumbnail_token =
|
||||||
let mut file = Vec::new();
|
match self.get_thumbnail_local(mxc, dimensions).await? {
|
||||||
File::open(path).await?.read_to_end(&mut file).await?;
|
Either::Left((meta, file)) => {
|
||||||
|
debug!("Using saved thumbnail");
|
||||||
|
return Ok(ThumbnailResult::Data(meta, file));
|
||||||
|
}
|
||||||
|
Either::Right(token) => token,
|
||||||
|
};
|
||||||
|
|
||||||
|
let (meta, file) = match self.get(mxc).await? {
|
||||||
|
Either::Left(ret) => ret,
|
||||||
|
Either::Right(media_token) => {
|
||||||
|
debug!("Original image not found, can't generate thumbnail");
|
||||||
|
return Ok(ThumbnailResult::NeedThumbnail(
|
||||||
|
thumbnail_token,
|
||||||
|
media_token,
|
||||||
|
));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
debug!("Generating thumbnail");
|
debug!("Generating thumbnail");
|
||||||
let thumbnail_result = {
|
let thumbnail_result = {
|
||||||
|
|
@ -313,7 +518,12 @@ impl Service {
|
||||||
|
|
||||||
tokio::task::spawn_blocking(move || {
|
tokio::task::spawn_blocking(move || {
|
||||||
outer_span.in_scope(|| {
|
outer_span.in_scope(|| {
|
||||||
Self::generate_thumbnail(&file, width, height, crop)
|
Self::generate_thumbnail(
|
||||||
|
&file,
|
||||||
|
width,
|
||||||
|
height,
|
||||||
|
dimensions.should_crop,
|
||||||
|
)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
.await
|
.await
|
||||||
|
|
@ -322,22 +532,20 @@ impl Service {
|
||||||
|
|
||||||
let Some(thumbnail_bytes) = thumbnail_result? else {
|
let Some(thumbnail_bytes) = thumbnail_result? else {
|
||||||
debug!("Returning source image as-is");
|
debug!("Returning source image as-is");
|
||||||
return Ok(Some((meta, file)));
|
return Ok(ThumbnailResult::Data(meta, file));
|
||||||
};
|
};
|
||||||
|
|
||||||
debug!("Saving created thumbnail");
|
debug!("Saving created thumbnail");
|
||||||
|
|
||||||
let meta = self
|
let meta = self
|
||||||
.upload_thumbnail(
|
.create_thumbnail(
|
||||||
mxc,
|
thumbnail_token,
|
||||||
meta.content_disposition,
|
None,
|
||||||
meta.content_type,
|
meta.content_type,
|
||||||
width,
|
|
||||||
height,
|
|
||||||
&thumbnail_bytes,
|
&thumbnail_bytes,
|
||||||
)
|
)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
Ok(Some((meta, thumbnail_bytes.clone())))
|
Ok(ThumbnailResult::Data(meta, thumbnail_bytes.clone()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue