Skip to main content

Uploader

Trait Uploader 

Source
pub trait Uploader:
    Send
    + Sync
    + 'static {
    // Required methods
    fn store(
        &self,
        cx: &Cx,
        filename: &str,
        bytes: &[u8],
    ) -> impl Future<Output = Result<String, String>> + Send;
    fn keep(
        &self,
        cx: &Cx,
        path: &str,
    ) -> impl Future<Output = Result<(), String>> + Send;

    // Provided methods
    fn holds(&self, cx: &Cx, path: &str) -> impl Future<Output = bool> + Send { ... }
    fn delete(
        &self,
        cx: &Cx,
        path: &str,
    ) -> impl Future<Output = Result<(), String>> + Send { ... }
}
Expand description

Stores one uploaded file and names the value a record stores; installed once per panel with Panel::uploads.

Each method takes the request it serves, so a store reads who asks: an uploader that keeps tenants apart names each file under tenant_id(cx), and answers holds only for a path under the request’s tenant, so no record of one tenant stores, carries or deletes another’s file.

filename arrives sanitized to a non-empty basename without directories or control characters; bytes are bounded by the panel’s Limits::body_bytes. The framework stores the returned path verbatim. A file a file field’s extensions or max_bytes refuse never reaches store.

  • store returns the value to save. An Err(reason) is shown to the user inline as <Label> could not be uploaded: <reason>, so keep the reason free of paths and driver messages.
  • What a file field stores comes from a file part only: store’s answer, the stored value on an untouched edit, or nothing when cleared. A path typed into the form is never stored.
  • Keeping a file across a failed submit. When a form re-renders with errors, the file input is empty again. The form carries the path store just returned, under a tag the panel signs for that field and that browser’s CSRF cookie, so a client cannot carry a path the field’s rules never passed. The panel reuses it on the next submit only when the tag holds and Uploader::holds(path) answers true. The default answers false, so the user uploads again. The tag’s key derives from the router’s cookie Key, else from one drawn per process, so without a shared Key a carry reaches only the instance that minted it. Answer true only for a path your store could have written and still holds inside its own root; checking whether an arbitrary path exists would let a client pick any file. A DirUploader answers true for a URL spelled as its store spells one, naming a file in its directory.
  • Editing. The edit form shows the stored file as a link and an empty file input. Leaving it empty keeps the file; the “Remove the current file” checkbox (clear_<field>) empties the field. The input is required only while nothing is stored, and clearing a required field fails validation, so declare the record-form field #[form(optional)] when a record may lose its file. A Field::files list shows a row per stored file, each posting its path back under keep_<field> to order the list, and a remove checkbox posting it under clear_<field>, the only way a save drops a stored file; the rows keep the order they post in, chosen files are stored after them, and a refused submission’s uploads are carried beside them as a single field’s is.
  • Links. The stored value renders as a link, on the edit form and in a FileColumn on the detail page, only when it is a root-relative path (/uploads/a.png, not //host) or an http(s) URL; anything else renders as text.
  • Serving. Panel::uploads_dir serves its directory only at signed URLs that expire, which the panel’s pages render; Panel::serve_dir(path, dir) serves a directory publicly unless the path sits under the panel prefix, which the auth gate covers.

Required Methods§

Source

fn store( &self, cx: &Cx, filename: &str, bytes: &[u8], ) -> impl Future<Output = Result<String, String>> + Send

Stores bytes and returns the value to store; the Err string renders inside the field’s inline error, so it must be user-actionable.

Source

fn keep( &self, cx: &Cx, path: &str, ) -> impl Future<Output = Result<(), String>> + Send

Confirms that the write about to commit stores path; the default does nothing.

The panel calls it inside the write’s transaction, before the commit, for each new path a file field’s create or edit stores and holds answers true for. A store that deletes what nothing kept, as DirUploader::expire_pending does, keeps path here. An Err(reason) fails the write, so a file is never deleted under a committed record.

It has no default: an uploader wrapping another forwards it, or the inner one deletes the files of saved records. A store that deletes nothing answers Ok(()).

Provided Methods§

Source

fn holds(&self, cx: &Cx, path: &str) -> impl Future<Output = bool> + Send

Answers true only for a path this store could have written and still resolves inside its own root; the default answers false.

A file field carries a refused submission’s pending upload back through a hidden keep_<field>, so this is what decides which paths a record may point at: a store that answers true for a file it never wrote lets a forged carry point the record at it.

Source

fn delete( &self, cx: &Cx, path: &str, ) -> impl Future<Output = Result<(), String>> + Send

Deletes the file at path, which a committed write no longer stores; the default keeps every file.

The panel calls it after the write commits, once per path, and only for a path holds answers true for: a record’s files when a delete removes its row (a soft delete keeps them until the force delete), and a file field’s previous file when an edit clears or replaces it. An Err(reason) is logged; the write stands. A path two records share goes with the first that lets it go.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§