Fix some clippy lints
This commit is contained in:
parent
0af84a1ce8
commit
02dcb9d5d0
81
src/main.rs
81
src/main.rs
@ -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,41 +241,39 @@ 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);
|
|
||||||
|
|
||||||
let exif = read_exif_data(&mut file);
|
let exif = read_exif_data(&mut file);
|
||||||
|
|
||||||
// Check if we need to flip the coordinates
|
// Check if we need to flip the coordinates
|
||||||
let (width, height) = read_image_size(&mut file, exif.as_ref().ok()).unwrap();
|
let (width, height) = read_image_size(&mut file, exif.as_ref().ok()).unwrap();
|
||||||
|
|
||||||
let datetime = 'datetime: {
|
let datetime = 'datetime: {
|
||||||
// First, try to read creation date from EXIF data
|
// First, try to read creation date from EXIF data
|
||||||
if let Ok(ref exif) = exif {
|
if let Ok(ref exif) = exif {
|
||||||
match exif.get_field(Tag::DateTimeOriginal, In::PRIMARY) {
|
match exif.get_field(Tag::DateTimeOriginal, In::PRIMARY) {
|
||||||
Some(Field { value: Value::Ascii(value), ..}) if !value.is_empty() => {
|
Some(Field { value: Value::Ascii(value), ..}) if !value.is_empty() => {
|
||||||
break 'datetime String::from_utf8_lossy(&value[0]).into_owned()
|
break 'datetime String::from_utf8_lossy(&value[0]).into_owned()
|
||||||
}
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
// If that doesn't work, fall back to the file modification time
|
// 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())
|
format!("{:?}", std::fs::metadata(&path).unwrap().modified().unwrap().duration_since(SystemTime::UNIX_EPOCH).unwrap())
|
||||||
};
|
};
|
||||||
|
|
||||||
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
|
||||||
|
Loading…
Reference in New Issue
Block a user