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
impl FileUploadHandler
Sourcepub fn new(upload_dir: PathBuf) -> Self
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 defaultSourcepub fn with_max_size(self, max_size: usize) -> Self
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);Sourcepub fn with_allowed_extensions(self, extensions: Vec<String>) -> Self
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()]);Sourcepub fn with_checksum_verification(self, enabled: bool) -> Self
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);Sourcepub fn with_allowed_mime_types(self, mime_types: Vec<String>) -> Self
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()
]);Sourcepub fn upload_dir(&self) -> &Path
pub fn upload_dir(&self) -> &Path
Get the upload directory
Sourcepub fn calculate_checksum(&self, content: &[u8]) -> String
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 charactersSourcepub fn verify_file_checksum(
&self,
content: &[u8],
expected_checksum: &str,
) -> Result<(), FileUploadError>
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());Sourcepub fn detect_mime_type(&self, content: &[u8]) -> Option<String>
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()));Sourcepub fn handle_upload(
&self,
field_name: &str,
filename: &str,
content: &[u8],
) -> Result<String, FileUploadError>
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 fieldfilename- Original filenamecontent- 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());Sourcepub fn handle_upload_with_checksum(
&self,
field_name: &str,
filename: &str,
content: &[u8],
expected_checksum: &str,
) -> Result<String, FileUploadError>
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());Sourcepub fn delete_upload(&self, filename: &str) -> Result<(), FileUploadError>
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§
impl Freeze for FileUploadHandler
impl RefUnwindSafe for FileUploadHandler
impl Send for FileUploadHandler
impl Sync for FileUploadHandler
impl Unpin for FileUploadHandler
impl UnsafeUnpin for FileUploadHandler
impl UnwindSafe for FileUploadHandler
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
Mutably borrows from an owned value. Read more
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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