feat: move image check to utils.rs
This commit is contained in:
parent
149b81973d
commit
81f7527c79
2 changed files with 28 additions and 28 deletions
21
src/utils.rs
21
src/utils.rs
|
@ -1,7 +1,8 @@
|
|||
use actix_web::{
|
||||
cookie::{Cookie, SameSite, time::Duration},
|
||||
http::header::HeaderMap,
|
||||
cookie::{time::Duration, Cookie, SameSite},
|
||||
http::header::HeaderMap, web::BytesMut,
|
||||
};
|
||||
use bindet::FileType;
|
||||
use getrandom::fill;
|
||||
use hex::encode;
|
||||
use redis::RedisError;
|
||||
|
@ -59,6 +60,22 @@ pub fn generate_refresh_token() -> Result<String, getrandom::Error> {
|
|||
Ok(encode(buf))
|
||||
}
|
||||
|
||||
pub fn image_check(icon: BytesMut) -> Result<String, Error> {
|
||||
let buf = std::io::Cursor::new(icon);
|
||||
|
||||
let detect = bindet::detect(buf).map_err(|e| e.kind());
|
||||
|
||||
if let Ok(Some(file_type)) = detect {
|
||||
if file_type.likely_to_be == vec![FileType::Jpg] {
|
||||
return Ok(String::from("jpg"))
|
||||
} else if file_type.likely_to_be == vec![FileType::Png] {
|
||||
return Ok(String::from("png"))
|
||||
}
|
||||
}
|
||||
|
||||
Err(Error::BadRequest("Uploaded file is not an image".to_string()))
|
||||
}
|
||||
|
||||
impl Data {
|
||||
pub async fn set_cache_key(
|
||||
&self,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue