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

image.rs82 lines, 2.9 KiBHistoryRawDownload
1//! The two halves every image upload shares, whatever the picture hangs on
2//! (exam question, bank template, pool question, solution, answer sheet):
3//! reading the `file` part under the school's size limit and the raster
4//! allowlist, and the blob-first write dance — fresh blob to disk, then the
5//! row, then the replaced blob off disk, with any row failure taking the fresh
6//! blob back out. A stored row therefore always points at a real blob, and a
7//! failed write strands nothing.
8//!
9//! Gates stay at the call sites (the exam freeze, the pool's approval, owner
10//! and author rules), as do the row types: this module owns only the
11//! format/limit rule and the rollback, once each.
12
13use std::future::Future;
14
15use axum::extract::Multipart;
16
17use crate::domain::note_file::FileContentType;
18use crate::error::AppError;
19use crate::service;
20use crate::state::AppState;
21
22use super::{blob_path, image_content_type, read_upload, remove_blob};
23
24/// A validated image upload: an allowlisted raster content type and the bytes.
25pub(crate) struct ImageUpload {
26 pub content_type: FileContentType,
27 pub data: Vec<u8>,
28}
29
30impl ImageUpload {
31 pub(crate) fn size(&self) -> i64 {
32 self.data.len() as i64
33 }
34}
35
36/// The multipart `file` part, held to the school's `max_file_bytes` and the
37/// raster allowlist — the one place either rule is spelled, so a format or
38/// limit change lands on every image endpoint at once.
39pub(crate) async fn read_image_upload(
40 st: &AppState,
41 multipart: &mut Multipart,
42) -> Result<ImageUpload, AppError> {
43 let limit = service::settings::load(&st.db).await?.get_max_file_bytes();
44 let upload = read_upload(multipart, limit).await?;
45 let content_type = image_content_type(&upload.content_type.unwrap_or_default())?;
46 Ok(ImageUpload {
47 content_type,
48 data: upload.data,
49 })
50}
51
52/// Blob first, row second: `data` lands on disk under `file`, then `write_row`
53/// runs. It returns what it stored plus the blob name it replaced (if any),
54/// which comes off disk only once the row is safely written; any error takes
55/// the fresh blob back out instead.
56pub(crate) async fn store_blob<T, Fut>(
57 st: &AppState,
58 file: &str,
59 data: &[u8],
60 write_row: impl FnOnce() -> Fut,
61) -> Result<T, AppError>
62where
63 Fut: Future<Output = Result<(T, Option<String>), AppError>>,
64{
65 crate::web::ensure_files_dir(&st.files_path).await?;
66 let path = blob_path(&st.files_path, file);
67 tokio::fs::write(&path, data)
68 .await
69 .map_err(|err| AppError::Internal(format!("failed to store the image blob: {err}")))?;
70 match write_row().await {
71 Ok((stored, replaced)) => {
72 if let Some(replaced) = replaced {
73 remove_blob(&st.files_path, &replaced).await;
74 }
75 Ok(stored)
76 }
77 Err(err) => {
78 let _ = tokio::fs::remove_file(&path).await;
79 Err(err)
80 }
81 }
82}