forge
Explore
Log in

Don't you dare go Hollow

hezarfen_backend

Clone
★ 0

0 watching0 forksRust8.3 MiB6d68754Ahmet Kaan Gümüş2 months ago566 commits

homework_file.rs110 lines, 3.7 KiBHistoryRawDownload
1//! A file attached to a homework submission. Like a note file, the row carries
2//! metadata only (original filename, MIME type, byte size) and the bytes live
3//! on disk under [`crate::config::Config::files_path`] — but named by this
4//! row's own `file` field (a fresh server-generated UUID per upload, like a
5//! question image), never by user input, so nothing a client sends shapes a
6//! disk path. Files are immutable: created and deleted, never updated, so the
7//! `submission`/`file`/`created_at` columns are only ever set once (READONLY
8//! app discipline — single writer). The web layer owns the blob I/O and its
9//! ordering (blob before row on upload, row before blob on delete); the rows
10//! are written and read by [`crate::db::homework_file`], the upload/attach
11//! workflow by [`crate::service::homework_file`].
12
13use crate::domain::homework_submission::HomeworkSubmissionId;
14use crate::domain::monotonic_id::next_uuid;
15use crate::domain::note_file::{FileContentType, FileName};
16use crate::domain::timestamp::Timestamp;
17
18#[derive(Debug, Clone, PartialEq, Eq, sqlx::Type)]
19#[sqlx(transparent)]
20pub struct HomeworkFileId(uuid::Uuid);
21
22impl HomeworkFileId {
23 /// Minted from the process-wide monotonic generator, not a plain random
24 /// UUID: a submission's files list `id DESC` (newest first), and a random
25 /// low half scrambles rows minted in the same millisecond.
26 pub fn generate() -> Self {
27 Self(next_uuid())
28 }
29
30 /// The inner uuid, for runtime-checked binds (Param/QueryBuilder) that
31 /// cannot take the newtype. Static `query!` binds take `self` directly.
32 pub fn uuid(&self) -> uuid::Uuid {
33 self.0
34 }
35
36 /// Parses a wire key. A key that is not a UUID parses as the nil UUID,
37 /// which matches no row.
38 pub fn from_key(key: &str) -> Self {
39 Self(uuid::Uuid::parse_str(key).unwrap_or(uuid::Uuid::nil()))
40 }
41
42 pub fn key(&self) -> String {
43 self.0.to_string()
44 }
45}
46
47/// An attachment row. `file` is the blob's on-disk name (a fresh UUID per
48/// upload), independent of the row id and reused as the GC key on cascade.
49#[derive(Debug, Clone, sqlx::FromRow)]
50pub struct HomeworkFile {
51 pub(crate) id: HomeworkFileId,
52 pub(crate) submission: HomeworkSubmissionId,
53 pub(crate) name: FileName,
54 pub(crate) content_type: FileContentType,
55 pub(crate) size: i64,
56 pub(crate) file: String,
57 pub(crate) created_at: Timestamp,
58}
59
60impl HomeworkFile {
61 /// Assemble a row (fresh blob name generated here) without persisting it.
62 /// The caller writes the blob under [`Self::get_file`] first, then calls
63 /// [`crate::db::homework_file::insert`] — so a stored row always points at
64 /// a real blob.
65 pub fn new(
66 submission: &HomeworkSubmissionId,
67 name: FileName,
68 content_type: FileContentType,
69 size: i64,
70 ) -> Self {
71 Self {
72 id: HomeworkFileId::generate(),
73 submission: submission.clone(),
74 name,
75 content_type,
76 size,
77 file: next_uuid().to_string(),
78 created_at: Timestamp::now(),
79 }
80 }
81
82 pub fn get_id(&self) -> &HomeworkFileId {
83 &self.id
84 }
85
86 pub fn get_submission(&self) -> &HomeworkSubmissionId {
87 &self.submission
88 }
89
90 pub fn get_name(&self) -> &FileName {
91 &self.name
92 }
93
94 pub fn get_content_type(&self) -> &FileContentType {
95 &self.content_type
96 }
97
98 pub fn get_size(&self) -> i64 {
99 self.size
100 }
101
102 /// The blob's on-disk name — a fresh UUID, so no user input shapes a path.
103 pub fn get_file(&self) -> &str {
104 &self.file
105 }
106
107 pub fn get_created_at(&self) -> Timestamp {
108 self.created_at
109 }
110}