mirror of
https://github.com/block/buzz.git
synced 2026-12-01 05:32:24 +00:00
fix(media): surface upload metadata from HEAD
Hoist upload attribution metadata keys and expose S3 user metadata from BlobHeadMeta so HEAD callers can verify the uploader/community stamp round-trips. Co-authored-by: Bradley Axen <baxen@squareup.com> Signed-off-by: Bradley Axen <baxen@squareup.com>
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
//! S3/MinIO storage client.
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::path::Path;
|
||||
use std::pin::Pin;
|
||||
|
||||
@@ -15,6 +16,11 @@ use serde::{Deserialize, Serialize};
|
||||
/// A stream of byte chunks from S3, usable with `axum::body::Body::from_stream()`.
|
||||
pub type ByteStream = Pin<Box<dyn futures_core::Stream<Item = Result<Bytes, MediaError>> + Send>>;
|
||||
|
||||
/// Bare S3 user-metadata key for the authenticated uploader pubkey.
|
||||
pub const BUZZ_UPLOADER_ID_META_KEY: &str = "buzz-uploader-id";
|
||||
/// Bare S3 user-metadata key for the server-resolved community id.
|
||||
pub const BUZZ_COMMUNITY_ID_META_KEY: &str = "buzz-community-id";
|
||||
|
||||
/// S3-compatible object storage client.
|
||||
pub struct MediaStorage {
|
||||
bucket: Box<Bucket>,
|
||||
@@ -219,12 +225,10 @@ impl MediaStorage {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// HEAD with metadata — returns Content-Length (size).
|
||||
/// HEAD with metadata — returns Content-Length (size) and user metadata.
|
||||
pub async fn head_with_metadata(&self, key: &str) -> Result<Option<BlobHeadMeta>, MediaError> {
|
||||
match self.bucket.head_object(key).await {
|
||||
Ok((result, _)) => Ok(Some(BlobHeadMeta {
|
||||
size: result.content_length.unwrap_or(0) as u64,
|
||||
})),
|
||||
Ok((result, _)) => Ok(Some(BlobHeadMeta::from_head_object_result(result))),
|
||||
Err(s3::error::S3Error::HttpFailWithBody(404, _)) => Ok(None),
|
||||
Err(e) => Err(MediaError::StorageError(e.to_string())),
|
||||
}
|
||||
@@ -396,25 +400,56 @@ mod tests {
|
||||
#[test]
|
||||
fn amz_meta_headers_are_prefixed_and_validated() {
|
||||
let headers = build_amz_meta_headers(&[
|
||||
("buzz-uploader-id", "aabbcc"),
|
||||
("buzz-community-id", "0000-1111"),
|
||||
(BUZZ_UPLOADER_ID_META_KEY, "aabbcc"),
|
||||
(BUZZ_COMMUNITY_ID_META_KEY, "0000-1111"),
|
||||
])
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
headers.get("x-amz-meta-buzz-uploader-id").unwrap(),
|
||||
headers
|
||||
.get(format!("x-amz-meta-{BUZZ_UPLOADER_ID_META_KEY}"))
|
||||
.unwrap(),
|
||||
"aabbcc"
|
||||
);
|
||||
assert_eq!(
|
||||
headers.get("x-amz-meta-buzz-community-id").unwrap(),
|
||||
headers
|
||||
.get(format!("x-amz-meta-{BUZZ_COMMUNITY_ID_META_KEY}"))
|
||||
.unwrap(),
|
||||
"0000-1111"
|
||||
);
|
||||
|
||||
// Control characters in values are rejected, not silently mangled.
|
||||
assert!(build_amz_meta_headers(&[("buzz-uploader-id", "bu\nzz")]).is_err());
|
||||
assert!(build_amz_meta_headers(&[(BUZZ_UPLOADER_ID_META_KEY, "bu\nzz")]).is_err());
|
||||
// Invalid header-name characters in the key are rejected.
|
||||
assert!(build_amz_meta_headers(&[("bad key", "v")]).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn blob_head_meta_surfaces_s3_user_metadata() {
|
||||
let mut metadata = HashMap::new();
|
||||
metadata.insert(BUZZ_UPLOADER_ID_META_KEY.to_string(), "aabbcc".to_string());
|
||||
metadata.insert(
|
||||
BUZZ_COMMUNITY_ID_META_KEY.to_string(),
|
||||
"0000-1111".to_string(),
|
||||
);
|
||||
|
||||
let result = s3::serde_types::HeadObjectResult {
|
||||
content_length: Some(42),
|
||||
metadata: Some(metadata),
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
let head = BlobHeadMeta::from_head_object_result(result);
|
||||
assert_eq!(head.size, 42);
|
||||
assert_eq!(
|
||||
head.metadata.get(BUZZ_UPLOADER_ID_META_KEY),
|
||||
Some(&"aabbcc".to_string())
|
||||
);
|
||||
assert_eq!(
|
||||
head.metadata.get(BUZZ_COMMUNITY_ID_META_KEY),
|
||||
Some(&"0000-1111".to_string())
|
||||
);
|
||||
}
|
||||
|
||||
/// Old sidecars (written before upload attribution) must still parse, and
|
||||
/// new fields must round-trip.
|
||||
#[test]
|
||||
@@ -459,9 +494,23 @@ fn build_amz_meta_headers(metadata: &[(&str, &str)]) -> Result<http::HeaderMap,
|
||||
Ok(headers)
|
||||
}
|
||||
|
||||
/// Metadata returned by HEAD — just enough for BUD-01 response headers.
|
||||
/// Metadata returned by HEAD for BUD-01 responses and moderation tooling.
|
||||
#[derive(Debug, Clone, Default, PartialEq, Eq)]
|
||||
pub struct BlobHeadMeta {
|
||||
/// Object size in bytes.
|
||||
pub size: u64,
|
||||
/// S3 user metadata returned by HEAD, keyed by bare metadata name (without
|
||||
/// the `x-amz-meta-` prefix), e.g. `buzz-uploader-id`.
|
||||
pub metadata: HashMap<String, String>,
|
||||
}
|
||||
|
||||
impl BlobHeadMeta {
|
||||
fn from_head_object_result(result: s3::serde_types::HeadObjectResult) -> Self {
|
||||
Self {
|
||||
size: result.content_length.unwrap_or(0).max(0) as u64,
|
||||
metadata: result.metadata.unwrap_or_default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Full blob metadata — stored as sidecar JSON in `_meta/{community}/{sha256}.json`.
|
||||
|
||||
@@ -8,13 +8,25 @@ use tokio::io::AsyncWriteExt;
|
||||
use crate::auth::verify_blossom_upload_auth;
|
||||
use crate::config::MediaConfig;
|
||||
use crate::error::MediaError;
|
||||
use crate::storage::{BlobMeta, MediaStorage};
|
||||
use crate::storage::{
|
||||
BlobMeta, MediaStorage, BUZZ_COMMUNITY_ID_META_KEY, BUZZ_UPLOADER_ID_META_KEY,
|
||||
};
|
||||
use crate::thumbnail::generate_image_metadata_sync;
|
||||
use crate::types::BlobDescriptor;
|
||||
use crate::validation::{
|
||||
mime_to_ext, validate_content, validate_file_content, validate_video_file,
|
||||
};
|
||||
|
||||
fn attribution_meta<'a>(
|
||||
uploader_id: &'a str,
|
||||
community_id: &'a str,
|
||||
) -> [(&'static str, &'a str); 2] {
|
||||
[
|
||||
(BUZZ_UPLOADER_ID_META_KEY, uploader_id),
|
||||
(BUZZ_COMMUNITY_ID_META_KEY, community_id),
|
||||
]
|
||||
}
|
||||
|
||||
/// Shared buffered-upload pipeline for the image and generic-file paths.
|
||||
///
|
||||
/// Both paths are identical except for two steps, which are injected:
|
||||
@@ -109,10 +121,7 @@ where
|
||||
&key,
|
||||
&body,
|
||||
&mime,
|
||||
&[
|
||||
("buzz-uploader-id", uploader_id.as_str()),
|
||||
("buzz-community-id", community_id.as_str()),
|
||||
],
|
||||
&attribution_meta(uploader_id.as_str(), community_id.as_str()),
|
||||
)
|
||||
.await?;
|
||||
|
||||
@@ -410,10 +419,7 @@ pub async fn process_video_upload(
|
||||
&key,
|
||||
&tmp_path,
|
||||
&mime,
|
||||
&[
|
||||
("buzz-uploader-id", uploader_id.as_str()),
|
||||
("buzz-community-id", community_id.as_str()),
|
||||
],
|
||||
&attribution_meta(uploader_id.as_str(), community_id.as_str()),
|
||||
)
|
||||
.await?;
|
||||
drop(tmp); // Free temp file disk space immediately after S3 upload.
|
||||
@@ -476,16 +482,10 @@ async fn generate_and_store_metadata(
|
||||
&thumb_key,
|
||||
tb,
|
||||
"image/jpeg",
|
||||
&[
|
||||
(
|
||||
"buzz-uploader-id",
|
||||
meta.uploader_id.as_deref().unwrap_or_default(),
|
||||
),
|
||||
(
|
||||
"buzz-community-id",
|
||||
meta.community_id.as_deref().unwrap_or_default(),
|
||||
),
|
||||
],
|
||||
&attribution_meta(
|
||||
meta.uploader_id.as_deref().unwrap_or_default(),
|
||||
meta.community_id.as_deref().unwrap_or_default(),
|
||||
),
|
||||
)
|
||||
.await?;
|
||||
}
|
||||
|
||||
@@ -18,7 +18,7 @@
|
||||
//! `BUZZ_S3_SECRET_KEY` / `BUZZ_S3_BUCKET`.
|
||||
|
||||
use buzz_media::config::MediaConfig;
|
||||
use buzz_media::storage::MediaStorage;
|
||||
use buzz_media::storage::{MediaStorage, BUZZ_COMMUNITY_ID_META_KEY, BUZZ_UPLOADER_ID_META_KEY};
|
||||
|
||||
fn minio_config() -> MediaConfig {
|
||||
MediaConfig {
|
||||
@@ -49,7 +49,15 @@ async fn static_creds_round_trip_against_minio() {
|
||||
|
||||
// PUT
|
||||
storage
|
||||
.put(&key, body, "application/octet-stream")
|
||||
.put_with_metadata(
|
||||
&key,
|
||||
body,
|
||||
"application/octet-stream",
|
||||
&[
|
||||
(BUZZ_UPLOADER_ID_META_KEY, "test-uploader"),
|
||||
(BUZZ_COMMUNITY_ID_META_KEY, "test-community"),
|
||||
],
|
||||
)
|
||||
.await
|
||||
.expect("put with static creds should succeed");
|
||||
|
||||
@@ -61,6 +69,14 @@ async fn static_creds_round_trip_against_minio() {
|
||||
.expect("head_with_metadata should succeed")
|
||||
.expect("object should exist");
|
||||
assert_eq!(meta.size, body.len() as u64);
|
||||
assert_eq!(
|
||||
meta.metadata.get(BUZZ_UPLOADER_ID_META_KEY),
|
||||
Some(&"test-uploader".to_string())
|
||||
);
|
||||
assert_eq!(
|
||||
meta.metadata.get(BUZZ_COMMUNITY_ID_META_KEY),
|
||||
Some(&"test-community".to_string())
|
||||
);
|
||||
|
||||
// GET round-trips the bytes
|
||||
let got = storage.get(&key).await.expect("get should succeed");
|
||||
|
||||
Reference in New Issue
Block a user