first commit
This commit is contained in:
commit
2a1854ab08
120 files changed
+11300
No files matched your search
@@ -0,0 +1,27 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
use sqlx::types::Json as SqlxJson;
|
||||
use uuid::Uuid;
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct UploadForm {
|
||||
pub entity_type: String,
|
||||
pub entity_id: String,
|
||||
#[serde(default = "default_kind")]
|
||||
pub kind: String,
|
||||
pub alt_th: Option<String>,
|
||||
#[serde(default)]
|
||||
pub display_order: i16,
|
||||
}
|
||||
|
||||
fn default_kind() -> String { "photo".into() }
|
||||
|
||||
#[derive(Debug, Serialize, sqlx::FromRow)]
|
||||
pub struct CreatedImage {
|
||||
pub id: Uuid,
|
||||
pub entity_type: String,
|
||||
pub entity_id: String,
|
||||
pub kind: String,
|
||||
pub url_path: String,
|
||||
pub display_order: i16,
|
||||
pub meta: SqlxJson<serde_json::Value>,
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
use super::{dto::*, service};
|
||||
use crate::{error::{AppError, AppResult}, state::AppState};
|
||||
use axum::{extract::{Multipart, State}, routing::post, Json, Router};
|
||||
use bytes::Bytes;
|
||||
|
||||
pub fn route() -> Router<AppState> {
|
||||
Router::new().route("/api/images", post(handler))
|
||||
}
|
||||
|
||||
async fn handler(
|
||||
State(state): State<AppState>,
|
||||
mut mp: Multipart,
|
||||
) -> AppResult<Json<serde_json::Value>> {
|
||||
let mut entity_type: Option<String> = None;
|
||||
let mut entity_id: Option<String> = None;
|
||||
let mut kind: Option<String> = None;
|
||||
let mut alt_th: Option<String> = None;
|
||||
let mut display_order: i16 = 0;
|
||||
let mut filename: Option<String> = None;
|
||||
let mut mime: Option<String> = None;
|
||||
let mut bytes: Option<Bytes> = None;
|
||||
|
||||
while let Some(field) = mp
|
||||
.next_field()
|
||||
.await
|
||||
.map_err(|e| AppError::BadRequest(e.to_string()))?
|
||||
{
|
||||
match field.name().unwrap_or("") {
|
||||
"entity_type" => entity_type = Some(field.text().await.map_err(io)?),
|
||||
"entity_id" => entity_id = Some(field.text().await.map_err(io)?),
|
||||
"kind" => kind = Some(field.text().await.map_err(io)?),
|
||||
"alt_th" => alt_th = Some(field.text().await.map_err(io)?),
|
||||
"display_order" => {
|
||||
display_order = field.text().await.map_err(io)?.parse().unwrap_or(0)
|
||||
}
|
||||
"file" => {
|
||||
filename = field.file_name().map(|s| s.to_string());
|
||||
mime = field.content_type().map(|s| s.to_string());
|
||||
bytes = Some(field.bytes().await.map_err(io)?);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
let form = UploadForm {
|
||||
entity_type: entity_type.ok_or_else(|| AppError::BadRequest("entity_type".into()))?,
|
||||
entity_id: entity_id.ok_or_else(|| AppError::BadRequest("entity_id".into()))?,
|
||||
kind: kind.unwrap_or_else(|| "photo".into()),
|
||||
alt_th,
|
||||
display_order,
|
||||
};
|
||||
let input = service::UploadInput {
|
||||
form,
|
||||
filename: filename.unwrap_or_else(|| "upload.bin".into()),
|
||||
mime: mime.unwrap_or_else(|| "application/octet-stream".into()),
|
||||
bytes: bytes.ok_or_else(|| AppError::BadRequest("file missing".into()))?,
|
||||
};
|
||||
let row = service::upload(&state, input).await?;
|
||||
Ok(Json(serde_json::to_value(row).unwrap()))
|
||||
}
|
||||
|
||||
fn io(e: axum::extract::multipart::MultipartError) -> AppError {
|
||||
AppError::BadRequest(e.to_string())
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
pub mod dto;
|
||||
pub mod handler;
|
||||
pub mod repo;
|
||||
pub mod service;
|
||||
pub use handler::route;
|
||||
@@ -0,0 +1,54 @@
|
||||
use super::dto::CreatedImage;
|
||||
use sqlx::PgPool;
|
||||
use uuid::Uuid;
|
||||
|
||||
pub async fn insert(
|
||||
pool: &PgPool,
|
||||
entity_type: &str,
|
||||
entity_id: &str,
|
||||
kind: &str,
|
||||
fid: &str,
|
||||
url_path: &str,
|
||||
display_order: i16,
|
||||
meta: &serde_json::Value,
|
||||
) -> Result<CreatedImage, sqlx::Error> {
|
||||
sqlx::query_as::<_, CreatedImage>(
|
||||
r#"
|
||||
INSERT INTO images (entity_type, entity_id, kind, fid, url_path, display_order, meta)
|
||||
VALUES ($1,$2,$3,$4,$5,$6,$7)
|
||||
ON CONFLICT (fid) DO UPDATE
|
||||
SET entity_type = EXCLUDED.entity_type,
|
||||
entity_id = EXCLUDED.entity_id,
|
||||
kind = EXCLUDED.kind,
|
||||
url_path = EXCLUDED.url_path,
|
||||
display_order = EXCLUDED.display_order,
|
||||
meta = EXCLUDED.meta
|
||||
RETURNING id, entity_type, entity_id, kind, url_path, display_order, meta
|
||||
"#,
|
||||
)
|
||||
.bind(entity_type)
|
||||
.bind(entity_id)
|
||||
.bind(kind)
|
||||
.bind(fid)
|
||||
.bind(url_path)
|
||||
.bind(display_order)
|
||||
.bind(meta)
|
||||
.fetch_one(pool)
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn count(pool: &PgPool) -> Result<i64, sqlx::Error> {
|
||||
let (n,): (i64,) = sqlx::query_as("SELECT COUNT(*) FROM images")
|
||||
.fetch_one(pool)
|
||||
.await?;
|
||||
Ok(n)
|
||||
}
|
||||
|
||||
pub async fn exists_fid(pool: &PgPool, fid: &str) -> Result<bool, sqlx::Error> {
|
||||
let row: Option<(Uuid,)> =
|
||||
sqlx::query_as("SELECT id FROM images WHERE fid = $1")
|
||||
.bind(fid)
|
||||
.fetch_optional(pool)
|
||||
.await?;
|
||||
Ok(row.is_some())
|
||||
}
|
||||
@@ -0,0 +1,136 @@
|
||||
use super::{dto::*, repo};
|
||||
use crate::{error::{AppError, AppResult}, state::AppState};
|
||||
use bytes::Bytes;
|
||||
use sha2::{Digest, Sha256};
|
||||
use std::path::Path;
|
||||
|
||||
pub struct UploadInput {
|
||||
pub form: UploadForm,
|
||||
pub filename: String,
|
||||
pub mime: String,
|
||||
pub bytes: Bytes,
|
||||
}
|
||||
|
||||
pub async fn upload(state: &AppState, input: UploadInput) -> AppResult<CreatedImage> {
|
||||
validate_entity_type(&input.form.entity_type)?;
|
||||
validate_kind(&input.form.kind)?;
|
||||
if input.bytes.is_empty() {
|
||||
return Err(AppError::BadRequest("empty file".into()));
|
||||
}
|
||||
|
||||
let (w, h) = probe_dimensions(&input.bytes);
|
||||
let sha = hex::encode(Sha256::digest(&input.bytes));
|
||||
let size = input.bytes.len() as i64;
|
||||
|
||||
let stored = state
|
||||
.seaweed
|
||||
.upload(
|
||||
&input.form.entity_type,
|
||||
&input.form.entity_id,
|
||||
&input.filename,
|
||||
&input.mime,
|
||||
input.bytes,
|
||||
)
|
||||
.await
|
||||
.map_err(|e| AppError::Internal(e.to_string()))?;
|
||||
|
||||
let meta = serde_json::json!({
|
||||
"mime": input.mime,
|
||||
"size_bytes": size,
|
||||
"width": w,
|
||||
"height": h,
|
||||
"alt_th": input.form.alt_th,
|
||||
"sha256": sha,
|
||||
});
|
||||
|
||||
let row = repo::insert(
|
||||
&state.db,
|
||||
&input.form.entity_type,
|
||||
&input.form.entity_id,
|
||||
&input.form.kind,
|
||||
&stored.fid,
|
||||
&format!("{}{}", state.public_base, stored.url_path),
|
||||
input.form.display_order,
|
||||
&meta,
|
||||
)
|
||||
.await?;
|
||||
|
||||
Ok(row)
|
||||
}
|
||||
|
||||
fn validate_entity_type(t: &str) -> AppResult<()> {
|
||||
match t {
|
||||
"route" | "station" | "sponsor" | "province" | "bus" | "misc" => Ok(()),
|
||||
_ => Err(AppError::BadRequest("invalid entity_type".into())),
|
||||
}
|
||||
}
|
||||
fn validate_kind(k: &str) -> AppResult<()> {
|
||||
match k {
|
||||
"photo" | "logo" | "icon" | "map" | "hero" => Ok(()),
|
||||
_ => Err(AppError::BadRequest("invalid kind".into())),
|
||||
}
|
||||
}
|
||||
|
||||
fn probe_dimensions(bytes: &[u8]) -> (Option<u32>, Option<u32>) {
|
||||
match image::load_from_memory(bytes) {
|
||||
Ok(img) => (Some(img.width()), Some(img.height())),
|
||||
Err(_) => (None, None),
|
||||
}
|
||||
}
|
||||
|
||||
/// Walk `dir/<entity_type>/<entity_id>/*.{jpg,png,webp}` and ingest each file
|
||||
/// once. Skips files whose sha256 already lives in `images` (matched
|
||||
/// by fid path).
|
||||
pub async fn ingest_seed_dir(state: &AppState, dir: &str) -> AppResult<()> {
|
||||
if repo::count(&state.db).await? > 0 {
|
||||
tracing::info!("seed-assets: images table already populated, skipping");
|
||||
return Ok(());
|
||||
}
|
||||
let base = Path::new(dir);
|
||||
if !base.exists() {
|
||||
tracing::warn!("seed-assets dir not found: {dir}");
|
||||
return Ok(());
|
||||
}
|
||||
let mut count = 0;
|
||||
for entry in walkdir::WalkDir::new(base).min_depth(3).max_depth(3) {
|
||||
let entry = match entry { Ok(e) => e, Err(_) => continue };
|
||||
if !entry.file_type().is_file() { continue; }
|
||||
let path = entry.path();
|
||||
let rel = match path.strip_prefix(base) { Ok(p) => p, Err(_) => continue };
|
||||
let mut comps = rel.components();
|
||||
let Some(et) = comps.next().and_then(|c| c.as_os_str().to_str()) else { continue };
|
||||
let Some(eid) = comps.next().and_then(|c| c.as_os_str().to_str()) else { continue };
|
||||
let Some(fname) = path.file_name().and_then(|s| s.to_str()) else { continue };
|
||||
let mime = mime_guess::from_path(path).first_or_octet_stream().to_string();
|
||||
let bytes = match tokio::fs::read(path).await {
|
||||
Ok(b) => Bytes::from(b),
|
||||
Err(e) => { tracing::warn!("read {path:?}: {e}"); continue }
|
||||
};
|
||||
let candidate_fid = format!("/{}/{}/{}", et, eid, fname);
|
||||
if repo::exists_fid(&state.db, &candidate_fid).await.unwrap_or(false) {
|
||||
continue;
|
||||
}
|
||||
let res = upload(
|
||||
state,
|
||||
UploadInput {
|
||||
form: UploadForm {
|
||||
entity_type: et.into(),
|
||||
entity_id: eid.into(),
|
||||
kind: "photo".into(),
|
||||
alt_th: None,
|
||||
display_order: 0,
|
||||
},
|
||||
filename: fname.into(),
|
||||
mime,
|
||||
bytes,
|
||||
},
|
||||
)
|
||||
.await;
|
||||
match res {
|
||||
Ok(_) => count += 1,
|
||||
Err(e) => tracing::warn!("ingest {path:?} failed: {e:?}"),
|
||||
}
|
||||
}
|
||||
tracing::info!("seed-assets ingested {count} files from {dir}");
|
||||
Ok(())
|
||||
}
|
||||
Reference in new issue
Block a user