use backbone_core::GenericCrudService;
use crate::presentation::dto::{CreateStoredFileDto, UpdateStoredFileDto};
use crate::domain::entity::StoredFile;
use crate::infrastructure::persistence::StoredFileRepository;
pub type StoredFileService = GenericCrudService<
StoredFile,
CreateStoredFileDto,
UpdateStoredFileDto,
StoredFileRepository,
>;
pub struct StoredFileRowRules;
#[async_trait::async_trait]
impl backbone_core::write_guard::WriteGuard<StoredFile> for StoredFileRowRules {
async fn check(
&self,
ctx: &backbone_core::write_guard::WriteCtx<'_, StoredFile>,
) -> backbone_core::write_guard::GuardOutcome {
use backbone_core::row_rule as r;
use backbone_core::violation::Violation;
use backbone_core::write_guard::{GuardOutcome, WriteKind};
let (create, update) = (ctx.kind == WriteKind::Create, ctx.kind == WriteKind::Update);
let Some(after) = ctx.after.filter(|_| create || update) else {
return GuardOutcome::allow();
};
let Ok(row) = serde_json::to_value(after) else {
return GuardOutcome::allow();
};
let row = &row;
let old = ctx.before.and_then(|b| serde_json::to_value(b).ok());
let mut shadow = Vec::new();
let mut refuse = Vec::new();
if update && ctx.transition.is_none() && r::changed(old.as_ref(), row, &["status"]) {
shadow.push(Violation::new("status", "state_field_verbs_only", "the state field moves only through its verbs"));
}
if (create || (update && r::changed(old.as_ref(), row, &["bucket_id"]))) && !r::holds(&r::and(r::is_not_null(r::field(row, "bucket_id")), r::not(r::is_empty(r::trim(r::field(row, "bucket_id")))))) {
refuse.push(Violation::new("bucket_id", "required", "bucket_id is required"));
}
if (create || (update && r::changed(old.as_ref(), row, &["owner_id"]))) && !r::holds(&r::and(r::is_not_null(r::field(row, "owner_id")), r::not(r::is_empty(r::trim(r::field(row, "owner_id")))))) {
refuse.push(Violation::new("owner_id", "required", "owner_id is required"));
}
if (create || (update && r::changed(old.as_ref(), row, &["path"]))) && !r::holds(&r::and(r::is_not_null(r::field(row, "path")), r::not(r::is_empty(r::trim(r::field(row, "path")))))) {
refuse.push(Violation::new("path", "required", "path is required"));
}
if (create || (update && r::changed(old.as_ref(), row, &["path"]))) && !r::holds(&r::le(r::length(r::field(row, "path")), r::Val::Num(1024.0_f64))) {
refuse.push(Violation::new("path", "max_length", "path must be at most 1024 characters").with_param("max", 1024_i64));
}
if (create || (update && r::changed(old.as_ref(), row, &["original_name"]))) && !r::holds(&r::and(r::is_not_null(r::field(row, "original_name")), r::not(r::is_empty(r::trim(r::field(row, "original_name")))))) {
refuse.push(Violation::new("original_name", "required", "original_name is required"));
}
if (create || (update && r::changed(old.as_ref(), row, &["original_name"]))) && !r::holds(&r::le(r::length(r::field(row, "original_name")), r::Val::Num(255.0_f64))) {
refuse.push(Violation::new("original_name", "max_length", "original_name must be at most 255 characters").with_param("max", 255_i64));
}
if (create || (update && r::changed(old.as_ref(), row, &["size_bytes"]))) && !r::holds(&r::ge(r::field(row, "size_bytes"), r::Val::Num(0.0_f64))) {
refuse.push(Violation::new("size_bytes", "non_negative", "size_bytes must not be negative"));
}
if (create || (update && r::changed(old.as_ref(), row, &["mime_type"]))) && !r::holds(&r::and(r::is_not_null(r::field(row, "mime_type")), r::not(r::is_empty(r::trim(r::field(row, "mime_type")))))) {
refuse.push(Violation::new("mime_type", "required", "mime_type is required"));
}
if (create || (update && r::changed(old.as_ref(), row, &["mime_type"]))) && !r::holds(&r::le(r::length(r::field(row, "mime_type")), r::Val::Num(127.0_f64))) {
refuse.push(Violation::new("mime_type", "max_length", "mime_type must be at most 127 characters").with_param("max", 127_i64));
}
if (create || (update && r::changed(old.as_ref(), row, &["checksum"]))) && !r::holds(&r::le(r::length(r::field(row, "checksum")), r::Val::Num(128.0_f64))) {
refuse.push(Violation::new("checksum", "max_length", "checksum must be at most 128 characters").with_param("max", 128_i64));
}
if (create || (update && r::changed(old.as_ref(), row, &["owner_module"]))) && !r::holds(&r::le(r::length(r::field(row, "owner_module")), r::Val::Num(100.0_f64))) {
refuse.push(Violation::new("owner_module", "max_length", "owner_module must be at most 100 characters").with_param("max", 100_i64));
}
if (create || (update && r::changed(old.as_ref(), row, &["owner_entity"]))) && !r::holds(&r::le(r::length(r::field(row, "owner_entity")), r::Val::Num(100.0_f64))) {
refuse.push(Violation::new("owner_entity", "max_length", "owner_entity must be at most 100 characters").with_param("max", 100_i64));
}
if (create || (update && r::changed(old.as_ref(), row, &["field_name"]))) && !r::holds(&r::le(r::length(r::field(row, "field_name")), r::Val::Num(100.0_f64))) {
refuse.push(Violation::new("field_name", "max_length", "field_name must be at most 100 characters").with_param("max", 100_i64));
}
if (create || (update && r::changed(old.as_ref(), row, &["storage_key"]))) && !r::holds(&r::and(r::is_not_null(r::field(row, "storage_key")), r::not(r::is_empty(r::trim(r::field(row, "storage_key")))))) {
refuse.push(Violation::new("storage_key", "required", "storage_key is required"));
}
if (create || (update && r::changed(old.as_ref(), row, &["storage_key"]))) && !r::holds(&r::le(r::length(r::field(row, "storage_key")), r::Val::Num(1024.0_f64))) {
refuse.push(Violation::new("storage_key", "max_length", "storage_key must be at most 1024 characters").with_param("max", 1024_i64));
}
if (create || (update && r::changed(old.as_ref(), row, &["download_count"]))) && !r::holds(&r::ge(r::field(row, "download_count"), r::Val::Num(0.0_f64))) {
refuse.push(Violation::new("download_count", "non_negative", "download_count must not be negative"));
}
if (create || update) && !r::holds(&r::le(r::length(r::field(row, "path")), r::Val::Num(1024.0_f64))) {
refuse.push(Violation::new("path", "path_too_long", "File path too long (max 1024 characters)"));
}
if (create || update) && !r::holds(&r::not(r::contains(r::field(row, "path"), r::text("..")))) {
refuse.push(Violation::new("path", "path_traversal", "Path traversal not allowed"));
}
if (create) && !r::holds(&r::le(r::length(r::field(row, "original_name")), r::Val::Num(255.0_f64))) {
refuse.push(Violation::new("original_name", "filename_too_long", "File name too long (max 255 characters)"));
}
if (create) && !r::holds(&r::matches(r::field(row, "mime_type"), "^[a-z]+/[a-z0-9.+-]+$")) {
refuse.push(Violation::new("mime_type", "invalid_mime_type", "Invalid MIME type format"));
}
if (update) && !r::holds(&r::ne(r::field(row, "status"), r::text("purged"))) {
refuse.push(Violation::new("status", "file_purged", "Cannot update purged files"));
}
if (update) && !r::holds(&r::or(r::ne(r::field(row, "status"), r::text("quarantined")), r::has_role(ctx.actor.as_deref(), "admin").await)) {
refuse.push(Violation::new("status", "quarantine_restricted", "Only admins can modify quarantined files"));
}
GuardOutcome { refuse, shadow }
}
}