Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: delete exif from a file or a whole dir #2

Merged
merged 1 commit into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Usage: rsexif <COMMAND>
Commands:
file, -f, --file Extract exif from a single file
dir, -d, --dir Extract exif from every files in a directory
rm, -r, --remove Remove exifs
help Print this message or the help of the given subcommand(s)

Options:
Expand Down Expand Up @@ -40,6 +41,17 @@ Options:
-h, --help Print help
```

### Mode Remove
```
Usage: rsexif {rm|--remove|-r} <path>

Arguments:
<path> file to remove exifs from

Options:
-h, --help Print help
```

## To-do
- [x] Read exif data
- [x] Write exif data in a json file (for one or multiple files)
Expand Down
73 changes: 73 additions & 0 deletions src/core/extraction.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
use crate::utils;
use std::collections::{HashMap, BTreeMap};
use crate::models::Image;
use std::fs;
use std::path::PathBuf;
use rayon::prelude::*;


pub fn from_file(path: String) -> HashMap<String, BTreeMap<String,String>> {

let metadata = match rexiv2::Metadata::new_from_path(path) {
Ok(m) => m,
Err(e) => {
println!("[*] Error while reading the exif from a file: {}", e);
return HashMap::new();
}
};

let tags = match metadata.get_exif_tags() {
Ok(t) => t,
Err(e) => {
println!("[*] Error while retreving the exif: {}", e);
return HashMap::new();
}
};

let map_data = tags.iter()
.map(|tag| {
let value = match metadata.get_tag_interpreted_string(tag) {
Ok(val) => val,
Err(_) => String::from("Failed to convert to string"),
};

// Exifs tags are like: Exif.Categ.TheTag
let parts: Vec<&str> = tag.split('.').collect();
if parts.len() >= 3 {
let category = parts[1].to_string();
let tag_name = parts[2..].join(".");
(category, tag_name, value)
} else {
let category = "Unknown".to_string();
let tag_name = parts[parts.len() -1].to_string();
(category, tag_name, value)
}
})
// We want the exifs to be in categories so we make a map of map
.fold(HashMap::new(), |mut acc: HashMap<String, BTreeMap<String, String>>, (category, tag_name, value)| {
// Use a BTreeMap to keep the elements sorted (better readability)
acc.entry(category)
.or_default()
.insert(tag_name, value);
acc
});

utils::add_google_map(map_data)
}

pub fn from_dir(_path: PathBuf) -> Vec<Image> {
let files = fs::read_dir(_path).expect("Couldn't read the directory given");

files.par_bridge()
.filter_map(|f| f.ok())
.filter(|f| !f.path().ends_with(".DS_Store") && !f.path().ends_with("/"))
.map(|f| {
let entry_path = f.path().display().to_string();
Image{
name: entry_path.clone(),
exifs: from_file(entry_path)
}
}).collect::<Vec<Image>>()
}


72 changes: 2 additions & 70 deletions src/core/mod.rs
Original file line number Diff line number Diff line change
@@ -1,71 +1,3 @@
use crate::utils;
use std::collections::{HashMap, BTreeMap};
use crate::models::Image;
use std::fs;
use std::path::PathBuf;
use rayon::prelude::*;

pub fn from_file(path: String) -> HashMap<String, BTreeMap<String,String>> {

let metadata = match rexiv2::Metadata::new_from_path(path) {
Ok(m) => m,
Err(e) => {
println!("[*] Error while reading the exif from a file: {}", e);
return HashMap::new();
}
};

let tags = match metadata.get_exif_tags() {
Ok(t) => t,
Err(e) => {
println!("[*] Error while retreving the exif: {}", e);
return HashMap::new();
}
};

let map_data = tags.iter()
.map(|tag| {
let value = match metadata.get_tag_interpreted_string(tag) {
Ok(val) => val,
Err(_) => String::from("Failed to convert to string"),
};

// Exifs tags are like: Exif.Categ.TheTag
let parts: Vec<&str> = tag.split('.').collect();
if parts.len() >= 3 {
let category = parts[1].to_string();
let tag_name = parts[2..].join(".");
(category, tag_name, value)
} else {
let category = "Unknown".to_string();
let tag_name = parts[parts.len() -1].to_string();
(category, tag_name, value)
}
})
// We want the exifs to be in categories so we make a map of map
.fold(HashMap::new(), |mut acc: HashMap<String, BTreeMap<String, String>>, (category, tag_name, value)| {
// Use a BTreeMap to keep the elements sorted (better readability)
acc.entry(category)
.or_default()
.insert(tag_name, value);
acc
});

utils::add_google_map(map_data)
}

pub fn from_dir(_path: PathBuf) -> Vec<Image> {
let files = fs::read_dir(_path).expect("Couldn't read the directory given");

files.par_bridge()
.filter_map(|f| f.ok())
.filter(|f| !f.path().ends_with(".DS_Store") && !f.path().ends_with("/"))
.map(|f| {
let entry_path = f.path().display().to_string();
Image{
name: entry_path.clone(),
exifs: from_file(entry_path)
}
}).collect::<Vec<Image>>()
}
pub mod extraction;

pub use extraction::*;
23 changes: 12 additions & 11 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use clap::{Parser, Subcommand};
use std::println;
use anyhow::{Result, anyhow, Error};
use anyhow::{Result, Error};

extern crate rsexif;
use rsexif::modules;
Expand Down Expand Up @@ -40,14 +39,20 @@ enum Commands {

#[arg(value_name = "export", required_unless_present = "split", short = 'e', long = "export", help = "The name of the Json file containing all the exifs")]
export_folder: Option<String>,
},

#[command(arg_required_else_help = true, long_flag = "remove", short_flag = 'r', about = "Remove exifs")]
Rm {
#[arg(value_name = "path", required = true, help = "file to remove exifs from")]
path: String
}
}


fn main() -> Result<(), Error> {
let args = Args::parse();

let status = match &args.command {
match &args.command {
Commands::File { file, export, json } => {
let m_file = file.as_str();
let export = export.to_owned();
Expand All @@ -57,14 +62,10 @@ fn main() -> Result<(), Error> {

Commands::Dir { dir, split, export_folder } => {
modules::dir_module(dir, *split, export_folder)
}
};
},

if let Err(e) = status {
println!("Error while extracing exifs");
Err(anyhow!(e))
} else {
Ok(())
Commands::Rm { path } => {
modules::remove_module(path)
}
}
}

2 changes: 2 additions & 0 deletions src/modules/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
pub mod file;
pub mod dir;
pub mod remove;


pub use file::*;
pub use dir::*;
pub use remove::*;
34 changes: 34 additions & 0 deletions src/modules/remove.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use anyhow::{Error, Result};
use std::fs::{metadata, read_dir};
use rayon::prelude::*;

pub fn remove_module(path: &String) -> Result<(), Error> {
if metadata(path)?.is_dir() {
let paths = read_dir(path)?;
let _ = paths
.par_bridge() // Parallelize the read_dir entries
.filter_map(|entry| {
entry.ok().and_then(|e| {
e.path().to_str().map(|s| s.to_string()) // Convert path to String
})
.map(|item| {
if let Err(e) = remove_exifs(&item) {
println!("[*] Error removing exifs from {}: {}", item, e);
}
}) // Apply remove_exifs if path is valid
}).collect::<Vec<_>>();

Ok(())

} else {
remove_exifs(path)
}
}

fn remove_exifs(path: &String) -> Result<(), Error> {
let meta = rexiv2::Metadata::new_from_path(path.clone())?;
meta.clear();
meta.save_to_file(path)?;
Ok(())
}