Skip to content

Commit

Permalink
Merge branch 'v3/params'
Browse files Browse the repository at this point in the history
  • Loading branch information
i5-650 committed Jan 25, 2023
1 parent 2e51635 commit 99c7ebe
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 103 deletions.
4 changes: 4 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"workbench.preferredDarkColorTheme": "Atom One Dark",
"css.format.spaceAroundSelectorSeparator": false
}
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "rusty-exif"
version = "0.1.1"
version = "0.2.1"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
Binary file removed froggy.png
Binary file not shown.
14 changes: 4 additions & 10 deletions src/exif_mapper/data_structures.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
use serde::{Serialize, Deserialize};
use std::{collections::HashMap};
pub use serde::{Serialize, Deserialize};
use std::collections::HashMap;

#[derive(Serialize, Deserialize)]
pub struct Image {
pub image_name: String,
#[serde(flatten)]
pub exif_fields: HashMap<String, String>
}
pub struct Image { pub name: String, pub exifs: HashMap<String, String> }

#[derive(Serialize, Deserialize)]
pub struct Data {
pub images: Vec<Image>
}
pub struct Data(pub Vec<Image>);
93 changes: 47 additions & 46 deletions src/exif_mapper/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,65 +4,66 @@ use sscanf::scanf;

mod data_structures;

pub fn map_exif(_path_to_image: PathBuf) -> HashMap<String, String> {
let mut map_data = HashMap::new();
pub fn map_exif_from_file(_path_to_image: PathBuf) -> HashMap<String, String> {
let mut map_data = HashMap::new();

if let Ok(file) = std::fs::File::open(_path_to_image.as_path()) {
let mut bufreader = std::io::BufReader::new(&file);
let exifreader = exif::Reader::new();
if let Ok(file) = std::fs::File::open(_path_to_image.as_path()) {
let mut bufreader = std::io::BufReader::new(&file);
let exifreader = exif::Reader::new();

if let Ok(exif) = exifreader.read_from_container(&mut bufreader) {
for f in exif.fields() {
let mut value = f.display_value().to_string();
if value.starts_with("\"") && value.ends_with("\"") {
value = value.strip_prefix("\"").unwrap().strip_suffix("\"").unwrap().to_string();
}
map_data.insert(f.tag.to_string(), value);
}
if let Ok(exif) = exifreader.read_from_container(&mut bufreader) {
for f in exif.fields() {
let mut value = f.display_value().to_string();
if value.starts_with("\"") && value.ends_with("\"") {
value = value.strip_prefix("\"").unwrap().strip_suffix("\"").unwrap().to_string();
}
map_data.insert(f.tag.to_string(), value);
}

if map_data.contains_key("GPSLatitude") && map_data.contains_key("GPSLongitude") {
map_data.insert(
"googleMap".to_string(),
format!("https://www.google.com/maps/search/?api=1&query={},{}",
convert_dms_to_decimal(map_data.get("GPSLatitude").unwrap()),
convert_dms_to_decimal(map_data.get("GPSLongitude").unwrap())
)
);
}
}
}
return map_data;
if map_data.contains_key("GPSLatitude") && map_data.contains_key("GPSLongitude") {
map_data.insert(
"googleMap".to_string(),
format!("https://www.google.com/maps/search/?api=1&query={},{}",
convert_dms_to_decimal(map_data.get("GPSLatitude").unwrap()),
convert_dms_to_decimal(map_data.get("GPSLongitude").unwrap())
)
);
}
}
}
return map_data;
}

pub fn convert_dms_to_decimal(dms: &String) -> f64 {
let parsed = scanf!(dms, "{} deg {} min {} sec", f64, f64, f64).unwrap();
let degrees = parsed.0;
let minutes = parsed.1;
let seconds = parsed.2;
return degrees + minutes / 60.0 + seconds / 3600.0;
let parsed = scanf!(dms, "{} deg {} min {} sec", f64, f64, f64).unwrap();
let degrees = parsed.0;
let minutes = parsed.1;
let seconds = parsed.2;
return degrees + minutes / 60.0 + seconds / 3600.0;
}

pub fn json_string_from_dir(_path: PathBuf) -> data_structures::Data {
let files_list = get_file_list(_path);
let mut data_out = data_structures::Data { images: vec![] };
pub fn map_exif_from_folder(_path: PathBuf) -> data_structures::Data {
let files_list = get_file_list(_path);
let mut data_out = data_structures::Data(vec![]);

for pathbuf_file in files_list {
data_out.images.push(data_structures::Image {
image_name: pathbuf_file.as_path().display().to_string(),
exif_fields: map_exif(pathbuf_file)});
}
return data_out;
for pathbuf_file in files_list {
data_out.0.push(data_structures::Image {
name: pathbuf_file.as_path().display().to_string(),
exifs: map_exif_from_file(pathbuf_file) }
);
}
return data_out;
}


fn get_file_list(_path: PathBuf) -> Vec<PathBuf>{
let mut file_list = vec![];
let mut file_list = vec![];

for file in fs::read_dir(_path).unwrap() {
file_list.push(
file.unwrap().path()
);
}
for file in fs::read_dir(_path).unwrap() {
file_list.push(
file.unwrap().path()
);
}

return file_list;
return file_list;
}
88 changes: 42 additions & 46 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,59 +15,55 @@ pub mod exif_mapper;
#[derive(Parser)]
#[command(author, version, about, long_about = None)]
#[command(group(
ArgGroup::new("incompatibles")
.required(false)
.args(["file", "folder"]),
ArgGroup::new("incompatibles")
.required(false)
.args(["file", "folder"])
))]
struct Args {
#[arg(short, long)]
export: Option<String>,
#[arg(short= 'f', long)]
file: Option<String>,
#[arg(short = 'F', long)]
folder: Option<String>
#[arg(short, long)]
export: Option<String>,
file: Option<PathBuf>,
#[arg(short = 'f', long)]
folder: Option<String>
}

fn main() {
let args = Args::parse();

if args.file.is_some() && args.export.is_some() {
let mut filename = args.export.unwrap();
if !filename.ends_with(".json") {
filename.push_str(".json");
}
let predic = File::create(filename);
let args = Args::parse();

if predic.is_ok() {
let mut output_file = predic.unwrap();
let data_to_jsonify = exif_mapper::map_exif(PathBuf::from(args.file.unwrap()));
let output = serde_json::to_string_pretty(&data_to_jsonify);
if !output_file.write(output.unwrap().as_bytes()).is_ok() {
println!("Couln't save file...");
}
}
}
else if args.file.is_some() {
for (key, value) in exif_mapper::map_exif( PathBuf::from(args.file.unwrap())) {
println!("{}: {}", key, value);
}
}
else if args.export.is_some() && args.folder.is_some() {
let mut filename = args.export.unwrap();
if !filename.ends_with(".json") {
filename.push_str(".json");
}
let predic = File::create(filename);
if args.file.is_some() && args.export.is_some() {
let predic = check_extension(&mut args.export.unwrap());
let mut output_file = predic.unwrap();
let data_to_jsonify = exif_mapper::map_exif_from_file(PathBuf::from(args.file.unwrap()));
let output = serde_json::to_string_pretty(&data_to_jsonify);
if !output_file.write(output.unwrap().as_bytes()).is_ok() {
println!("Couln't save file...");
}
}
else if args.file.is_some() {
for (key, value) in exif_mapper::map_exif_from_file( PathBuf::from(args.file.unwrap())) {
println!("{}: {}", key, value);
}
}
else if args.export.is_some() && args.folder.is_some() {
let predic = check_extension(&mut args.export.unwrap());

if predic.is_ok() {
let mut output_file = predic.unwrap();
let data_to_jsonify = exif_mapper::json_string_from_dir(PathBuf::from(args.folder.unwrap()));
let output = serde_json::to_string_pretty(&data_to_jsonify);
if !output_file.write(output.unwrap().as_bytes()).is_ok() {
println!("Couln't save file...");
}
}
}
let mut output_file = predic.unwrap();
let data_to_jsonify = exif_mapper::map_exif_from_folder(PathBuf::from(args.folder.unwrap()));
let output = serde_json::to_string_pretty(&data_to_jsonify);
if !output_file.write(output.unwrap().as_bytes()).is_ok() {
println!("Couln't save file...");
}

}
else {
println!("No file or folder specified, use -h for help");
}
}

fn check_extension(filename: &mut String) -> Result<File, std::io::Error> {
if !filename.ends_with(".json") {
filename.push_str(".json");
}
return File::create(filename);
}

0 comments on commit 99c7ebe

Please sign in to comment.