|
| 1 | +use clap::Args; |
| 2 | +use tellers_api_client::apis::accepts_api_key_api as api; |
| 3 | + |
| 4 | +use crate::commands::api_config; |
| 5 | + |
| 6 | +#[derive(Args, Debug)] |
| 7 | +pub struct ExportArgs { |
| 8 | + /// Project ID to export |
| 9 | + #[arg(value_name = "PROJECT_ID")] |
| 10 | + pub project_id: String, |
| 11 | + |
| 12 | + /// Renditions to export (360p, 480p, 720p, 1080p, 1440p, 4k). Default 1080p when omitted. |
| 13 | + #[arg(long = "rendition", short = 'r', value_name = "RESOLUTION")] |
| 14 | + pub renditions: Vec<String>, |
| 15 | + |
| 16 | + #[arg(long, env = "TELLERS_API_KEY")] |
| 17 | + pub api_key: Option<String>, |
| 18 | + |
| 19 | + #[arg(long, env = "TELLERS_AUTH_BEARER")] |
| 20 | + pub auth_bearer: Option<String>, |
| 21 | +} |
| 22 | + |
| 23 | +const ALLOWED_RENDITIONS: &[&str] = &["360p", "480p", "720p", "1080p", "1440p", "4k"]; |
| 24 | + |
| 25 | +fn parse_rendition(s: &str) -> Result<String, String> { |
| 26 | + let v = s.trim().to_lowercase(); |
| 27 | + if ALLOWED_RENDITIONS.contains(&v.as_str()) { |
| 28 | + Ok(v) |
| 29 | + } else { |
| 30 | + Err(format!( |
| 31 | + "Invalid rendition '{}'; allowed: {}", |
| 32 | + s, |
| 33 | + ALLOWED_RENDITIONS.join(", ") |
| 34 | + )) |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +pub fn run(args: ExportArgs) -> Result<(), String> { |
| 39 | + let cfg = api_config::create_config(); |
| 40 | + let api_key = api_config::get_api_key(args.api_key)?; |
| 41 | + let bearer_header = api_config::get_bearer_header(args.auth_bearer); |
| 42 | + |
| 43 | + let rendition_strs: Vec<String> = if args.renditions.is_empty() { |
| 44 | + vec!["1080p".to_string()] |
| 45 | + } else { |
| 46 | + args.renditions |
| 47 | + .iter() |
| 48 | + .flat_map(|s| s.split(',').map(|s| s.trim().to_string())) |
| 49 | + .filter(|s| !s.is_empty()) |
| 50 | + .collect() |
| 51 | + }; |
| 52 | + |
| 53 | + let renditions: Vec<String> = rendition_strs |
| 54 | + .iter() |
| 55 | + .map(|s| parse_rendition(s)) |
| 56 | + .collect::<Result<Vec<_>, _>>()?; |
| 57 | + |
| 58 | + tokio::runtime::Runtime::new() |
| 59 | + .map_err(|e| format!("failed to start runtime: {}", e))? |
| 60 | + .block_on(async move { |
| 61 | + let resp = api::export_project_project_project_id_export_post( |
| 62 | + &cfg, |
| 63 | + &args.project_id, |
| 64 | + renditions, |
| 65 | + Some(&api_key), |
| 66 | + bearer_header.as_deref(), |
| 67 | + ) |
| 68 | + .await |
| 69 | + .map_err(|e| { |
| 70 | + let mut m = format!("export failed: {}", e); |
| 71 | + match &e { |
| 72 | + tellers_api_client::apis::Error::Reqwest(req_err) => { |
| 73 | + if let Some(status) = req_err.status() { |
| 74 | + m.push_str(&format!("; http_status: {}", status)); |
| 75 | + } |
| 76 | + } |
| 77 | + tellers_api_client::apis::Error::ResponseError(resp) => { |
| 78 | + m.push_str(&format!("; http_status: {}", resp.status)); |
| 79 | + if !resp.content.is_empty() { |
| 80 | + m.push_str(&format!("; response: {}", resp.content)); |
| 81 | + } |
| 82 | + } |
| 83 | + _ => {} |
| 84 | + } |
| 85 | + m |
| 86 | + })?; |
| 87 | + |
| 88 | + println!("task_id: {}", resp.task_id); |
| 89 | + println!("asset_id: {}", resp.asset_id); |
| 90 | + Ok(()) |
| 91 | + }) |
| 92 | +} |
0 commit comments