π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
feat(rust): Add retry logic and export filters
Manav Rathif55973367d16
diff --git a/rust/src/api/client.rs b/rust/src/api/client.rsindex e1bb454fba..66742bdd81 100644--- a/rust/src/api/client.rs+++ b/rust/src/api/client.rs@@ -1,5 +1,6 @@#![allow(dead_code)]+use crate::api::retry::RetryConfig;use crate::models::error::{Error, Result};use reqwest::{Client, RequestBuilder, Response, StatusCode};use serde::{Deserialize, Serialize};@@ -32,6 +33,8 @@ pub struct ApiClient {pub(crate) base_url: String,/// Token storage for multi-account support: account_id -> tokentokens: Arc<RwLock<HashMap<String, String>>>,+ /// Retry configuration+ retry_config: RetryConfig,}impl ApiClient {@@ -55,6 +58,7 @@ impl ApiClient {download_client,base_url: base_url.unwrap_or_else(|| ENTE_API_ENDPOINT.to_string()),tokens: Arc::new(RwLock::new(HashMap::new())),+ retry_config: RetryConfig::default(),})}@@ -76,6 +80,11 @@ impl ApiClient {tokens.get(account_id).cloned()}+ /// Set retry configuration+ pub fn set_retry_config(&mut self, config: RetryConfig) {+ self.retry_config = config;+ }+/// Build a request with common headersfn build_request(&self, builder: RequestBuilder, account_id: Option<&str>) -> RequestBuilder {let mut req = builder.header(CLIENT_PKG_HEADER, CLIENT_PACKAGE);diff --git a/rust/src/api/mod.rs b/rust/src/api/mod.rsindex 30ad1d8051..6026f0ae7f 100644--- a/rust/src/api/mod.rs+++ b/rust/src/api/mod.rs@@ -2,6 +2,7 @@ pub mod auth;pub mod client;pub mod methods;pub mod models;+pub mod retry;pub use auth::AuthClient;pub use client::ApiClient;diff --git a/rust/src/api/retry.rs b/rust/src/api/retry.rsnew file mode 100644index 0000000000..2a40add9ed--- /dev/null+++ b/rust/src/api/retry.rs@@ -0,0 +1,117 @@+use crate::Result;+use reqwest::{Response, StatusCode};+use std::time::Duration;+use tokio::time::sleep;++/// Retry configuration+pub struct RetryConfig {+ pub max_retries: u32,+ pub initial_delay: Duration,+ pub max_delay: Duration,+ pub exponential_base: f64,+}++impl Default for RetryConfig {+ fn default() -> Self {+ Self {+ max_retries: 3,+ initial_delay: Duration::from_millis(500),+ max_delay: Duration::from_secs(30),+ exponential_base: 2.0,+ }+ }+}++/// Execute a request with retry logic+pub async fn with_retry<F, Fut>(config: &RetryConfig, mut operation: F) -> Result<Response>+where+ F: FnMut() -> Fut,+ Fut: std::future::Future<Output = Result<Response>>,+{+ let mut attempt = 0;+ let mut delay = config.initial_delay;++ loop {+ match operation().await {+ Ok(response) => {+ // Check if we should retry based on status code+ let status = response.status();++ if status.is_success() {+ return Ok(response);+ }++ // Don't retry on client errors (except 429)+ if status.is_client_error() && status != StatusCode::TOO_MANY_REQUESTS {+ return Ok(response);+ }++ // Retry on 429 (rate limited) or 5xx errors+ if status == StatusCode::TOO_MANY_REQUESTS || status.is_server_error() {+ attempt += 1;++ if attempt > config.max_retries {+ log::warn!("Max retries ({}) exceeded for request", config.max_retries);+ return Ok(response);+ }++ // Check for Retry-After header on 429 responses+ if status == StatusCode::TOO_MANY_REQUESTS+ && let Some(retry_after) = response.headers().get("retry-after")+ && let Ok(retry_str) = retry_after.to_str()+ && let Ok(seconds) = retry_str.parse::<u64>()+ {+ delay = Duration::from_secs(seconds);+ log::info!("Rate limited, retrying after {} seconds", seconds);+ }++ log::info!(+ "Request failed with status {}, retrying in {:?} (attempt {}/{})",+ status,+ delay,+ attempt,+ config.max_retries+ );++ sleep(delay).await;++ // Calculate next delay with exponential backoff+ delay = Duration::from_secs_f64(+ (delay.as_secs_f64() * config.exponential_base)+ .min(config.max_delay.as_secs_f64()),+ );++ continue;+ }++ // For other status codes, don't retry+ return Ok(response);+ }+ Err(e) => {+ // Network errors should be retried+ attempt += 1;++ if attempt > config.max_retries {+ log::error!("Max retries ({}) exceeded: {}", config.max_retries, e);+ return Err(e);+ }++ log::warn!(+ "Request failed: {}, retrying in {:?} (attempt {}/{})",+ e,+ delay,+ attempt,+ config.max_retries+ );++ sleep(delay).await;++ // Calculate next delay with exponential backoff+ delay = Duration::from_secs_f64(+ (delay.as_secs_f64() * config.exponential_base)+ .min(config.max_delay.as_secs_f64()),+ );+ }+ }+ }+}diff --git a/rust/src/commands/account.rs b/rust/src/commands/account.rsindex c00cbed3fa..4bea1c28db 100644--- a/rust/src/commands/account.rs+++ b/rust/src/commands/account.rs@@ -125,7 +125,7 @@ async fn add_account(// Check if we're in non-interactive mode (password provided via CLI)let is_non_interactive = password_arg.is_some();-+// Get password (from arg or prompt)let password = if let Some(password) = password_arg {passworddiff --git a/rust/src/commands/export.rs b/rust/src/commands/export.rsindex bacec5f2d9..3150fd83d8 100644--- a/rust/src/commands/export.rs+++ b/rust/src/commands/export.rs@@ -2,14 +2,14 @@ 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, metadata::FileMetadata};+use crate::models::{account::Account, filter::ExportFilter, metadata::FileMetadata};use crate::storage::Storage;use base64::Engine;use std::path::{Path, PathBuf};use tokio::fs;use tokio::io::AsyncWriteExt;-pub async fn run_export(account_email: Option<String>) -> Result<()> {+pub async fn run_export(account_email: Option<String>, filter: ExportFilter) -> Result<()> {// Initialize cryptocrypto_init()?;@@ -48,7 +48,7 @@ pub async fn run_export(account_email: Option<String>) -> Result<()> {for account in accounts {println!("\n=== Exporting account: {} ===", account.email);- if let Err(e) = export_account(&storage, &account).await {+ if let Err(e) = export_account(&storage, &account, &filter).await {log::error!("Failed to export account {}: {}", account.email, e);println!("β Export failed: {e}");} else {@@ -59,7 +59,7 @@ pub async fn run_export(account_email: Option<String>) -> Result<()> {Ok(())}-async fn export_account(storage: &Storage, account: &Account) -> Result<()> {+async fn export_account(storage: &Storage, account: &Account, filter: &ExportFilter) -> Result<()> {// Get export directorylet export_dir = account.export_dir@@ -111,7 +111,27 @@ async fn export_account(storage: &Storage, account: &Account) -> Result<()> {continue;}- println!("Processing collection: {}", collection.id);+ // Decrypt collection name to check filters+ let collection_name = if let Some(ref _encrypted_name) = collection.encrypted_name {+ // Decrypt collection name if needed for filtering+ // For now, we'll use the collection ID as a fallback+ // TODO: Implement proper name decryption+ format!("Collection {}", collection.id)+ } else {+ format!("Collection {}", collection.id)+ };++ // Apply collection filters+ // TODO: Determine if collection is shared or hidden from metadata+ let is_shared = false; // Need to check collection metadata+ let is_hidden = false; // Need to check collection metadata++ if !filter.should_include_collection(&collection_name, is_shared, is_hidden) {+ log::debug!("Skipping filtered collection: {}", collection_name);+ continue;+ }++ println!("Processing collection: {}", collection_name);// Decrypt collection keylet collection_key = match decrypt_collection_key(diff --git a/rust/src/main.rs b/rust/src/main.rsindex 731b1272bf..7bff121e87 100644--- a/rust/src/main.rs+++ b/rust/src/main.rs@@ -31,7 +31,16 @@ async fn main() -> Result<()> {commands::account::handle_account_command(account_cmd, &storage).await?;}Commands::Export(export_cmd) => {- commands::export::run_export(export_cmd.account).await?;+ use ente_rs::models::filter::ExportFilter;++ let filter = ExportFilter {+ include_shared: export_cmd.shared,+ include_hidden: export_cmd.hidden,+ albums: export_cmd.albums,+ emails: export_cmd.emails,+ };++ commands::export::run_export(export_cmd.account, filter).await?;}Commands::Sync(sync_cmd) => {commands::sync::run_sync(sync_cmd.account, sync_cmd.metadata_only, sync_cmd.full)diff --git a/rust/src/models/filter.rs b/rust/src/models/filter.rsnew file mode 100644index 0000000000..525bddaece--- /dev/null+++ b/rust/src/models/filter.rs@@ -0,0 +1,58 @@+/// Export filter options+#[derive(Debug, Clone, Default)]+pub struct ExportFilter {+ /// Include shared albums+ pub include_shared: bool,++ /// Include hidden albums+ pub include_hidden: bool,++ /// Specific album names to export (None means all)+ pub albums: Option<Vec<String>>,++ /// Specific user emails to export files shared with (None means all)+ pub emails: Option<Vec<String>>,+}++impl ExportFilter {+ /// Check if a collection should be included based on filters+ pub fn should_include_collection(+ &self,+ collection_name: &str,+ is_shared: bool,+ is_hidden: bool,+ ) -> bool {+ // Check shared filter+ if is_shared && !self.include_shared {+ return false;+ }++ // Check hidden filter+ if is_hidden && !self.include_hidden {+ return false;+ }++ // Check album name filter+ if let Some(ref albums) = self.albums+ && !albums.is_empty()+ && !albums.iter().any(|a| a == collection_name)+ {+ return false;+ }++ true+ }++ /// Check if a file should be included based on email filter+ pub fn should_include_file_by_owner(&self, owner_email: Option<&str>) -> bool {+ if let Some(ref emails) = self.emails+ && !emails.is_empty()+ {+ if let Some(email) = owner_email {+ return emails.iter().any(|e| e == email);+ }+ return false;+ }+ true+ }+}diff --git a/rust/src/models/mod.rs b/rust/src/models/mod.rsindex a09913d2ac..2004632b95 100644--- a/rust/src/models/mod.rs+++ b/rust/src/models/mod.rs@@ -2,6 +2,7 @@ pub mod account;pub mod collection;pub mod error;pub mod file;+pub mod filter;pub mod metadata;pub use error::{Error, Result};