39 lines
1.1 KiB
Rust
39 lines
1.1 KiB
Rust
use anyhow::{Error, Result};
|
|
use img_parts::{jpeg::Jpeg, Bytes, ImageEXIF};
|
|
use serde::{Deserialize, Serialize};
|
|
use sp_backend::bitcoin::secp256k1::SecretKey;
|
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
pub struct BackUpImage(Vec<u8>);
|
|
|
|
impl BackUpImage {
|
|
pub fn new_recover(image: Vec<u8>, data: &[u8]) -> Result<Self> {
|
|
// TODO: sanity check on data
|
|
let img = write_exif(image, data)?;
|
|
Ok(Self(img))
|
|
}
|
|
|
|
pub fn new_revoke(image: Vec<u8>, data: &[u8]) -> Result<Self> {
|
|
// TODO: sanity check on data
|
|
let img = write_exif(image, data)?;
|
|
Ok(Self(img))
|
|
}
|
|
|
|
pub fn to_inner(&self) -> Vec<u8> {
|
|
self.0.clone()
|
|
}
|
|
|
|
pub fn as_inner(&self) -> &[u8] {
|
|
&self.0
|
|
}
|
|
}
|
|
|
|
fn write_exif(image: Vec<u8>, data: &[u8]) -> Result<Vec<u8>> {
|
|
let mut jpeg = Jpeg::from_bytes(Bytes::from(image))?;
|
|
let data_bytes = Bytes::from(data.to_owned());
|
|
jpeg.set_exif(Some(data_bytes));
|
|
let output_image_bytes = jpeg.encoder().bytes();
|
|
let output_image = output_image_bytes.to_vec();
|
|
Ok(output_image)
|
|
}
|