fix fmt and clippy warnings

next
hamidreza kalbasi 2021-06-06 16:58:32 +04:30 committed by Timo Kösters
parent 972caacdc2
commit 804105479c
No known key found for this signature in database
GPG Key ID: 24DA7517711A2BA4
3 changed files with 73 additions and 47 deletions

View File

@ -36,17 +36,20 @@ pub async fn create_content_route(
db.globals.server_name(), db.globals.server_name(),
utils::random_string(MXC_LENGTH) utils::random_string(MXC_LENGTH)
); );
db.media.create(
mxc.clone(), db.media
&db.globals, .create(
&body mxc.clone(),
.filename &db.globals,
.as_ref() &body
.map(|filename| "inline; filename=".to_owned() + filename) .filename
.as_deref(), .as_ref()
&body.content_type.as_deref(), .map(|filename| "inline; filename=".to_owned() + filename)
&body.file, .as_deref(),
).await?; &body.content_type.as_deref(),
&body.file,
)
.await?;
db.flush().await?; db.flush().await?;
@ -94,13 +97,15 @@ pub async fn get_content_route(
) )
.await?; .await?;
db.media.create( db.media
mxc, .create(
&db.globals, mxc,
&get_content_response.content_disposition.as_deref(), &db.globals,
&get_content_response.content_type.as_deref(), &get_content_response.content_disposition.as_deref(),
&get_content_response.file, &get_content_response.content_type.as_deref(),
).await?; &get_content_response.file,
)
.await?;
Ok(get_content_response.into()) Ok(get_content_response.into())
} else { } else {
@ -121,16 +126,20 @@ pub async fn get_content_thumbnail_route(
if let Some(FileMeta { if let Some(FileMeta {
content_type, file, .. content_type, file, ..
}) = db.media.get_thumbnail( }) = db
mxc.clone(), .media
&db.globals, .get_thumbnail(
body.width mxc.clone(),
.try_into() &db.globals,
.map_err(|_| Error::BadRequest(ErrorKind::InvalidParam, "Width is invalid."))?, body.width
body.height .try_into()
.try_into() .map_err(|_| Error::BadRequest(ErrorKind::InvalidParam, "Width is invalid."))?,
.map_err(|_| Error::BadRequest(ErrorKind::InvalidParam, "Width is invalid."))?, body.height
).await? { .try_into()
.map_err(|_| Error::BadRequest(ErrorKind::InvalidParam, "Width is invalid."))?,
)
.await?
{
Ok(get_content_thumbnail::Response { file, content_type }.into()) Ok(get_content_thumbnail::Response { file, content_type }.into())
} else if &*body.server_name != db.globals.server_name() && body.allow_remote { } else if &*body.server_name != db.globals.server_name() && body.allow_remote {
let get_thumbnail_response = db let get_thumbnail_response = db
@ -149,15 +158,17 @@ pub async fn get_content_thumbnail_route(
) )
.await?; .await?;
db.media.upload_thumbnail( db.media
mxc, .upload_thumbnail(
&db.globals, mxc,
&None, &db.globals,
&get_thumbnail_response.content_type, &None,
body.width.try_into().expect("all UInts are valid u32s"), &get_thumbnail_response.content_type,
body.height.try_into().expect("all UInts are valid u32s"), body.width.try_into().expect("all UInts are valid u32s"),
&get_thumbnail_response.file, body.height.try_into().expect("all UInts are valid u32s"),
).await?; &get_thumbnail_response.file,
)
.await?;
Ok(get_thumbnail_response.into()) Ok(get_thumbnail_response.into())
} else { } else {

View File

@ -5,7 +5,12 @@ use ruma::{
EventId, MilliSecondsSinceUnixEpoch, ServerName, ServerSigningKeyId, EventId, MilliSecondsSinceUnixEpoch, ServerName, ServerSigningKeyId,
}; };
use rustls::{ServerCertVerifier, WebPKIVerifier}; use rustls::{ServerCertVerifier, WebPKIVerifier};
use std::{collections::{BTreeMap, HashMap}, path::{PathBuf}, sync::{Arc, RwLock}, time::{Duration, Instant}}; use std::{
collections::{BTreeMap, HashMap},
path::PathBuf,
sync::{Arc, RwLock},
time::{Duration, Instant},
};
use tokio::sync::Semaphore; use tokio::sync::Semaphore;
use trust_dns_resolver::TokioAsyncResolver; use trust_dns_resolver::TokioAsyncResolver;
@ -279,7 +284,7 @@ impl Globals {
r r
} }
pub fn get_media_file(&self, key: &Vec<u8>) -> PathBuf { pub fn get_media_file(&self, key: &[u8]) -> PathBuf {
let mut r = PathBuf::new(); let mut r = PathBuf::new();
r.push(self.config.database_path.clone()); r.push(self.config.database_path.clone());
r.push("media"); r.push("media");

View File

@ -1,10 +1,14 @@
use image::{imageops::FilterType, GenericImageView};
use crate::database::globals::Globals; use crate::database::globals::Globals;
use image::{imageops::FilterType, GenericImageView};
use super::abstraction::Tree;
use crate::{utils, Error, Result}; use crate::{utils, Error, Result};
use std::{mem, sync::Arc}; use std::{mem, sync::Arc};
use super::abstraction::Tree; use tokio::{
use tokio::{fs::{self, File}, io::AsyncWriteExt, io::AsyncReadExt}; fs::{self, File},
io::AsyncReadExt,
io::AsyncWriteExt,
};
pub struct FileMeta { pub struct FileMeta {
pub content_disposition: Option<String>, pub content_disposition: Option<String>,
@ -167,7 +171,13 @@ impl Media {
/// - Server creates the thumbnail and sends it to the user /// - Server creates the thumbnail and sends it to the user
/// ///
/// For width,height <= 96 the server uses another thumbnailing algorithm which crops the image afterwards. /// For width,height <= 96 the server uses another thumbnailing algorithm which crops the image afterwards.
pub async fn get_thumbnail(&self, mxc: String, globals: &Globals, width: u32, height: u32) -> Result<Option<FileMeta>> { pub async fn get_thumbnail(
&self,
mxc: String,
globals: &Globals,
width: u32,
height: u32,
) -> Result<Option<FileMeta>> {
let (width, height, crop) = self let (width, height, crop) = self
.thumbnail_properties(width, height) .thumbnail_properties(width, height)
.unwrap_or((0, 0, false)); // 0, 0 because that's the original file .unwrap_or((0, 0, false)); // 0, 0 because that's the original file
@ -332,14 +342,14 @@ impl Media {
Ok(Some(FileMeta { Ok(Some(FileMeta {
content_disposition, content_disposition,
content_type, content_type,
file: thumbnail_bytes.to_vec() file: thumbnail_bytes.to_vec(),
})) }))
} else { } else {
// Couldn't parse file to generate thumbnail, send original // Couldn't parse file to generate thumbnail, send original
Ok(Some(FileMeta { Ok(Some(FileMeta {
content_disposition, content_disposition,
content_type, content_type,
file: file.to_vec() file: file.to_vec(),
})) }))
} }
} else { } else {