pub fn delete_attachments(
ctx: &Ctx,
attachments: &[Option<Attachment>],
) -> impl Future<Output = Result<()>> + Send + use<>Expand description
Deletes the objects of every Some attachment in one R2 call.
None entries (optional attachments left empty) are skipped, and
nothing is sent when all are None. Generated models call it after a
record is deleted, or after an update replaced or removed its files, so
a failed database write never loses a file that a row still points to.
Missing keys are not an error. R2 deletes are free.
§Errors
Error::Internal (500) when the STORAGE
binding is missing (the message shows the STORAGE entry to add
to cloudflare.config.ts) or R2 fails.
§Examples
use axum::{extract::{Path, State}, http::StatusCode};
use ocre::storage::{self, Attachment};
use ocre::{Ctx, Result, params};
async fn destroy(State(ctx): State<Ctx>, Path(id): Path<i64>) -> Result<StatusCode> {
let db = ctx.db()?;
let image: Option<Attachment> = db
.first(
"SELECT image_key AS key, image_filename AS filename, image_content_type AS content_type, \
image_size AS size FROM photos WHERE id = ?1 AND image_key IS NOT NULL",
params![id],
)
.await?;
db.execute("DELETE FROM photos WHERE id = ?1", params![id]).await?;
storage::delete_attachments(&ctx, &[image]).await?;
Ok(StatusCode::NO_CONTENT)
}