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_sessions::{async_session::CookieStore, SessionLayer, extractors::{ReadableSession, WritableSession}};
@ -30,7 +30,7 @@ struct ApplicationState {
#[tokio::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 tracing_filter = EnvFilter::try_from_default_env().unwrap_or(default_tracing);
@ -103,7 +103,7 @@ async fn index(
RenderHtml("index", engine, IndexTempalte {
title: "Some pictures".into(),
images: images,
images,
})
} else {
RenderHtml("login", engine, IndexTempalte {
@ -153,7 +153,7 @@ async fn converted_image(
let mut image = debug_span!("decode_image",
image=?image_path.file_name(),
).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)
.with_guessed_format()
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?
@ -184,7 +184,7 @@ async fn converted_image(
}).await
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)??;
return Ok((
Ok((
[
(header::CONTENT_TYPE, "image/webp"),
],
@ -193,14 +193,14 @@ async fn converted_image(
}
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();
exifreader.read_from_container(&mut file)
.map_err(|_| ())
}
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)
.with_guessed_format()
@ -209,13 +209,10 @@ fn read_image_size(mut file: impl BufRead+Seek, exif: Option<&Exif>) -> Result<(
.map_err(|_| ())?;
if let Some(exif) = exif {
match exif.get_field(Tag::Orientation, In::PRIMARY) {
Some(orientation) =>
match orientation.value.get_uint(0) {
Some(5 | 6 | 7 | 8) => std::mem::swap(&mut width, &mut height),
_ => {},
},
None => {},
if let Some(orientation) = exif.get_field(Tag::Orientation, In::PRIMARY) {
if let Some(5 | 6 | 7 | 8) = orientation.value.get_uint(0) {
std::mem::swap(&mut width, &mut height);
};
};
};
@ -244,41 +241,39 @@ fn fix_image_orientation(image: DynamicImage, exif: &Exif) -> DynamicImage {
fn read_images(directory: &Path) -> Vec<ImageInfo> {
let mut files = vec![];
for file in directory.read_dir().expect("read_dir call failed") {
if let Ok(file_entry) = file {
let path = file_entry.path();
if path.extension() == Some(OsStr::new("jpg")) {
let file = File::open(&path).unwrap();
let mut file = BufReader::new(file);
for file in directory.read_dir().expect("read_dir call failed").flatten() {
let path = file.path();
if path.extension() == Some(OsStr::new("jpg")) {
let file = File::open(&path).unwrap();
let mut file = BufReader::new(file);
let exif = read_exif_data(&mut file);
let exif = read_exif_data(&mut file);
// Check if we need to flip the coordinates
let (width, height) = read_image_size(&mut file, exif.as_ref().ok()).unwrap();
// Check if we need to flip the coordinates
let (width, height) = read_image_size(&mut file, exif.as_ref().ok()).unwrap();
let datetime = 'datetime: {
// First, try to read creation date from EXIF data
if let Ok(ref exif) = exif {
match exif.get_field(Tag::DateTimeOriginal, In::PRIMARY) {
Some(Field { value: Value::Ascii(value), ..}) if !value.is_empty() => {
break 'datetime String::from_utf8_lossy(&value[0]).into_owned()
}
_ => {}
};
}
let datetime = 'datetime: {
// First, try to read creation date from EXIF data
if let Ok(ref exif) = exif {
match exif.get_field(Tag::DateTimeOriginal, In::PRIMARY) {
Some(Field { value: Value::Ascii(value), ..}) if !value.is_empty() => {
break 'datetime String::from_utf8_lossy(&value[0]).into_owned()
}
_ => {}
};
}
// If that doesn't work, fall back to the file modification time
format!("{:?}", std::fs::metadata(&path).unwrap().modified().unwrap().duration_since(SystemTime::UNIX_EPOCH).unwrap())
};
// If that doesn't work, fall back to the file modification time
format!("{:?}", std::fs::metadata(&path).unwrap().modified().unwrap().duration_since(SystemTime::UNIX_EPOCH).unwrap())
};
let image_info = ImageInfo {
width: width,
height: height,
name: path.file_name().expect("invalid file path").to_string_lossy().to_string(),
};
let image_info = ImageInfo {
width,
height,
name: path.file_name().expect("invalid file path").to_string_lossy().to_string(),
};
files.push(image_info);
}
files.push(image_info);
}
}
files