Skip to main content

max / makenotwork

2.0 KB · 73 lines History Blame Raw
1 use super::{PgPool, Uuid};
2
3 /// Insert an uploaded image record.
4 #[tracing::instrument(skip_all)]
5 pub async fn insert_image(
6 pool: &PgPool,
7 uploader_id: Uuid,
8 community_id: Uuid,
9 s3_key: &str,
10 filename: &str,
11 content_type: &str,
12 size_bytes: i64,
13 ) -> Result<Uuid, sqlx::Error> {
14 sqlx::query_scalar!(
15 "INSERT INTO images (uploader_id, community_id, s3_key, filename, content_type, size_bytes)
16 VALUES ($1, $2, $3, $4, $5, $6)
17 RETURNING id",
18 uploader_id,
19 community_id,
20 s3_key,
21 filename,
22 content_type,
23 size_bytes,
24 )
25 .fetch_one(pool)
26 .await
27 }
28
29 /// Mark an image as removed by a moderator.
30 #[tracing::instrument(skip_all)]
31 pub async fn remove_image<'e, E: sqlx::PgExecutor<'e>>(
32 executor: E,
33 image_id: Uuid,
34 removed_by: Uuid,
35 ) -> Result<(), sqlx::Error> {
36 sqlx::query!(
37 "UPDATE images SET removed_at = now(), removed_by = $2 WHERE id = $1 AND removed_at IS NULL",
38 image_id,
39 removed_by,
40 )
41 .execute(executor)
42 .await?;
43 Ok(())
44 }
45
46 /// Hard-delete an image row. Used to roll back the row inserted just before a
47 /// failed S3 upload, the object never landed in the bucket, so there is
48 /// nothing to reconcile and the row should not exist.
49 #[tracing::instrument(skip_all)]
50 pub async fn delete_image_row(pool: &PgPool, image_id: Uuid) -> Result<(), sqlx::Error> {
51 sqlx::query!("DELETE FROM images WHERE id = $1", image_id)
52 .execute(pool)
53 .await?;
54 Ok(())
55 }
56
57 /// Mark images whose backing S3 object has been deleted, so the reconcile sweep
58 /// never revisits them. Called inline after a successful best-effort delete and
59 /// in batch by the background sweep.
60 #[tracing::instrument(skip_all)]
61 pub async fn mark_images_s3_purged(pool: &PgPool, image_ids: &[Uuid]) -> Result<(), sqlx::Error> {
62 if image_ids.is_empty() {
63 return Ok(());
64 }
65 sqlx::query!(
66 "UPDATE images SET s3_purged_at = now() WHERE id = ANY($1)",
67 image_ids
68 )
69 .execute(pool)
70 .await?;
71 Ok(())
72 }
73