Add ThumbnailResponse struct with content_type and extension helper

This commit is contained in:
Joakim Hulthe
2026-04-06 08:20:13 +00:00
parent 15d67f5667
commit 037f9a5782
2 changed files with 81 additions and 12 deletions

View File

@@ -8,6 +8,44 @@ use crate::{
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)
#[derive(Debug, Clone)]
pub struct AssetsApi {
@@ -338,7 +376,7 @@ impl ThumbnailBuilder {
}
/// 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 mut req = self.client.get(&path);
@@ -357,7 +395,17 @@ impl ThumbnailBuilder {
}
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 })
}
}