mirror of
https://gitlab.computer.surgery/matrix/grapevine.git
synced 2025-12-18 08:11:24 +01:00
service/media: refactor to reduce indentation
This commit is contained in:
parent
ec1b086a35
commit
c973485c73
1 changed files with 102 additions and 107 deletions
|
|
@ -151,20 +151,35 @@ impl Service {
|
||||||
let mut file = Vec::new();
|
let mut file = Vec::new();
|
||||||
File::open(path).await?.read_to_end(&mut file).await?;
|
File::open(path).await?.read_to_end(&mut file).await?;
|
||||||
|
|
||||||
Ok(Some(FileMeta {
|
return Ok(Some(FileMeta {
|
||||||
content_disposition,
|
content_disposition,
|
||||||
content_type,
|
content_type,
|
||||||
file: file.clone(),
|
file: file.clone(),
|
||||||
}))
|
}));
|
||||||
} else if let Ok((content_disposition, content_type, key)) =
|
}
|
||||||
|
|
||||||
|
// thumbnail not found, generate
|
||||||
|
|
||||||
|
let Ok((content_disposition, content_type, key)) =
|
||||||
self.db.search_file_metadata(mxc.clone(), 0, 0)
|
self.db.search_file_metadata(mxc.clone(), 0, 0)
|
||||||
{
|
else {
|
||||||
|
return Ok(None);
|
||||||
|
};
|
||||||
|
|
||||||
// Generate a thumbnail
|
// Generate a thumbnail
|
||||||
let path = services().globals.get_media_file(&key);
|
let path = services().globals.get_media_file(&key);
|
||||||
let mut file = Vec::new();
|
let mut file = Vec::new();
|
||||||
File::open(path).await?.read_to_end(&mut file).await?;
|
File::open(path).await?.read_to_end(&mut file).await?;
|
||||||
|
|
||||||
if let Ok(image) = image::load_from_memory(&file) {
|
let Ok(image) = image::load_from_memory(&file) else {
|
||||||
|
// Couldn't parse file to generate thumbnail, send original
|
||||||
|
return Ok(Some(FileMeta {
|
||||||
|
content_disposition,
|
||||||
|
content_type,
|
||||||
|
file: file.clone(),
|
||||||
|
}));
|
||||||
|
};
|
||||||
|
|
||||||
let original_width = image.width();
|
let original_width = image.width();
|
||||||
let original_height = image.height();
|
let original_height = image.height();
|
||||||
if width > original_width || height > original_height {
|
if width > original_width || height > original_height {
|
||||||
|
|
@ -180,8 +195,7 @@ impl Service {
|
||||||
} else {
|
} else {
|
||||||
let (exact_width, exact_height) = {
|
let (exact_width, exact_height) = {
|
||||||
// Copied from image::dynimage::resize_dimensions
|
// Copied from image::dynimage::resize_dimensions
|
||||||
let use_width = (u64::from(width)
|
let use_width = (u64::from(width) * u64::from(original_height))
|
||||||
* u64::from(original_height))
|
|
||||||
<= (u64::from(original_width) * u64::from(height));
|
<= (u64::from(original_width) * u64::from(height));
|
||||||
let intermediate = if use_width {
|
let intermediate = if use_width {
|
||||||
u64::from(original_height) * u64::from(width)
|
u64::from(original_height) * u64::from(width)
|
||||||
|
|
@ -192,14 +206,10 @@ impl Service {
|
||||||
};
|
};
|
||||||
if use_width {
|
if use_width {
|
||||||
if intermediate <= u64::from(::std::u32::MAX) {
|
if intermediate <= u64::from(::std::u32::MAX) {
|
||||||
(
|
(width, intermediate.try_into().unwrap_or(u32::MAX))
|
||||||
width,
|
|
||||||
intermediate.try_into().unwrap_or(u32::MAX),
|
|
||||||
)
|
|
||||||
} else {
|
} else {
|
||||||
(
|
(
|
||||||
(u64::from(width)
|
(u64::from(width) * u64::from(::std::u32::MAX)
|
||||||
* u64::from(::std::u32::MAX)
|
|
||||||
/ intermediate)
|
/ intermediate)
|
||||||
.try_into()
|
.try_into()
|
||||||
.unwrap_or(u32::MAX),
|
.unwrap_or(u32::MAX),
|
||||||
|
|
@ -207,15 +217,11 @@ impl Service {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
} else if intermediate <= u64::from(::std::u32::MAX) {
|
} else if intermediate <= u64::from(::std::u32::MAX) {
|
||||||
(
|
(intermediate.try_into().unwrap_or(u32::MAX), height)
|
||||||
intermediate.try_into().unwrap_or(u32::MAX),
|
|
||||||
height,
|
|
||||||
)
|
|
||||||
} else {
|
} else {
|
||||||
(
|
(
|
||||||
::std::u32::MAX,
|
::std::u32::MAX,
|
||||||
(u64::from(height)
|
(u64::from(height) * u64::from(::std::u32::MAX)
|
||||||
* u64::from(::std::u32::MAX)
|
|
||||||
/ intermediate)
|
/ intermediate)
|
||||||
.try_into()
|
.try_into()
|
||||||
.unwrap_or(u32::MAX),
|
.unwrap_or(u32::MAX),
|
||||||
|
|
@ -251,16 +257,5 @@ impl Service {
|
||||||
content_type,
|
content_type,
|
||||||
file: thumbnail_bytes.clone(),
|
file: thumbnail_bytes.clone(),
|
||||||
}))
|
}))
|
||||||
} else {
|
|
||||||
// Couldn't parse file to generate thumbnail, send original
|
|
||||||
Ok(Some(FileMeta {
|
|
||||||
content_disposition,
|
|
||||||
content_type,
|
|
||||||
file: file.clone(),
|
|
||||||
}))
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
Ok(None)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue