Skip to main content

StoredFile

Struct StoredFile 

Source
pub struct StoredFile { /* private fields */ }
Available on crate feature fs only.
Expand description

One received file, in memory or in a temporary file.

Dropping it without persist deletes it (closing the handle first) and wipes memory-held bytes. Debug never prints the file name or the data.

§Examples

use std::io::Read;

let body = b"--X\r\nContent-Disposition: form-data; name=doc; filename=a.txt\r\n\r\nhello\r\n--X--";
let upload = form_net::read_multipart(b"multipart/form-data; boundary=X", &body[..])?;
let file = upload.file("doc").unwrap();
let mut text = String::new();
file.reader()?.read_to_string(&mut text).unwrap();
assert_eq!((text.as_str(), file.len(), file.is_in_memory()), ("hello", 5, true));

Implementations§

Source§

impl StoredFile

Source

pub fn name(&self) -> &str

The form field name.

Source

pub fn filename(&self) -> Option<FileName<'_>>

The client’s file name: a hint, never a path.

Source

pub fn content_type(&self) -> Option<ContentType<'_>>

The declared content type: a hint, never trusted.

Source

pub const fn len(&self) -> u64

The length in bytes.

Source

pub const fn is_empty(&self) -> bool

Whether the file is empty.

Source

pub const fn is_in_memory(&self) -> bool

Whether the file is held in memory.

Source

pub fn bytes(&self) -> Option<&[u8]>

The bytes, while the file is in memory.

Source

pub fn reader(&self) -> Result<StoredReader<'_>, FormError>

A reader over the file, in memory or on disk. It reads with positional reads, so several readers can exist at once.

§Errors

Io { kind } when the file is gone.

Source

pub fn read_to_vec(&self, max: u64) -> Result<Vec<u8>, FormError>

The whole file in a new vector, refusing a file longer than max before allocating.

§Errors

Limit { FileSize }; Io { kind }.

Source

pub fn path(&self) -> Option<&Path>

The temporary file’s path, in SpillMode::Named only.

Source

pub fn persist(self, dest: &Path) -> Result<(), FormError>

Moves the finished upload to dest, which the caller built. It never overwrites: a named file is hard-linked when dest is on the same file system and copied otherwise; anonymous and memory files are copied into dest opened with create_new. Whether to fsync dest and its directory is the caller’s choice. On error the temporary file is deleted like any dropped file.

§Errors

Store { DestinationExists }; Io { kind }.

Trait Implementations§

Source§

impl Debug for StoredFile

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Drop for StoredFile

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more
Source§

impl FromField for StoredFile

Source§

type Slot = Option<StoredFile>

Available on crate feature alloc only.
What has arrived so far.
Source§

fn accept_file( slot: &mut Self::Slot, file: FileValue<'_>, ) -> Result<(), ValueError>

Available on crate feature alloc only.
Takes one file. Read more
Source§

fn finish(slot: Self::Slot) -> Result<Self, ValueError>

Available on crate feature alloc only.
The value, once every field has arrived. Read more
Source§

fn accept_text(slot: &mut Self::Slot, value: &str) -> Result<(), ValueError>

Available on crate feature alloc only.
Takes one text value. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.