Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

feat(rust): Add metadata export matching Go CLI format

Manav Rathi0f5e30e96b88
diff --git a/rust/src/commands/export.rs b/rust/src/commands/export.rs
index b4e6daddcf..58472113ae 100644
--- a/rust/src/commands/export.rs
+++ b/rust/src/commands/export.rs
@@ -2,7 +2,12 @@ use crate::Result;
use crate::api::client::ApiClient;
use crate::api::methods::ApiMethods;
use crate::crypto::{decrypt_file_data, decrypt_stream, init as crypto_init, secret_box_open};
-use crate::models::{account::Account, filter::ExportFilter, metadata::FileMetadata};
+use crate::models::{
+ account::Account,
+ export_metadata::{AlbumMetadata, DiskFileMetadata},
+ filter::ExportFilter,
+ metadata::FileMetadata,
+};
use crate::storage::Storage;
use crate::sync::SyncEngine;
use base64::Engine;
@@ -124,6 +129,12 @@ async fn export_account(storage: &Storage, account: &Account, filter: &ExportFil
// Track exported hashes for deduplication within this export session
let mut exported_hashes: HashMap<String, PathBuf> = HashMap::new();
+ // Track albums that have metadata written
+ let mut albums_with_metadata: HashMap<String, bool> = HashMap::new();
+
+ // Track file indices per album for unique metadata filenames
+ let mut album_file_indices: HashMap<String, usize> = HashMap::new();
+
// Get stored secrets
let secrets = storage
.accounts()
@@ -393,6 +404,42 @@ async fn export_account(storage: &Storage, account: &Account, filter: &ExportFil
exported_hashes.insert(hash.clone(), file_path.clone());
}
+ // Determine album folder for metadata
+ let album_folder = if let Some(ref name) = collection.name
+ && !name.is_empty()
+ {
+ sanitize_album_name(name)
+ } else {
+ "Uncategorized".to_string()
+ };
+
+ // Write album metadata if not already written for this album
+ if !albums_with_metadata.contains_key(&album_folder) {
+ write_album_metadata(export_path, &album_folder, collection, account.user_id)
+ .await?;
+ albums_with_metadata.insert(album_folder.clone(), true);
+ }
+
+ // Get and increment file index for this album
+ let file_index = album_file_indices.entry(album_folder.clone()).or_insert(0);
+
+ // Write file metadata
+ let filename = file_path
+ .file_name()
+ .and_then(|n| n.to_str())
+ .unwrap_or("file");
+ write_file_metadata(
+ export_path,
+ &album_folder,
+ &file,
+ metadata.as_ref(),
+ filename,
+ *file_index,
+ )
+ .await?;
+
+ *file_index += 1;
+
// Progress indicator - show every file for now since we have few files
println!(
" [{}] Exported: {}",
@@ -664,3 +711,65 @@ async fn extract_live_photo(zip_data: &[u8], output_path: &Path) -> Result<()> {
Ok(())
}
+
+/// Write album metadata to .meta/album_meta.json
+async fn write_album_metadata(
+ export_path: &Path,
+ album_folder: &str,
+ collection: &crate::api::models::Collection,
+ account_id: i64,
+) -> Result<()> {
+ let meta_dir = export_path.join(album_folder).join(".meta");
+ fs::create_dir_all(&meta_dir).await?;
+
+ let album_meta = AlbumMetadata::new(
+ collection.id,
+ collection.owner.id,
+ collection
+ .name
+ .clone()
+ .unwrap_or_else(|| "Unnamed".to_string()),
+ account_id,
+ );
+
+ let meta_path = meta_dir.join("album_meta.json");
+ let json = serde_json::to_string_pretty(&album_meta)?;
+ fs::write(meta_path, json).await?;
+
+ Ok(())
+}
+
+/// Write file metadata to .meta folder
+async fn write_file_metadata(
+ export_path: &Path,
+ album_folder: &str,
+ file: &crate::api::models::File,
+ metadata: Option<&FileMetadata>,
+ filename: &str,
+ file_index: usize,
+) -> Result<()> {
+ let meta_dir = export_path.join(album_folder).join(".meta");
+ fs::create_dir_all(&meta_dir).await?;
+
+ // Generate unique metadata filename
+ let base_name = Path::new(filename)
+ .file_stem()
+ .and_then(|s| s.to_str())
+ .unwrap_or("file");
+ let extension = Path::new(filename)
+ .extension()
+ .and_then(|s| s.to_str())
+ .unwrap_or("bin");
+
+ // Create metadata filename like "IMG_1234.jpg_0.json"
+ let meta_filename = format!("{}_{}.{}.json", base_name, file_index, extension);
+
+ let mut disk_metadata = DiskFileMetadata::from_file(file, metadata, filename.to_string());
+ disk_metadata.meta_file_name = meta_filename.clone();
+
+ let meta_path = meta_dir.join(meta_filename);
+ let json = serde_json::to_string_pretty(&disk_metadata)?;
+ fs::write(meta_path, json).await?;
+
+ Ok(())
+}
diff --git a/rust/src/models/export_metadata.rs b/rust/src/models/export_metadata.rs
new file mode 100644
index 0000000000..b01dd9251c
--- /dev/null
+++ b/rust/src/models/export_metadata.rs
@@ -0,0 +1,129 @@
+use serde::{Deserialize, Serialize};
+
+/// Album metadata matching Go's export.AlbumMetadata structure
+#[derive(Debug, Clone, Serialize, Deserialize)]
+#[serde(rename_all = "camelCase")]
+pub struct AlbumMetadata {
+ pub id: i64,
+ pub owner_id: i64,
+ pub album_name: String,
+ pub is_deleted: bool,
+ /// Account IDs that own this album (for shared export directories)
+ pub account_owner_ids: Vec<i64>,
+ /// Folder name on disk (excluded from JSON)
+ #[serde(skip)]
+ pub folder_name: String,
+}
+
+impl AlbumMetadata {
+ pub fn new(id: i64, owner_id: i64, album_name: String, account_id: i64) -> Self {
+ Self {
+ id,
+ owner_id,
+ album_name,
+ is_deleted: false,
+ account_owner_ids: vec![account_id],
+ folder_name: String::new(),
+ }
+ }
+}
+
+/// File metadata matching Go's export.DiskFileMetadata structure
+#[derive(Debug, Clone, Serialize, Deserialize)]
+#[serde(rename_all = "camelCase")]
+pub struct DiskFileMetadata {
+ pub title: String,
+ #[serde(skip_serializing_if = "Option::is_none")]
+ pub description: Option<String>,
+ #[serde(skip_serializing_if = "Option::is_none")]
+ pub location: Option<Location>,
+ pub creation_time: i64, // Unix timestamp in microseconds
+ pub modification_time: i64, // Unix timestamp in microseconds
+ pub info: FileInfo,
+ /// Meta filename on disk (excluded from JSON)
+ #[serde(skip)]
+ pub meta_file_name: String,
+}
+
+/// File info matching Go's export.Info structure
+#[derive(Debug, Clone, Serialize, Deserialize)]
+#[serde(rename_all = "camelCase")]
+pub struct FileInfo {
+ pub id: i64,
+ #[serde(skip_serializing_if = "Option::is_none")]
+ pub hash: Option<String>,
+ pub owner_id: i64,
+ /// Multiple filenames for live photos or burst photos
+ pub file_names: Vec<String>,
+}
+
+/// Location metadata
+#[derive(Debug, Clone, Serialize, Deserialize)]
+#[serde(rename_all = "camelCase")]
+pub struct Location {
+ pub latitude: f64,
+ pub longitude: f64,
+}
+
+impl DiskFileMetadata {
+ pub fn from_file(
+ file: &crate::api::models::File,
+ metadata: Option<&crate::models::metadata::FileMetadata>,
+ filename: String,
+ ) -> Self {
+ // Extract metadata values
+ let (title, location, creation_time) = if let Some(meta) = metadata {
+ let title = meta.get_title().unwrap_or(&filename).to_string();
+ let location = meta.latitude.and_then(|lat| {
+ meta.longitude.map(|lon| Location {
+ latitude: lat,
+ longitude: lon,
+ })
+ });
+
+ // Use creation time from metadata if available
+ let creation_time = meta.creation_time.unwrap_or(file.updation_time);
+
+ (title, location, creation_time)
+ } else {
+ let title = filename.clone();
+ let creation_time = file.updation_time;
+ (title, None, creation_time)
+ };
+
+ // Use modification time from metadata or fall back to updation time
+ let modification_time = metadata
+ .and_then(|m| m.modification_time)
+ .unwrap_or(file.updation_time);
+
+ // Get hash from metadata
+ let hash = metadata.and_then(|m| {
+ m.hash
+ .as_ref()
+ .or(m.image_hash.as_ref())
+ .or(m.video_hash.as_ref())
+ .cloned()
+ });
+
+ Self {
+ title,
+ description: None, // FileMetadata doesn't have description field
+ location,
+ creation_time,
+ modification_time,
+ info: FileInfo {
+ id: file.id,
+ hash,
+ owner_id: file.owner_id,
+ file_names: vec![filename],
+ },
+ meta_file_name: String::new(),
+ }
+ }
+
+ pub fn add_file_name(&mut self, filename: String) {
+ if !self.info.file_names.contains(&filename) {
+ self.info.file_names.push(filename);
+ }
+ }
+}
diff --git a/rust/src/models/mod.rs b/rust/src/models/mod.rs
index 2004632b95..ff504429f1 100644
--- a/rust/src/models/mod.rs
+++ b/rust/src/models/mod.rs
@@ -1,6 +1,7 @@
pub mod account;
pub mod collection;
pub mod error;
+pub mod export_metadata;
pub mod file;
pub mod filter;
pub mod metadata;