pub struct Upload {
pub filename: String,
pub content_type: String,
pub bytes: Bytes,
}Expand description
A file received in a multipart/form-data request, before it is stored.
Get it with MultipartForm::file, check it with
Validator::file, store it with store. The
bytes are a slice of the request body held in memory (no copy until R2
gets them).
§Examples
use ocre::storage::Upload;
let upload = Upload { filename: "notes.txt".into(), content_type: "text/plain".into(), bytes: "hi".into() };
assert_eq!(upload.size(), 2);Fields§
§filename: StringFile name sent by the browser, cleaned up.
Directories (C:\Users\me\ from old Windows browsers) and control
characters are removed, the name is trimmed to 200 characters with its
extension kept, and file stands in when nothing is left.
content_type: StringContent type sent by the browser, lowercase without parameters.
application/octet-stream when none was sent. The client chooses it:
check it against an allowlist with Validator::file.
bytes: BytesThe file’s bytes.
Implementations§
Source§impl Upload
impl Upload
Sourcepub fn new(filename: &str, content_type: &str, bytes: impl Into<Bytes>) -> Self
pub fn new(filename: &str, content_type: &str, bytes: impl Into<Bytes>) -> Self
An upload made by the app (Active Storage’s attach(io:, filename:, content_type:)), cleaned up like a browser’s.
For bytes that did not come from a form: a generated PDF, a fetched
image, a test fixture. The file name loses directories and control
characters, the content type is lowercased without parameters, as
MultipartForm::file does. Hand it to a model (NewPhoto { image: Some(upload), .. }) or to store; store_bytes stores bytes directly.
§Examples
use ocre::storage::Upload;
let upload = Upload::new("reports/2026.pdf", "Application/PDF; x=y", b"%PDF-1.7".to_vec());
assert_eq!((upload.filename.as_str(), upload.content_type.as_str()), ("2026.pdf", "application/pdf"));
assert_eq!(upload.size(), 8);