first commit

This commit is contained in:
phattaranun.a committed 2026-05-24 22:55:17 +07:00
commit 2a1854ab08
120 files changed
+11300

No files matched your search

+1
View File
@@ -0,0 +1 @@
// no DTOs beyond path id
+18
View File
@@ -0,0 +1,18 @@
use super::service;
use crate::{error::AppResult, state::AppState};
use axum::{
extract::{Path, State},
http::StatusCode,
routing::delete,
Router,
};
use uuid::Uuid;
pub fn route() -> Router<AppState> {
Router::new().route("/api/images/:id", delete(handler))
}
async fn handler(State(state): State<AppState>, Path(id): Path<Uuid>) -> AppResult<StatusCode> {
service::handle(&state, id).await?;
Ok(StatusCode::NO_CONTENT)
}
+5
View File
@@ -0,0 +1,5 @@
pub mod dto;
pub mod handler;
pub mod repo;
pub mod service;
pub use handler::route;
+34
View File
@@ -0,0 +1,34 @@
use sqlx::PgPool;
use uuid::Uuid;
pub struct ImageRowMin {
pub fid: String,
pub url_path: String,
}
pub async fn fetch_min(pool: &PgPool, id: Uuid) -> Result<Option<ImageRowMin>, sqlx::Error> {
let row: Option<(String, String)> =
sqlx::query_as("SELECT fid, url_path FROM images WHERE id = $1")
.bind(id)
.fetch_optional(pool)
.await?;
Ok(row.map(|(fid, url_path)| ImageRowMin { fid, url_path }))
}
pub async fn delete(pool: &PgPool, id: Uuid) -> Result<u64, sqlx::Error> {
let r = sqlx::query("DELETE FROM images WHERE id = $1")
.bind(id)
.execute(pool)
.await?;
Ok(r.rows_affected())
}
pub async fn mark_pending_gc(pool: &PgPool, id: Uuid) -> Result<(), sqlx::Error> {
sqlx::query(
"UPDATE images SET meta = meta || '{\"pending_gc\":true}'::jsonb WHERE id = $1",
)
.bind(id)
.execute(pool)
.await?;
Ok(())
}
+25
View File
@@ -0,0 +1,25 @@
use super::repo;
use crate::{error::{AppError, AppResult}, state::AppState};
use uuid::Uuid;
pub async fn handle(state: &AppState, id: Uuid) -> AppResult<()> {
let Some(row) = repo::fetch_min(&state.db, id).await? else {
return Err(AppError::NotFound);
};
// strip public_base prefix to compute filer relative path
let rel = row
.url_path
.strip_prefix(state.public_base.as_str())
.unwrap_or(&row.url_path);
match state.seaweed.delete(rel).await {
Ok(()) => {
repo::delete(&state.db, id).await?;
Ok(())
}
Err(e) => {
tracing::warn!("filer delete failed for {id}: {e:?}; marking pending_gc");
repo::mark_pending_gc(&state.db, id).await?;
Err(AppError::Internal("storage delete failed".into()))
}
}
}