|
| 1 | +use anstream::println; |
| 2 | +use anyhow::{Context, Result}; |
| 3 | +use std::path::PathBuf; |
| 4 | + |
| 5 | +use crate::api::{BacklogApi, BacklogClient}; |
| 6 | + |
| 7 | +pub struct ProjectImageArgs { |
| 8 | + key: String, |
| 9 | + output: Option<PathBuf>, |
| 10 | +} |
| 11 | + |
| 12 | +impl ProjectImageArgs { |
| 13 | + pub fn new(key: String, output: Option<PathBuf>) -> Self { |
| 14 | + Self { key, output } |
| 15 | + } |
| 16 | +} |
| 17 | + |
| 18 | +pub fn image(args: &ProjectImageArgs) -> Result<()> { |
| 19 | + let client = BacklogClient::from_config()?; |
| 20 | + image_with(args, &client) |
| 21 | +} |
| 22 | + |
| 23 | +pub fn image_with(args: &ProjectImageArgs, api: &dyn BacklogApi) -> Result<()> { |
| 24 | + let (bytes, filename) = api.download_project_image(&args.key)?; |
| 25 | + let path = args |
| 26 | + .output |
| 27 | + .clone() |
| 28 | + .unwrap_or_else(|| default_output_path(&filename)); |
| 29 | + std::fs::write(&path, &bytes).with_context(|| format!("Failed to write {}", path.display()))?; |
| 30 | + println!("Saved: {} ({} bytes)", path.display(), bytes.len()); |
| 31 | + Ok(()) |
| 32 | +} |
| 33 | + |
| 34 | +fn default_output_path(filename: &str) -> PathBuf { |
| 35 | + let normalized = filename.trim(); |
| 36 | + let base = std::path::Path::new(normalized) |
| 37 | + .file_name() |
| 38 | + .unwrap_or(std::ffi::OsStr::new("")); |
| 39 | + let base_lower = base.to_string_lossy().to_ascii_lowercase(); |
| 40 | + let is_generic_attachment = base_lower == "attachment" || base_lower.starts_with("attachment."); |
| 41 | + |
| 42 | + if base.is_empty() || is_generic_attachment { |
| 43 | + PathBuf::from("project_image") |
| 44 | + } else { |
| 45 | + PathBuf::from(base) |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +#[cfg(test)] |
| 50 | +mod tests { |
| 51 | + use super::*; |
| 52 | + use anyhow::anyhow; |
| 53 | + use tempfile::tempdir; |
| 54 | + |
| 55 | + struct MockApi { |
| 56 | + result: Option<(Vec<u8>, String)>, |
| 57 | + } |
| 58 | + |
| 59 | + impl crate::api::BacklogApi for MockApi { |
| 60 | + fn download_project_image(&self, _key: &str) -> anyhow::Result<(Vec<u8>, String)> { |
| 61 | + self.result |
| 62 | + .clone() |
| 63 | + .ok_or_else(|| anyhow!("download failed")) |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + fn args(output: Option<PathBuf>) -> ProjectImageArgs { |
| 68 | + ProjectImageArgs::new("TEST".to_string(), output) |
| 69 | + } |
| 70 | + |
| 71 | + #[test] |
| 72 | + fn image_with_saves_file_to_specified_path() { |
| 73 | + let dir = tempdir().unwrap(); |
| 74 | + let path = dir.path().join("out.png"); |
| 75 | + let api = MockApi { |
| 76 | + result: Some((b"png-data".to_vec(), "project_image.png".to_string())), |
| 77 | + }; |
| 78 | + assert!(image_with(&args(Some(path.clone())), &api).is_ok()); |
| 79 | + assert_eq!(std::fs::read(&path).unwrap(), b"png-data"); |
| 80 | + } |
| 81 | + |
| 82 | + #[test] |
| 83 | + fn image_with_propagates_api_error() { |
| 84 | + let api = MockApi { result: None }; |
| 85 | + let err = image_with(&args(None), &api).unwrap_err(); |
| 86 | + assert!(err.to_string().contains("download failed")); |
| 87 | + } |
| 88 | + |
| 89 | + #[test] |
| 90 | + fn default_output_path_uses_server_filename() { |
| 91 | + assert_eq!( |
| 92 | + default_output_path("project_image.png"), |
| 93 | + PathBuf::from("project_image.png") |
| 94 | + ); |
| 95 | + } |
| 96 | + |
| 97 | + #[test] |
| 98 | + fn default_output_path_falls_back_for_attachment() { |
| 99 | + assert_eq!( |
| 100 | + default_output_path("attachment"), |
| 101 | + PathBuf::from("project_image") |
| 102 | + ); |
| 103 | + } |
| 104 | + |
| 105 | + #[test] |
| 106 | + fn default_output_path_falls_back_for_attachment_with_extension() { |
| 107 | + assert_eq!( |
| 108 | + default_output_path("attachment.png"), |
| 109 | + PathBuf::from("project_image") |
| 110 | + ); |
| 111 | + } |
| 112 | + |
| 113 | + #[test] |
| 114 | + fn default_output_path_falls_back_for_path_with_attachment_basename() { |
| 115 | + assert_eq!( |
| 116 | + default_output_path("foo/attachment.png"), |
| 117 | + PathBuf::from("project_image") |
| 118 | + ); |
| 119 | + } |
| 120 | + |
| 121 | + #[test] |
| 122 | + fn default_output_path_falls_back_for_empty() { |
| 123 | + assert_eq!(default_output_path(""), PathBuf::from("project_image")); |
| 124 | + } |
| 125 | +} |
0 commit comments