pub struct Rules {
pub max_bytes: u64,
pub content_types: &'static [&'static str],
}Expand description
Describes what a file field accepts: a size limit and a content-type allowlist.
Checked by Validator::file before anything is
stored. A const, so the Multipart request limit can be computed from
it (generated forms use the sum of their files’ limits plus 1 MB). The
content type comes from the browser: the allowlist limits it, and
Validator::file_content checks the bytes match it.
§Examples
use ocre::storage::Rules;
const AVATAR: Rules = Rules { max_bytes: 5 * 1024 * 1024, content_types: &["image/png", "image/jpeg"] };
const FORM_LIMIT: usize = AVATAR.max_bytes as usize + 1024 * 1024;
assert_eq!(FORM_LIMIT, 6 * 1024 * 1024);
assert!(AVATAR.allows("image/PNG"));
assert!(!AVATAR.allows("image/svg+xml"));Fields§
§max_bytes: u64Largest accepted file, in bytes (u64: multipart uploads take files
over 4 GB, more than a usize holds in WebAssembly). Request limits
are usize: RULES.max_bytes as usize.
content_types: &'static [&'static str]Accepted content types, exact and lowercase (image/png).
There is no wildcard: image/* would admit SVG, which can carry scripts.
Implementations§
Source§impl Rules
impl Rules
Sourcepub fn allows(&self, content_type: &str) -> bool
pub fn allows(&self, content_type: &str) -> bool
Returns whether content_type is in the allowlist, ignoring case and parameters.
Image/PNG; charset=binary is compared as image/png; an empty type
counts as application/octet-stream. There is no wildcard matching.
§Examples
use ocre::storage::Rules;
const DOC: Rules = Rules { max_bytes: 1024, content_types: &["application/pdf", "text/plain"] };
assert!(DOC.allows("text/plain; charset=utf-8"));
assert!(DOC.allows("Application/PDF"));
assert!(!DOC.allows("text/html"));
assert!(!DOC.allows(""));Trait Implementations§
impl Copy for Rules
impl Eq for Rules
impl StructuralPartialEq for Rules
Auto Trait Implementations§
impl Freeze for Rules
impl RefUnwindSafe for Rules
impl Send for Rules
impl Sync for Rules
impl Unpin for Rules
impl UnsafeUnpin for Rules
impl UnwindSafe for Rules
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.