Skip to main content

link_common/models/
file_upload.rs

1//! Multipart file payload for SQL `FILE("placeholder")` uploads.
2
3/// File bytes referenced by a `FILE("placeholder")` expression in SQL.
4#[derive(Debug, Clone, PartialEq, Eq)]
5pub struct FileUpload {
6    /// Placeholder name used in SQL, without quotes (e.g. `"upload"` → `upload`).
7    pub placeholder: String,
8    /// Original filename sent to the server.
9    pub filename: String,
10    /// Raw file content.
11    pub data: Vec<u8>,
12    /// Optional MIME type override.
13    pub mime: Option<String>,
14}
15
16impl FileUpload {
17    /// Create a new upload payload for a SQL placeholder.
18    pub fn new(placeholder: impl Into<String>, filename: impl Into<String>, data: Vec<u8>) -> Self {
19        Self {
20            placeholder: placeholder.into(),
21            filename: filename.into(),
22            data,
23            mime: None,
24        }
25    }
26
27    /// Set an explicit MIME type for this upload.
28    pub fn with_mime(mut self, mime: impl Into<String>) -> Self {
29        self.mime = Some(mime.into());
30        self
31    }
32}
33
34impl FileUpload {
35    pub(crate) fn into_owned_tuple(self) -> (String, String, Vec<u8>, Option<String>) {
36        (self.placeholder, self.filename, self.data, self.mime)
37    }
38}
39
40pub(crate) fn uploads_to_owned(
41    files: Option<Vec<FileUpload>>,
42) -> Option<Vec<(String, String, Vec<u8>, Option<String>)>> {
43    files.map(|items| items.into_iter().map(FileUpload::into_owned_tuple).collect())
44}