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

course_note_file.rs94 lines, 2.9 KiBHistoryRawDownload
1//! A file attached to a course note. The row carries metadata only (original
2//! filename, MIME type, byte size); the bytes themselves live on disk under
3//! [`crate::config::Config::files_path`], in a file named by this row's `file`
4//! field — a server-generated UUID, so no user input ever shapes a disk path.
5//! The web layer owns the blob I/O and its ordering (blob before row on
6//! upload, row before blob on delete); persistence lives in
7//! [`crate::db::course_note_file`]. `FileName` and `FileContentType` are
8//! shared with [`crate::domain::note_file`] — same validation, no reason to
9//! duplicate it.
10
11pub use crate::domain::note_file::{FileContentType, FileName};
12
13use crate::domain::course_note::CourseNoteId;
14use crate::domain::monotonic_id::next_uuid;
15
16#[derive(Debug, Clone, PartialEq, Eq, sqlx::Type)]
17#[sqlx(transparent)]
18pub struct CourseNoteFileId(uuid::Uuid);
19
20impl CourseNoteFileId {
21 /// Minted from the process-wide monotonic generator, not a plain random
22 /// UUID: a note's files list `id DESC` (newest first,
23 /// [`crate::db::course_note_file::list_for`]), and a random low half
24 /// scrambles rows minted in the same millisecond.
25 pub fn generate() -> Self {
26 Self(next_uuid())
27 }
28
29 /// The inner uuid, for runtime-checked binds (Param/QueryBuilder) that
30 /// cannot take the newtype. Static `query!` binds take `self` directly.
31 pub fn uuid(&self) -> uuid::Uuid {
32 self.0
33 }
34
35 /// Parses a wire key. A key that is not a UUID parses as the nil UUID,
36 /// which matches no row.
37 pub fn from_key(key: &str) -> Self {
38 Self(uuid::Uuid::parse_str(key).unwrap_or(uuid::Uuid::nil()))
39 }
40
41 pub fn key(&self) -> String {
42 self.0.to_string()
43 }
44}
45
46#[derive(Debug, Clone, sqlx::FromRow)]
47pub struct CourseNoteFile {
48 pub(crate) id: CourseNoteFileId,
49 pub(crate) course_note: CourseNoteId,
50 pub(crate) name: FileName,
51 pub(crate) content_type: FileContentType,
52 pub(crate) size: i64,
53}
54
55impl CourseNoteFile {
56 /// Assemble a new attachment row (id generated here) without persisting
57 /// it. The caller writes the blob to disk under the fresh id first, then
58 /// calls [`crate::db::course_note_file::insert`] — so a stored row always
59 /// points at a real blob.
60 pub fn new(
61 note: &CourseNoteId,
62 name: FileName,
63 content_type: FileContentType,
64 size: i64,
65 ) -> Self {
66 Self {
67 id: CourseNoteFileId::generate(),
68 course_note: note.clone(),
69 name,
70 content_type,
71 size,
72 }
73 }
74
75 pub fn get_id(&self) -> &CourseNoteFileId {
76 &self.id
77 }
78
79 pub fn get_course_note(&self) -> &CourseNoteId {
80 &self.course_note
81 }
82
83 pub fn get_name(&self) -> &FileName {
84 &self.name
85 }
86
87 pub fn get_content_type(&self) -> &FileContentType {
88 &self.content_type
89 }
90
91 pub fn get_size(&self) -> i64 {
92 self.size
93 }
94}