Fix some clippy lints

This commit is contained in:
Klemens Schölhorn 2023-03-10 22:00:51 +01:00
parent 0af84a1ce8
commit 02dcb9d5d0

View File

@ -1,4 +1,4 @@
use std::{net::SocketAddr, path::{Path, PathBuf}, ffi::OsStr, fs::File, io::{BufReader, Seek, SeekFrom, Cursor, BufRead}, time::SystemTime, env::args_os}; use std::{net::SocketAddr, path::{Path, PathBuf}, ffi::OsStr, fs::File, io::{BufReader, Seek, Cursor, BufRead}, time::SystemTime, env::args_os};
use axum::{Router, routing::{get, post}, response::{IntoResponse, Redirect}, http::{StatusCode, header}, extract::{self, State}, Form, handler::Handler}; use axum::{Router, routing::{get, post}, response::{IntoResponse, Redirect}, http::{StatusCode, header}, extract::{self, State}, Form, handler::Handler};
use axum_sessions::{async_session::CookieStore, SessionLayer, extractors::{ReadableSession, WritableSession}}; use axum_sessions::{async_session::CookieStore, SessionLayer, extractors::{ReadableSession, WritableSession}};
@ -30,7 +30,7 @@ struct ApplicationState {
#[tokio::main] #[tokio::main]
async fn main() { async fn main() {
let image_path = args_os().skip(1).next().expect("Usage: image-gallery IMAGE_DIRECTORY"); let image_path = args_os().nth(1).expect("Usage: image-gallery IMAGE_DIRECTORY");
let default_tracing = "image_gallery=debug,tower_http=info".into(); let default_tracing = "image_gallery=debug,tower_http=info".into();
let tracing_filter = EnvFilter::try_from_default_env().unwrap_or(default_tracing); let tracing_filter = EnvFilter::try_from_default_env().unwrap_or(default_tracing);
@ -103,7 +103,7 @@ async fn index(
RenderHtml("index", engine, IndexTempalte { RenderHtml("index", engine, IndexTempalte {
title: "Some pictures".into(), title: "Some pictures".into(),
images: images, images,
}) })
} else { } else {
RenderHtml("login", engine, IndexTempalte { RenderHtml("login", engine, IndexTempalte {
@ -153,7 +153,7 @@ async fn converted_image(
let mut image = debug_span!("decode_image", let mut image = debug_span!("decode_image",
image=?image_path.file_name(), image=?image_path.file_name(),
).in_scope(|| -> Result<_, StatusCode>{ ).in_scope(|| -> Result<_, StatusCode>{
file.seek(SeekFrom::Start(0)).map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?; file.rewind().map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
image::io::Reader::new(&mut file) image::io::Reader::new(&mut file)
.with_guessed_format() .with_guessed_format()
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)? .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?
@ -184,7 +184,7 @@ async fn converted_image(
}).await }).await
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)??; .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)??;
return Ok(( Ok((
[ [
(header::CONTENT_TYPE, "image/webp"), (header::CONTENT_TYPE, "image/webp"),
], ],
@ -193,14 +193,14 @@ async fn converted_image(
} }
fn read_exif_data(mut file: impl BufRead+Seek) -> Result<Exif, ()> { fn read_exif_data(mut file: impl BufRead+Seek) -> Result<Exif, ()> {
file.seek(SeekFrom::Start(0)).map_err(|_| ())?; file.rewind().map_err(|_| ())?;
let exifreader = exif::Reader::new(); let exifreader = exif::Reader::new();
exifreader.read_from_container(&mut file) exifreader.read_from_container(&mut file)
.map_err(|_| ()) .map_err(|_| ())
} }
fn read_image_size(mut file: impl BufRead+Seek, exif: Option<&Exif>) -> Result<(u32, u32), ()> { fn read_image_size(mut file: impl BufRead+Seek, exif: Option<&Exif>) -> Result<(u32, u32), ()> {
file.seek(SeekFrom::Start(0)).map_err(|_| ())?; file.rewind().map_err(|_| ())?;
let (mut width, mut height) = image::io::Reader::new(&mut file) let (mut width, mut height) = image::io::Reader::new(&mut file)
.with_guessed_format() .with_guessed_format()
@ -209,13 +209,10 @@ fn read_image_size(mut file: impl BufRead+Seek, exif: Option<&Exif>) -> Result<(
.map_err(|_| ())?; .map_err(|_| ())?;
if let Some(exif) = exif { if let Some(exif) = exif {
match exif.get_field(Tag::Orientation, In::PRIMARY) { if let Some(orientation) = exif.get_field(Tag::Orientation, In::PRIMARY) {
Some(orientation) => if let Some(5 | 6 | 7 | 8) = orientation.value.get_uint(0) {
match orientation.value.get_uint(0) { std::mem::swap(&mut width, &mut height);
Some(5 | 6 | 7 | 8) => std::mem::swap(&mut width, &mut height), };
_ => {},
},
None => {},
}; };
}; };
@ -244,9 +241,8 @@ fn fix_image_orientation(image: DynamicImage, exif: &Exif) -> DynamicImage {
fn read_images(directory: &Path) -> Vec<ImageInfo> { fn read_images(directory: &Path) -> Vec<ImageInfo> {
let mut files = vec![]; let mut files = vec![];
for file in directory.read_dir().expect("read_dir call failed") { for file in directory.read_dir().expect("read_dir call failed").flatten() {
if let Ok(file_entry) = file { let path = file.path();
let path = file_entry.path();
if path.extension() == Some(OsStr::new("jpg")) { if path.extension() == Some(OsStr::new("jpg")) {
let file = File::open(&path).unwrap(); let file = File::open(&path).unwrap();
let mut file = BufReader::new(file); let mut file = BufReader::new(file);
@ -272,15 +268,14 @@ fn read_images(directory: &Path) -> Vec<ImageInfo> {
}; };
let image_info = ImageInfo { let image_info = ImageInfo {
width: width, width,
height: height, height,
name: path.file_name().expect("invalid file path").to_string_lossy().to_string(), name: path.file_name().expect("invalid file path").to_string_lossy().to_string(),
}; };
files.push(image_info); files.push(image_info);
} }
} }
}
files files
} }