first commit
This commit is contained in:
commit
2a1854ab08
120 files changed
+11300
No files matched your search
@@ -0,0 +1 @@
|
||||
pub mod seaweed;
|
||||
@@ -0,0 +1,126 @@
|
||||
use bytes::Bytes;
|
||||
use reqwest::Client;
|
||||
use serde::Deserialize;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct SeaweedClient {
|
||||
pub filer_url: String,
|
||||
pub http: Client,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct UploadResult {
|
||||
#[serde(default)]
|
||||
pub fid: String,
|
||||
#[serde(default)]
|
||||
pub size: u64,
|
||||
#[serde(default)]
|
||||
pub mime: Option<String>,
|
||||
pub name: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct StoredObject {
|
||||
pub fid: String,
|
||||
pub url_path: String,
|
||||
pub size: u64,
|
||||
pub mime: String,
|
||||
}
|
||||
|
||||
impl SeaweedClient {
|
||||
pub fn new(filer_url: impl Into<String>) -> Self {
|
||||
Self {
|
||||
filer_url: filer_url.into(),
|
||||
http: Client::builder()
|
||||
.timeout(std::time::Duration::from_secs(60))
|
||||
.build()
|
||||
.expect("reqwest client"),
|
||||
}
|
||||
}
|
||||
|
||||
/// PUT bytes to `/buckets/transit/<entity_type>/<entity_id>/<filename>`.
|
||||
/// Returns the relative public path under SEAWEED_PUBLIC_BASE (without prefix).
|
||||
pub async fn upload(
|
||||
&self,
|
||||
entity_type: &str,
|
||||
entity_id: &str,
|
||||
filename: &str,
|
||||
mime: &str,
|
||||
bytes: Bytes,
|
||||
) -> anyhow::Result<StoredObject> {
|
||||
let safe_name = sanitize_filename(filename);
|
||||
let key = format!(
|
||||
"/buckets/transit/{}/{}/{}",
|
||||
sanitize_segment(entity_type),
|
||||
sanitize_segment(entity_id),
|
||||
safe_name
|
||||
);
|
||||
let url = format!("{}{}", self.filer_url, key);
|
||||
let size = bytes.len() as u64;
|
||||
let resp = self
|
||||
.http
|
||||
.put(&url)
|
||||
.header(reqwest::header::CONTENT_TYPE, mime)
|
||||
.body(bytes)
|
||||
.send()
|
||||
.await?;
|
||||
if !resp.status().is_success() {
|
||||
anyhow::bail!("filer upload failed: {} {}", resp.status(), resp.text().await.unwrap_or_default());
|
||||
}
|
||||
let body: serde_json::Value = resp.json().await.unwrap_or(serde_json::Value::Null);
|
||||
let fid = body
|
||||
.get("fid")
|
||||
.and_then(|v| v.as_str())
|
||||
.map(|s| s.to_string())
|
||||
.unwrap_or_else(|| key.clone());
|
||||
Ok(StoredObject {
|
||||
fid,
|
||||
url_path: key.trim_start_matches("/buckets/transit").to_string(),
|
||||
size,
|
||||
mime: mime.to_string(),
|
||||
})
|
||||
}
|
||||
|
||||
pub async fn delete(&self, url_path: &str) -> anyhow::Result<()> {
|
||||
let url = format!("{}/buckets/transit{}", self.filer_url, url_path);
|
||||
let resp = self.http.delete(&url).send().await?;
|
||||
if !resp.status().is_success() && resp.status() != reqwest::StatusCode::NOT_FOUND {
|
||||
anyhow::bail!("filer delete failed: {}", resp.status());
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn fetch(&self, url_path: &str) -> anyhow::Result<(Bytes, String)> {
|
||||
let url = format!("{}/buckets/transit{}", self.filer_url, url_path);
|
||||
let resp = self.http.get(&url).send().await?;
|
||||
if !resp.status().is_success() {
|
||||
anyhow::bail!("filer fetch failed: {}", resp.status());
|
||||
}
|
||||
let mime = resp
|
||||
.headers()
|
||||
.get(reqwest::header::CONTENT_TYPE)
|
||||
.and_then(|v| v.to_str().ok())
|
||||
.unwrap_or("application/octet-stream")
|
||||
.to_string();
|
||||
let bytes = resp.bytes().await?;
|
||||
Ok((bytes, mime))
|
||||
}
|
||||
}
|
||||
|
||||
fn sanitize_segment(s: &str) -> String {
|
||||
s.chars()
|
||||
.map(|c| if c.is_ascii_alphanumeric() || c == '-' || c == '_' { c } else { '_' })
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn sanitize_filename(s: &str) -> String {
|
||||
s.chars()
|
||||
.map(|c| {
|
||||
if c.is_ascii_alphanumeric() || matches!(c, '-' | '_' | '.') {
|
||||
c
|
||||
} else {
|
||||
'_'
|
||||
}
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
Reference in new issue
Block a user