Skip to main content

UploadStream

Struct UploadStream 

Source
pub struct UploadStream<A> { /* private fields */ }
Expand description

What a streaming upload’s handler receives: the arguments, then the files one at a time, each in chunks as they arrive.

RouterExt::upload_stream hands one to its handler, which keeps no file in memory unless it collects the chunks itself. The arguments are read before the handler runs, because the runtime sends them as the first part of the body. The limit of the Upload holds while the handler reads: past it, the read fails with a RequestError whose status is 413 Payload Too Large, which the handler can return as its error.

§Examples

A handler that counts the bytes of each file without holding one in memory. Writing each chunk to a file works the same way.

use axum::Router;
use hypersig::{Patches, RequestError, RouterExt, Signals, Upload, UploadStream};
use serde::{Deserialize, Serialize};

#[derive(Signals, Serialize, Deserialize)]
struct Library {
    received: u64,
}

const VIDEO: Upload<Library> = Upload::post("/video").max_bytes(512 * 1024 * 1024);

async fn receive((): (), mut upload: UploadStream<()>) -> Result<Patches<Library>, RequestError> {
    let mut received = 0;
    while let Some(mut file) = upload.next_file().await? {
        while let Some(chunk) = file.chunk().await? {
            received += chunk.len() as u64;
        }
    }
    Ok(Library::received().patch(received).into())
}

let _app: Router = Router::new().upload_stream(VIDEO, receive);

Implementations§

Source§

impl<A> UploadStream<A>

Source

pub fn args(&self) -> &A

The arguments the call passed.

Source

pub async fn next_file( &mut self, ) -> Result<Option<StreamedFile<'_>>, RequestError>

The next file, in the order the visitor picked them, or None after the last one.

The part of the previous file that was not read is skipped, and so is a part other than a file, as RouterExt::upload skips it.

§Errors

Returns a RequestError when the body is malformed or longer than the upload’s limit.

Trait Implementations§

Source§

impl<A: Debug> Debug for UploadStream<A>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<A> !RefUnwindSafe for UploadStream<A>

§

impl<A> !UnwindSafe for UploadStream<A>

§

impl<A> Freeze for UploadStream<A>
where A: Freeze,

§

impl<A> Send for UploadStream<A>
where A: Send,

§

impl<A> Sync for UploadStream<A>
where A: Sync,

§

impl<A> Unpin for UploadStream<A>
where A: Unpin,

§

impl<A> UnsafeUnpin for UploadStream<A>
where A: UnsafeUnpin,

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 = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.