Add ThumbnailResponse struct with content_type and extension helper
This commit is contained in:
@@ -14,37 +14,58 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
|
|
||||||
// Download thumbnail (small size, default)
|
// Download thumbnail (small size, default)
|
||||||
println!("Downloading thumbnail...");
|
println!("Downloading thumbnail...");
|
||||||
let thumbnail = client
|
let response = client
|
||||||
.assets()
|
.assets()
|
||||||
.thumbnail(asset_id)
|
.thumbnail(asset_id)
|
||||||
.size(AssetMediaSize::Thumbnail)
|
.size(AssetMediaSize::Thumbnail)
|
||||||
.execute()
|
.execute()
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
fs::write("/path/to/output/thumbnail.jpg", &thumbnail)?;
|
println!("Content type: {}", response.content_type());
|
||||||
println!("Saved thumbnail: {} bytes", thumbnail.len());
|
println!("Extension: {:?}", response.extension());
|
||||||
|
|
||||||
|
let ext = response.extension().unwrap_or("jpg");
|
||||||
|
let output_path = format!("/path/to/output/thumbnail.{}", ext);
|
||||||
|
fs::write(&output_path, response.data())?;
|
||||||
|
println!(
|
||||||
|
"Saved thumbnail: {} bytes to {}",
|
||||||
|
response.data().len(),
|
||||||
|
output_path
|
||||||
|
);
|
||||||
|
|
||||||
// Download preview (medium size)
|
// Download preview (medium size)
|
||||||
let preview = client
|
let response = client
|
||||||
.assets()
|
.assets()
|
||||||
.thumbnail(asset_id)
|
.thumbnail(asset_id)
|
||||||
.size(AssetMediaSize::Preview)
|
.size(AssetMediaSize::Preview)
|
||||||
.execute()
|
.execute()
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
fs::write("/path/to/output/preview.jpg", &preview)?;
|
let ext = response.extension().unwrap_or("jpg");
|
||||||
println!("Saved preview: {} bytes", preview.len());
|
let output_path = format!("/path/to/output/preview.{}", ext);
|
||||||
|
fs::write(&output_path, response.data())?;
|
||||||
|
println!(
|
||||||
|
"Saved preview: {} bytes to {}",
|
||||||
|
response.data().len(),
|
||||||
|
output_path
|
||||||
|
);
|
||||||
|
|
||||||
// Download fullsize
|
// Download fullsize
|
||||||
let fullsize = client
|
let response = client
|
||||||
.assets()
|
.assets()
|
||||||
.thumbnail(asset_id)
|
.thumbnail(asset_id)
|
||||||
.size(AssetMediaSize::Fullsize)
|
.size(AssetMediaSize::Fullsize)
|
||||||
.execute()
|
.execute()
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
fs::write("/path/to/output/fullsize.jpg", &fullsize)?;
|
let ext = response.extension().unwrap_or("jpg");
|
||||||
println!("Saved fullsize: {} bytes", fullsize.len());
|
let output_path = format!("/path/to/output/fullsize.{}", ext);
|
||||||
|
fs::write(&output_path, response.data())?;
|
||||||
|
println!(
|
||||||
|
"Saved fullsize: {} bytes to {}",
|
||||||
|
response.data().len(),
|
||||||
|
output_path
|
||||||
|
);
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,6 +8,44 @@ use crate::{
|
|||||||
models::{AssetId, AssetMediaSize, AssetResponse, AssetUploadResponse, DeleteAssetsRequest},
|
models::{AssetId, AssetMediaSize, AssetResponse, AssetUploadResponse, DeleteAssetsRequest},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/// Response from downloading a thumbnail containing image data and metadata
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
pub struct ThumbnailResponse {
|
||||||
|
/// The image data as bytes
|
||||||
|
pub data: bytes::Bytes,
|
||||||
|
/// The MIME type of the image (e.g., "image/jpeg", "image/png", "image/webp")
|
||||||
|
pub content_type: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ThumbnailResponse {
|
||||||
|
/// Get the file extension based on content type
|
||||||
|
pub fn extension(&self) -> Option<&str> {
|
||||||
|
match self.content_type.as_str() {
|
||||||
|
"image/jpeg" | "image/jpg" => Some("jpg"),
|
||||||
|
"image/png" => Some("png"),
|
||||||
|
"image/webp" => Some("webp"),
|
||||||
|
"image/gif" => Some("gif"),
|
||||||
|
"image/tiff" => Some("tiff"),
|
||||||
|
_ => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Get the content type
|
||||||
|
pub fn content_type(&self) -> &str {
|
||||||
|
&self.content_type
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Get the data
|
||||||
|
pub fn data(&self) -> &bytes::Bytes {
|
||||||
|
&self.data
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Convert to owned bytes
|
||||||
|
pub fn into_data(self) -> bytes::Bytes {
|
||||||
|
self.data
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// API for managing assets (photos and videos)
|
/// API for managing assets (photos and videos)
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct AssetsApi {
|
pub struct AssetsApi {
|
||||||
@@ -338,7 +376,7 @@ impl ThumbnailBuilder {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Execute the request
|
/// Execute the request
|
||||||
pub async fn execute(self) -> Result<bytes::Bytes> {
|
pub async fn execute(self) -> Result<ThumbnailResponse> {
|
||||||
let path = format!("/assets/{}/thumbnail", self.id);
|
let path = format!("/assets/{}/thumbnail", self.id);
|
||||||
let mut req = self.client.get(&path);
|
let mut req = self.client.get(&path);
|
||||||
|
|
||||||
@@ -357,7 +395,17 @@ impl ThumbnailBuilder {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let response = self.client.execute(req.build()?).await?;
|
let response = self.client.execute(req.build()?).await?;
|
||||||
let bytes = response.bytes().await?;
|
|
||||||
Ok(bytes)
|
// Get content type from response headers
|
||||||
|
let content_type = response
|
||||||
|
.headers()
|
||||||
|
.get("content-type")
|
||||||
|
.and_then(|ct| ct.to_str().ok())
|
||||||
|
.map(String::from)
|
||||||
|
.unwrap_or_else(|| "application/octet-stream".to_string());
|
||||||
|
|
||||||
|
let data = response.bytes().await?;
|
||||||
|
|
||||||
|
Ok(ThumbnailResponse { data, content_type })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user