1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
//! Scratch storage for multi-step media uploads.
//!
//! The editor uploads
//! bytes first, then fills in title / alt / collection on a follow-up
//! form, then the row commits to [`crate::media::Media`]. If the
//! editor cancels or closes the tab mid-step, the scratch row is
//! collected by the background sweep — no orphan media leaks into
//! `cms_media`.
//!
//! Existing single-step `media_upload_submit` keeps working unchanged;
//! this primitive is the foundation for future drag-drop / multi-file
//! UIs where bytes arrive faster than the editor can fill metadata.
//!
//! Scratch objects live under the storage key `<tenant>/scratch/<hash>-<filename>`
//! so the commit path can rename in place rather than copying bytes
//! across volumes.
use chrono::{DateTime, Duration, Utc};
use rustango::sql::Auto;
use rustango::Model;
use serde::{Deserialize, Serialize};
/// How long a scratch upload sticks around before the sweep collects
/// it. Generous — editors sometimes step away mid-upload.
pub const STALE_THRESHOLD_HOURS: i64 = 24;
#[derive(Model, Debug, Clone, Serialize, Deserialize)]
#[rustango(
table = "cms_uploaded_file",
app = "cms",
display = "original_filename",
admin(
list_display = "user_id, original_filename, mime, size, uploaded_at",
ordering = "-uploaded_at",
)
)]
pub struct UploadedFile {
#[rustango(primary_key)]
pub id: Auto<i64>,
/// Editor who uploaded — for permission gating on commit.
#[rustango(fk = "rustango_users", on = "id", index)]
pub user_id: i64,
/// Opaque key inside the tenant's media storage backend. Living
/// under a `scratch/` prefix so directory listings make the
/// distinction obvious.
#[rustango(max_length = 512)]
pub storage_key: String,
#[rustango(max_length = 255)]
pub original_filename: String,
#[rustango(max_length = 128)]
pub mime: String,
pub size: i64,
/// SHA-256 hex. Used by commit to dedup against existing
/// `cms_media` rows.
#[rustango(max_length = 64, index)]
pub content_hash: String,
#[rustango(auto_now_add)]
pub uploaded_at: Auto<DateTime<Utc>>,
}
/// Insert one scratch row.
///
/// # Errors
/// Driver / query failures.
pub async fn create(
pool: &rustango::sql::Pool,
user_id: i64,
storage_key: String,
original_filename: String,
mime: String,
size: i64,
content_hash: String,
) -> Result<UploadedFile, rustango::sql::ExecError> {
let mut row = UploadedFile {
id: Auto::Unset,
user_id,
storage_key,
original_filename,
mime,
size,
content_hash,
uploaded_at: Auto::Unset,
};
row.insert(pool).await?;
Ok(row)
}
/// Look up a scratch row by id. Returns `None` when missing — callers
/// should treat that as the editor cancelled / sweep collected.
///
/// # Errors
/// Driver / query failures.
pub async fn get(
pool: &rustango::sql::Pool,
id: i64,
) -> Result<Option<UploadedFile>, rustango::sql::ExecError> {
use rustango::core::Column as _;
use rustango::sql::FetcherPool as _;
let mut rows: Vec<UploadedFile> = UploadedFile::objects()
.where_(UploadedFile::id.eq(id))
.fetch(pool)
.await?;
Ok(rows.pop())
}
/// Delete one scratch row by id. Returns the number of rows removed
/// (0 or 1). Filesystem cleanup is the caller's responsibility — this
/// only touches the DB row.
///
/// # Errors
/// Driver / query failures.
pub async fn delete(
pool: &rustango::sql::Pool,
id: i64,
) -> Result<usize, rustango::sql::ExecError> {
use rustango::core::Column as _;
use rustango::sql::FetcherPool as _;
let rows: Vec<UploadedFile> = UploadedFile::objects()
.where_(UploadedFile::id.eq(id))
.fetch(pool)
.await?;
let mut removed = 0;
for row in rows {
row.delete(pool).await?;
removed += 1;
}
Ok(removed)
}
/// Drop every scratch row older than [`STALE_THRESHOLD_HOURS`].
/// Filesystem-side cleanup is the caller's responsibility — the sweep
/// returns the list of `storage_key`s the caller should unlink so the
/// IO happens outside any held DB lock.
///
/// # Errors
/// Driver / query failures.
pub async fn gc_stale(pool: &rustango::sql::Pool) -> Result<Vec<String>, rustango::sql::ExecError> {
use rustango::sql::FetcherPool as _;
let cutoff = Utc::now() - Duration::hours(STALE_THRESHOLD_HOURS);
let all: Vec<UploadedFile> = UploadedFile::objects().fetch(pool).await?;
let mut unlinked: Vec<String> = Vec::new();
for row in all {
let uploaded_at = row.uploaded_at.get().copied();
let Some(ts) = uploaded_at else {
continue;
};
if ts >= cutoff {
continue;
}
let key = row.storage_key.clone();
if row.delete(pool).await.is_ok() {
unlinked.push(key);
}
}
Ok(unlinked)
}