fix warnings and errors when building with no features
Signed-off-by: strawberry <strawberry@puppygock.gay>
This commit is contained in:
parent
f761d4d5c9
commit
ef2d307c15
3 changed files with 51 additions and 55 deletions
|
@ -1,20 +1,30 @@
|
|||
use std::{error::Error, ffi::OsStr, fmt::Display, io::Cursor, path::Path};
|
||||
|
||||
use conduwuit::{config::BlurhashConfig as CoreBlurhashConfig, err, implement, Result};
|
||||
use image::{DynamicImage, ImageDecoder, ImageError, ImageFormat, ImageReader};
|
||||
#[cfg(feature = "blurhashing")]
|
||||
use conduwuit::config::BlurhashConfig as CoreBlurhashConfig;
|
||||
use conduwuit::{implement, Result};
|
||||
|
||||
use super::Service;
|
||||
|
||||
#[implement(Service)]
|
||||
#[cfg(not(feature = "blurhashing"))]
|
||||
pub fn create_blurhash(
|
||||
&self,
|
||||
_file: &[u8],
|
||||
_content_type: Option<&str>,
|
||||
_file_name: Option<&str>,
|
||||
) -> Result<Option<String>> {
|
||||
conduwuit::debug_warn!("blurhashing on upload support was not compiled");
|
||||
|
||||
Ok(None)
|
||||
}
|
||||
|
||||
#[implement(Service)]
|
||||
#[cfg(feature = "blurhashing")]
|
||||
pub fn create_blurhash(
|
||||
&self,
|
||||
file: &[u8],
|
||||
content_type: Option<&str>,
|
||||
file_name: Option<&str>,
|
||||
) -> Result<Option<String>> {
|
||||
if !cfg!(feature = "blurhashing") {
|
||||
return Ok(None);
|
||||
}
|
||||
|
||||
let config = BlurhashConfig::from(self.services.server.config.blurhashing);
|
||||
|
||||
// since 0 means disabled blurhashing, skipped blurhashing
|
||||
|
@ -23,7 +33,7 @@ pub fn create_blurhash(
|
|||
}
|
||||
|
||||
get_blurhash_from_request(file, content_type, file_name, config)
|
||||
.map_err(|e| err!(debug_error!("blurhashing error: {e}")))
|
||||
.map_err(|e| conduwuit::err!(debug_error!("blurhashing error: {e}")))
|
||||
.map(Some)
|
||||
}
|
||||
|
||||
|
@ -36,6 +46,7 @@ pub fn create_blurhash(
|
|||
bytes = data.len(),
|
||||
),
|
||||
)]
|
||||
#[cfg(feature = "blurhashing")]
|
||||
fn get_blurhash_from_request(
|
||||
data: &[u8],
|
||||
mime: Option<&str>,
|
||||
|
@ -53,8 +64,7 @@ fn get_blurhash_from_request(
|
|||
return Err(BlurhashingError::ImageTooLarge);
|
||||
}
|
||||
|
||||
// decode the image finally
|
||||
let image = DynamicImage::from_decoder(decoder)?;
|
||||
let image = image::DynamicImage::from_decoder(decoder)?;
|
||||
|
||||
blurhash_an_image(&image, config)
|
||||
}
|
||||
|
@ -64,31 +74,34 @@ fn get_blurhash_from_request(
|
|||
/// Then it checks if the filename has a format, otherwise just guess based on
|
||||
/// the binary data Assumes that mime and filename extension won't be for a
|
||||
/// different file format than file.
|
||||
#[cfg(feature = "blurhashing")]
|
||||
fn get_format_from_data_mime_and_filename(
|
||||
data: &[u8],
|
||||
mime: Option<&str>,
|
||||
filename: Option<&str>,
|
||||
) -> Result<ImageFormat, BlurhashingError> {
|
||||
) -> Result<image::ImageFormat, BlurhashingError> {
|
||||
let extension = filename
|
||||
.map(Path::new)
|
||||
.and_then(Path::extension)
|
||||
.map(OsStr::to_string_lossy);
|
||||
.map(std::path::Path::new)
|
||||
.and_then(std::path::Path::extension)
|
||||
.map(std::ffi::OsStr::to_string_lossy);
|
||||
|
||||
mime.or(extension.as_deref())
|
||||
.and_then(ImageFormat::from_mime_type)
|
||||
.and_then(image::ImageFormat::from_mime_type)
|
||||
.map_or_else(|| image::guess_format(data).map_err(Into::into), Ok)
|
||||
}
|
||||
|
||||
#[cfg(feature = "blurhashing")]
|
||||
fn get_image_decoder_with_format_and_data(
|
||||
image_format: ImageFormat,
|
||||
image_format: image::ImageFormat,
|
||||
data: &[u8],
|
||||
) -> Result<Box<dyn ImageDecoder + '_>, BlurhashingError> {
|
||||
let mut image_reader = ImageReader::new(Cursor::new(data));
|
||||
) -> Result<Box<dyn image::ImageDecoder + '_>, BlurhashingError> {
|
||||
let mut image_reader = image::ImageReader::new(std::io::Cursor::new(data));
|
||||
image_reader.set_format(image_format);
|
||||
Ok(Box::new(image_reader.into_decoder()?))
|
||||
}
|
||||
|
||||
fn is_image_above_size_limit<T: ImageDecoder>(
|
||||
#[cfg(feature = "blurhashing")]
|
||||
fn is_image_above_size_limit<T: image::ImageDecoder>(
|
||||
decoder: &T,
|
||||
blurhash_config: BlurhashConfig,
|
||||
) -> bool {
|
||||
|
@ -99,7 +112,7 @@ fn is_image_above_size_limit<T: ImageDecoder>(
|
|||
#[tracing::instrument(name = "encode", level = "debug", skip_all)]
|
||||
#[inline]
|
||||
fn blurhash_an_image(
|
||||
image: &DynamicImage,
|
||||
image: &image::DynamicImage,
|
||||
blurhash_config: BlurhashConfig,
|
||||
) -> Result<String, BlurhashingError> {
|
||||
Ok(blurhash::encode_image(
|
||||
|
@ -109,15 +122,6 @@ fn blurhash_an_image(
|
|||
)?)
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "blurhashing"))]
|
||||
#[inline]
|
||||
fn blurhash_an_image(
|
||||
_image: &DynamicImage,
|
||||
_blurhash_config: BlurhashConfig,
|
||||
) -> Result<String, BlurhashingError> {
|
||||
Err(BlurhashingError::Unavailable)
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
pub struct BlurhashConfig {
|
||||
pub components_x: u32,
|
||||
|
@ -127,6 +131,7 @@ pub struct BlurhashConfig {
|
|||
pub size_limit: u64,
|
||||
}
|
||||
|
||||
#[cfg(feature = "blurhashing")]
|
||||
impl From<CoreBlurhashConfig> for BlurhashConfig {
|
||||
fn from(value: CoreBlurhashConfig) -> Self {
|
||||
Self {
|
||||
|
@ -138,17 +143,17 @@ impl From<CoreBlurhashConfig> for BlurhashConfig {
|
|||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
#[cfg(feature = "blurhashing")]
|
||||
pub enum BlurhashingError {
|
||||
HashingLibError(Box<dyn Error + Send>),
|
||||
ImageError(Box<ImageError>),
|
||||
HashingLibError(Box<dyn std::error::Error + Send>),
|
||||
#[cfg(feature = "blurhashing")]
|
||||
ImageError(Box<image::ImageError>),
|
||||
ImageTooLarge,
|
||||
|
||||
#[cfg(not(feature = "blurhashing"))]
|
||||
Unavailable,
|
||||
}
|
||||
|
||||
impl From<ImageError> for BlurhashingError {
|
||||
fn from(value: ImageError) -> Self { Self::ImageError(Box::new(value)) }
|
||||
#[cfg(feature = "blurhashing")]
|
||||
impl From<image::ImageError> for BlurhashingError {
|
||||
fn from(value: image::ImageError) -> Self { Self::ImageError(Box::new(value)) }
|
||||
}
|
||||
|
||||
#[cfg(feature = "blurhashing")]
|
||||
|
@ -156,19 +161,17 @@ impl From<blurhash::Error> for BlurhashingError {
|
|||
fn from(value: blurhash::Error) -> Self { Self::HashingLibError(Box::new(value)) }
|
||||
}
|
||||
|
||||
impl Display for BlurhashingError {
|
||||
#[cfg(feature = "blurhashing")]
|
||||
impl std::fmt::Display for BlurhashingError {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f, "Blurhash Error:")?;
|
||||
match &self {
|
||||
| Self::ImageTooLarge => write!(f, "Image was too large to blurhash")?,
|
||||
| Self::HashingLibError(e) =>
|
||||
write!(f, "There was an error with the blurhashing library => {e}")?,
|
||||
|
||||
#[cfg(feature = "blurhashing")]
|
||||
| Self::ImageError(e) =>
|
||||
write!(f, "There was an error with the image loading library => {e}")?,
|
||||
|
||||
#[cfg(not(feature = "blurhashing"))]
|
||||
| Self::Unavailable => write!(f, "Blurhashing is not supported")?,
|
||||
};
|
||||
|
||||
Ok(())
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue