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

Retrieve Metadata As Bytes #72

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
22 changes: 21 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,11 @@ extern crate gexiv2_sys as gexiv2;
pub use gexiv2::GExiv2LogLevel as LogLevel;

use std::ffi;
use std::ffi::c_void;
use std::io::Cursor;
use std::ptr;
use std::str;
use gexiv2::ManagedStreamCallbacks;

/// A wrapper type for the kinds of errors one might encounter when using the library.
#[derive(Debug, PartialEq, Eq)]
Expand Down Expand Up @@ -106,7 +109,7 @@ pub type Result<T> = std::result::Result<T, Rexiv2Error>;
/// An opaque structure that serves as a container for a media file's metadata.
#[derive(Debug, PartialEq, Eq)]
pub struct Metadata {
raw: *mut gexiv2::GExiv2Metadata,
pub raw: *mut gexiv2::GExiv2Metadata,
}

/// An opaque structure that serves as a container for a preview image.
Expand Down Expand Up @@ -372,6 +375,23 @@ impl Metadata {
}
}

/// Put metadata in a buffer by constructing a stream, writing to it, and then reading the result
pub fn save_to_buffer(&self, image_data: &[u8]) -> Result<Vec<u8>> {
unsafe {
let mut cursor = Cursor::new(Vec::from(image_data));
let mut callbacks = ManagedStreamCallbacks::new(&mut cursor as *mut Cursor<Vec<u8>> as *mut c_void);
let mut err: *mut gexiv2::GError = ptr::null_mut();
let result = gexiv2::gexiv2_metadata_save_stream(self.raw, &mut callbacks as *mut ManagedStreamCallbacks, &mut err);
if result != 1 {
let err_msg = ffi::CStr::from_ptr((*err).message).to_str();
return Err(Rexiv2Error::Internal(
err_msg.ok().map(|msg| msg.to_string()),
));
}

Ok(cursor.into_inner())
}
}

// Image information.

Expand Down
15 changes: 15 additions & 0 deletions tst/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,21 @@ fn log_levels() {
assert_eq!(rexiv2::get_log_level(), rexiv2::LogLevel::INFO);
}

#[test]
fn test_simple_save_to_buffer() {
test_setup();
let data = include_bytes!("sample.png");
let meta = rexiv2::Metadata::new_from_buffer(data).unwrap();
let result = meta.save_to_buffer(data);
assert!(result.is_ok());
let unwrapped_result = result.unwrap();
assert!(unwrapped_result.len() > 0);
assert_eq!(unwrapped_result.len(), data.len());

// these should be equal since we didn't adjust anything
assert!(unwrapped_result == data);
}

#[test]
#[cfg(feature = "raw-tag-access")]
fn get_tag_raw() {
Expand Down