|
| 1 | +use crate::cli::{ImageAction, ImageArgs}; |
| 2 | +use colored::*; |
| 3 | +use exif::{Field, In, Tag}; |
| 4 | +use std::fs::File; |
| 5 | +use std::io::BufReader; |
| 6 | + |
| 7 | +pub fn handle(args: ImageArgs) { |
| 8 | + match args.action { |
| 9 | + ImageAction::Extract { path, all } => { |
| 10 | + if let Err(e) = extract_image_metadata(&path, all) { |
| 11 | + eprintln!("{}: {}", "Error".red(), e); |
| 12 | + } |
| 13 | + } |
| 14 | + } |
| 15 | +} |
| 16 | + |
| 17 | +fn extract_image_metadata(path: &str, show_all: bool) -> Result<(), Box<dyn std::error::Error>> { |
| 18 | + let file = File::open(path)?; |
| 19 | + let mut bufreader = BufReader::new(&file); |
| 20 | + let exifreader = exif::Reader::new(); |
| 21 | + let exif = match exifreader.read_from_container(&mut bufreader) { |
| 22 | + Ok(exif) => exif, |
| 23 | + Err(_) => { |
| 24 | + println!("{}: No EXIF data found in image", "Warning".yellow()); |
| 25 | + return Ok(()); |
| 26 | + } |
| 27 | + }; |
| 28 | + |
| 29 | + println!("{}", "Extracted metadata:".green().bold()); |
| 30 | + |
| 31 | + let interesting_tags = if show_all { |
| 32 | + // Show all tags if --all flag is set |
| 33 | + exif.fields().collect::<Vec<_>>() |
| 34 | + } else { |
| 35 | + // Only show common interesting tags |
| 36 | + exif.fields() |
| 37 | + .filter(|field| { |
| 38 | + matches!( |
| 39 | + field.tag, |
| 40 | + Tag::DateTime |
| 41 | + | Tag::DateTimeOriginal |
| 42 | + | Tag::DateTimeDigitized |
| 43 | + | Tag::GPSLatitude |
| 44 | + | Tag::GPSLongitude |
| 45 | + | Tag::GPSLatitudeRef |
| 46 | + | Tag::GPSLongitudeRef |
| 47 | + | Tag::Make |
| 48 | + | Tag::Model |
| 49 | + | Tag::Software |
| 50 | + | Tag::Artist |
| 51 | + | Tag::Copyright |
| 52 | + | Tag::ImageDescription |
| 53 | + ) |
| 54 | + }) |
| 55 | + .collect() |
| 56 | + }; |
| 57 | + |
| 58 | + for field in interesting_tags { |
| 59 | + let tag_name = format!("{:?}", field.tag); |
| 60 | + let value = match field.tag { |
| 61 | + Tag::GPSLatitude | Tag::GPSLongitude => { |
| 62 | + if let Ok(coord) = field.value.get_float(0) { |
| 63 | + format!("{}°", coord) |
| 64 | + } else { |
| 65 | + field.display_value().to_string() |
| 66 | + } |
| 67 | + } |
| 68 | + _ => field.display_value().to_string(), |
| 69 | + }; |
| 70 | + |
| 71 | + println!("{:20}: {}", tag_name.cyan(), value); |
| 72 | + } |
| 73 | + |
| 74 | + // Try to extract GPS coordinates if available |
| 75 | + if let (Some(lat), Some(lon)) = get_gps_coordinates(&exif) { |
| 76 | + println!(); |
| 77 | + println!("{}", "GPS Coordinates:".green().bold()); |
| 78 | + println!("Latitude: {}", lat.to_string().yellow()); |
| 79 | + println!("Longitude: {}", lon.to_string().yellow()); |
| 80 | + println!(); |
| 81 | + println!( |
| 82 | + "{}: https://www.google.com/maps/place/{},{}", |
| 83 | + "View on map".blue(), |
| 84 | + lat, |
| 85 | + lon |
| 86 | + ); |
| 87 | + } |
| 88 | + |
| 89 | + Ok(()) |
| 90 | +} |
| 91 | + |
| 92 | +fn get_gps_coordinates(exif: &exif::Exif) -> (Option<f64>, Option<f64>) { |
| 93 | + let lat = get_gps_coordinate(exif, Tag::GPSLatitude, Tag::GPSLatitudeRef); |
| 94 | + let lon = get_gps_coordinate(exif, Tag::GPSLongitude, Tag::GPSLongitudeRef); |
| 95 | + (lat, lon) |
| 96 | +} |
| 97 | + |
| 98 | +fn get_gps_coordinate(exif: &exif::Exif, coord_tag: Tag, ref_tag: Tag) -> Option<f64> { |
| 99 | + let coord = exif.get_field(coord_tag, In::PRIMARY)?; |
| 100 | + let ref_val = exif.get_field(ref_tag, In::PRIMARY)?; |
| 101 | + |
| 102 | + let coord_vec = coord.value.as_rational()?.get(0..3)?; |
| 103 | + let degrees = coord_vec[0].to_f64(); |
| 104 | + let minutes = coord_vec[1].to_f64(); |
| 105 | + let seconds = coord_vec[2].to_f64(); |
| 106 | + |
| 107 | + let mut coord = degrees + minutes / 60.0 + seconds / 3600.0; |
| 108 | + |
| 109 | + if ref_val.display_value().to_string() == "S" || ref_val.display_value().to_string() == "W" { |
| 110 | + coord = -coord; |
| 111 | + } |
| 112 | + |
| 113 | + Some(coord) |
| 114 | +} |
0 commit comments