Skip to main content

FileUploadHandler

Struct FileUploadHandler 

Source
pub struct FileUploadHandler { /* private fields */ }
Expand description

FileUploadHandler processes file uploads

Handles file upload operations including validation, storage, and cleanup of temporary files.

Implementations§

Source§

impl FileUploadHandler

Source

pub fn new(upload_dir: PathBuf) -> Self

Create a new FileUploadHandler

§Arguments
  • upload_dir - Directory where uploaded files will be stored
§Examples
use reinhardt_http::upload::FileUploadHandler;
use std::path::PathBuf;

let handler = FileUploadHandler::new(PathBuf::from("/tmp/uploads"));
assert_eq!(handler.max_size(), 10 * 1024 * 1024); // 10MB default
Source

pub fn with_max_size(self, max_size: usize) -> Self

Set maximum file size

§Examples
use reinhardt_http::upload::FileUploadHandler;
use std::path::PathBuf;

let handler = FileUploadHandler::new(PathBuf::from("/tmp/uploads"))
    .with_max_size(5 * 1024 * 1024); // 5MB
assert_eq!(handler.max_size(), 5 * 1024 * 1024);
Source

pub fn with_allowed_extensions(self, extensions: Vec<String>) -> Self

Set allowed file extensions

§Examples
use reinhardt_http::upload::FileUploadHandler;
use std::path::PathBuf;

let handler = FileUploadHandler::new(PathBuf::from("/tmp/uploads"))
    .with_allowed_extensions(vec!["jpg".to_string(), "png".to_string()]);
Source

pub fn with_checksum_verification(self, enabled: bool) -> Self

Enable checksum verification

§Examples
use reinhardt_http::upload::FileUploadHandler;
use std::path::PathBuf;

let handler = FileUploadHandler::new(PathBuf::from("/tmp/uploads"))
    .with_checksum_verification(true);
Source

pub fn with_allowed_mime_types(self, mime_types: Vec<String>) -> Self

Set allowed MIME types

§Examples
use reinhardt_http::upload::FileUploadHandler;
use std::path::PathBuf;

let handler = FileUploadHandler::new(PathBuf::from("/tmp/uploads"))
    .with_allowed_mime_types(vec![
        "image/jpeg".to_string(),
        "image/png".to_string()
    ]);
Source

pub fn max_size(&self) -> usize

Get the maximum file size

Source

pub fn upload_dir(&self) -> &Path

Get the upload directory

Source

pub fn calculate_checksum(&self, content: &[u8]) -> String

Calculate SHA-256 checksum of file content

§Examples
use reinhardt_http::upload::FileUploadHandler;
use std::path::PathBuf;

let handler = FileUploadHandler::new(PathBuf::from("/tmp/uploads"));
let checksum = handler.calculate_checksum(b"test data");
assert_eq!(checksum.len(), 64); // SHA-256 produces 64 hex characters
Source

pub fn verify_file_checksum( &self, content: &[u8], expected_checksum: &str, ) -> Result<(), FileUploadError>

Verify file checksum

§Examples
use reinhardt_http::upload::FileUploadHandler;
use std::path::PathBuf;

let handler = FileUploadHandler::new(PathBuf::from("/tmp/uploads"));
let content = b"test data";
let checksum = handler.calculate_checksum(content);
assert!(handler.verify_file_checksum(content, &checksum).is_ok());
Source

pub fn detect_mime_type(&self, content: &[u8]) -> Option<String>

Detect MIME type from file content

Basic MIME type detection based on file signatures (magic numbers).

§Examples
use reinhardt_http::upload::FileUploadHandler;
use std::path::PathBuf;

let handler = FileUploadHandler::new(PathBuf::from("/tmp/uploads"));

// PNG signature
let png_data = vec![0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A];
assert_eq!(handler.detect_mime_type(&png_data), Some("image/png".to_string()));

// JPEG signature
let jpeg_data = vec![0xFF, 0xD8, 0xFF];
assert_eq!(handler.detect_mime_type(&jpeg_data), Some("image/jpeg".to_string()));
Source

pub fn handle_upload( &self, field_name: &str, filename: &str, content: &[u8], ) -> Result<String, FileUploadError>

Handle a file upload

§Arguments
  • field_name - Name of the form field
  • filename - Original filename
  • content - File content as bytes
§Returns

Returns the path to the saved file

§Examples
use reinhardt_http::upload::FileUploadHandler;
use std::path::PathBuf;

let handler = FileUploadHandler::new(PathBuf::from("/tmp/uploads"));
let result = handler.handle_upload("avatar", "photo.jpg", b"image data");
assert!(result.is_ok());
Source

pub fn handle_upload_with_checksum( &self, field_name: &str, filename: &str, content: &[u8], expected_checksum: &str, ) -> Result<String, FileUploadError>

Handle upload with checksum verification

§Examples
use reinhardt_http::upload::FileUploadHandler;
use std::path::PathBuf;

let handler = FileUploadHandler::new(PathBuf::from("/tmp/uploads"))
    .with_checksum_verification(true);

let content = b"test data";
let checksum = handler.calculate_checksum(content);
let result = handler.handle_upload_with_checksum(
    "file",
    "test.txt",
    content,
    &checksum
);
assert!(result.is_ok());
Source

pub fn delete_upload(&self, filename: &str) -> Result<(), FileUploadError>

Delete an uploaded file

§Examples
use reinhardt_http::upload::FileUploadHandler;
use std::path::PathBuf;

let handler = FileUploadHandler::new(PathBuf::from("/tmp/uploads"));
let result = handler.delete_upload("avatar_123456.jpg");
assert!(result.is_ok());

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> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more