diff --git a/Cargo.lock b/Cargo.lock index a135070..c33124e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -546,6 +546,7 @@ dependencies = [ "encoding_rs", "futures", "image", + "kamadak-exif", "mailchecker", "mysql", "percent-encoding", @@ -1111,6 +1112,15 @@ dependencies = [ "rayon", ] +[[package]] +name = "kamadak-exif" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6a5e66d5b5469321038611f7f0e845a48989e4fd54987b6e5bb4c8ae3adbace7" +dependencies = [ + "mutate_once", +] + [[package]] name = "kernel32-sys" version = "0.2.2" @@ -1323,6 +1333,12 @@ dependencies = [ "ws2_32-sys", ] +[[package]] +name = "mutate_once" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "16cf681a23b4d0a43fc35024c176437f9dcd818db34e0f42ab456a0ee5ad497b" + [[package]] name = "mysql" version = "18.2.0" diff --git a/Cargo.toml b/Cargo.toml index 68a02ee..ab2ee6a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,4 +22,5 @@ sha1 = "0.6.0" rand = "0.7.3" chrono = "0.4.11" bytes = "0.5.4" -image = "0.23.5" \ No newline at end of file +image = "0.23.5" +kamadak-exif = "0.5.1" \ No newline at end of file diff --git a/src/data/http_request_handler.rs b/src/data/http_request_handler.rs index a181a8b..4bf8e60 100644 --- a/src/data/http_request_handler.rs +++ b/src/data/http_request_handler.rs @@ -5,6 +5,7 @@ use std::str::FromStr; use actix_web::{HttpRequest, HttpResponse, web}; use actix_web::http::{HeaderName, HeaderValue}; use bytes::Bytes; +use exif::In; use image::{GenericImageView, ImageFormat}; use serde::Serialize; @@ -287,6 +288,39 @@ impl HttpRequestHandler { image = image.resize(max_w, max_h, image::imageops::FilterType::Nearest); } + // Read EXIF information in case of JPEG image, if possible + if let Ok(ImageFormat::Jpeg) = image::guess_format(file.buff.as_ref()) { + let mut reader = std::io::BufReader::new(file.buff.as_ref()); + + if let Ok(exif_attr) = exif::get_exif_attr_from_jpeg(&mut reader) { + let exif_reader = exif::Reader::new(); + let exif = exif_reader.read_raw(exif_attr)?; + + if let Some(v) = exif.get_field(exif::Tag::Orientation, In::PRIMARY) { + match v.value.get_uint(0) { + Some(1) => { /* row 0 is top and column 0 is left */ } + //Some(2) => println!("row 0 at top and column 0 at right"), + Some(3) => { + /* row 0 at bottom and column 0 at right */ + image = image.rotate180() + } + //Some(4) => println!("row 0 at bottom and column 0 at left"), + //Some(5) => println!("row 0 at left and column 0 at top"), + Some(6) => { + /* row 0 is right and column 0 is top */ + image = image.rotate90(); + } + //Some(7) => println!("row 0 at right and column 0 at bottom"), + Some(8) => { + /* row 0 is left and column 0 is bottom */ + image = image.rotate270(); + } + v => println!("Unhandled EXIF Orientation: {:?}", v), + }; + } + } + } + // Determine image destination let target_user_data_folder = prepare_file_creation(self.user_id()?, folder)?;