feat: add web upload for EML/MBOX files #260

This commit is contained in:
rustmailer
2026-06-26 17:49:35 +08:00
parent db36272bae
commit cdf27f2dd4
35 changed files with 2910 additions and 44 deletions
+127
View File
@@ -0,0 +1,127 @@
//
// Copyright (c) 2025-2026 rustmailer.com (https://rustmailer.com)
//
// This file is part of the Bichon Email Archiving Project
use crate::database::MemDbModel;
use crate::import::{ImportProgress, ImportStatus};
use serde::{Deserialize, Serialize};
/// Maximum number of import history entries to keep per user.
pub const MAX_HISTORY_PER_USER: usize = 5;
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "web-api", derive(poem_openapi::Object))]
pub struct ImportHistory {
/// Composite key: "{user_id}:{import_id}"
pub id: String,
pub user_id: u64,
pub import_id: String,
pub account_id: u64,
pub folder: String,
pub format: String,
pub status: String,
pub total: usize,
pub success: usize,
pub duplicates: usize,
pub failed: usize,
pub failed_details: Vec<crate::import::FailedItemDetail>,
/// Unix timestamp in milliseconds.
pub created_at: i64,
}
impl MemDbModel for ImportHistory {
fn collection() -> &'static str {
"import_history"
}
fn key(&self) -> String {
self.id.clone()
}
}
impl ImportHistory {
pub fn from_progress(
user_id: u64,
import_id: &str,
account_id: u64,
folder: &str,
progress: &ImportProgress,
) -> Self {
Self {
id: format!("{}:{}", user_id, import_id),
user_id,
import_id: import_id.to_string(),
account_id,
folder: folder.to_string(),
format: progress.format.clone(),
status: match progress.status {
ImportStatus::Pending => "pending",
ImportStatus::Processing => "processing",
ImportStatus::Completed => "completed",
ImportStatus::Failed => "failed",
}
.to_string(),
total: progress.total,
success: progress.success,
duplicates: progress.duplicates,
failed: progress.failed,
failed_details: progress.failed_details.clone(),
created_at: crate::utc_now!(),
}
}
}
/// Prune old entries for a user so only the latest `MAX_HISTORY_PER_USER` remain.
pub fn prune_user_history(user_id: u64) -> crate::error::BichonResult<()> {
use crate::database::manager::DB_MANAGER;
use crate::database::batch_delete_impl;
use crate::raise_error;
use crate::error::code::ErrorCode;
let db = DB_MANAGER.db();
let coll = db.collection(ImportHistory::collection());
let prefix = format!("{}:", user_id);
let mut entries: Vec<ImportHistory> = coll
.scan_prefix(&prefix)
.map_err(|e| raise_error!(format!("{:#?}", e), ErrorCode::InternalError))?;
if entries.len() <= MAX_HISTORY_PER_USER {
return Ok(());
}
// Sort by created_at descending (newest first), keep the first N
entries.sort_by(|a, b| b.created_at.cmp(&a.created_at));
let to_delete: Vec<String> = entries
.iter()
.skip(MAX_HISTORY_PER_USER)
.map(|e| e.id.clone())
.collect();
if !to_delete.is_empty() {
batch_delete_impl::<ImportHistory>(db, to_delete)?;
}
Ok(())
}
/// Save an import history record and prune old entries for the user.
pub fn save_import_history(
user_id: u64,
account_id: u64,
folder: &str,
progress: &ImportProgress,
) {
use crate::database::manager::DB_MANAGER;
use crate::database::upsert_impl;
let entry = ImportHistory::from_progress(user_id, &progress.import_id, account_id, folder, progress);
let db = DB_MANAGER.db();
if let Err(e) = upsert_impl::<ImportHistory>(db, entry) {
tracing::error!("Failed to save import history: {:?}", e);
return;
}
if let Err(e) = prune_user_history(user_id) {
tracing::warn!("Failed to prune import history: {:?}", e);
}
}
+550 -14
View File
@@ -18,7 +18,15 @@
//use poem_openapi::Object;
pub mod history;
pub mod reader;
pub use history::ImportHistory;
use serde::{Deserialize, Serialize};
use std::{
collections::HashMap,
path::Path,
sync::RwLock,
};
use crate::{
base64_decode_url_safe,
@@ -27,15 +35,20 @@ use crate::{
cache::imap::mailbox::{Attribute, AttributeEnum, MailBox},
envelope::extractor::extract_envelope_from_eml,
error::{BichonResult, code::ErrorCode},
settings::dir::DATA_DIR_MANAGER,
utils::create_hash,
},
raise_error,
};
/// Skip individual emails larger than this after decoding (100 MB).
/// Maximum byte size of an individual email message after splitting (100 MB).
const MAX_SINGLE_EML_BYTES: usize = 100 * 1024 * 1024;
#[derive(Debug, Clone, Default, Eq, PartialEq, Serialize, Deserialize)]
/// Max file size accepted via the web upload endpoint.
pub const MAX_WEB_EML_BYTES: usize = 100 * 1024 * 1024; // 100 MB
pub const MAX_WEB_MBOX_BYTES: usize = 1024 * 1024 * 1024; // 1 GB
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[cfg_attr(feature = "web-api", derive(poem_openapi::Object))]
pub struct BatchEmlRequest {
pub account_id: u64,
@@ -46,24 +59,26 @@ pub struct BatchEmlRequest {
#[derive(Debug, Clone, Default, Eq, PartialEq, Serialize, Deserialize)]
#[cfg_attr(feature = "web-api", derive(poem_openapi::Object))]
pub struct FailedEmlDetail {
/// The 0-based index of the failed EML in the request list
pub struct FailedItemDetail {
/// The index (0-based) of the failed item.
pub index: usize,
/// The error message that caused the import to fail
/// The error message that caused the import to fail.
pub error_message: String,
}
#[derive(Debug, Clone, Default, Eq, PartialEq, Serialize, Deserialize)]
#[cfg_attr(feature = "web-api", derive(poem_openapi::Object))]
pub struct BatchEmlResult {
/// Total number of emails processed
/// Total number of emails processed.
pub total: usize,
/// Number of emails successfully imported
/// Number of emails successfully imported.
pub success: usize,
/// Number of emails failed to import
/// Number of duplicate emails skipped (content hash already existed).
pub duplicates: usize,
/// Number of emails failed to import.
pub failed: usize,
/// A list of details for failed imports
pub failed_details: Vec<FailedEmlDetail>,
/// A list of details for failed imports.
pub failed_details: Vec<FailedItemDetail>,
}
pub struct ImportEmls;
@@ -116,7 +131,7 @@ impl ImportEmls {
let account_id = account.id;
let mut success_count = 0;
let mut failed_details: Vec<FailedEmlDetail> = Vec::new(); // Store failure details
let mut failed_details: Vec<FailedItemDetail> = Vec::new(); // Store failure details
let total = request.emls.len();
let mut index: usize = 0;
@@ -127,7 +142,7 @@ impl ImportEmls {
let error_msg =
format!("Failed to decode base64 EML at index {}: {:?}", index, e);
tracing::error!("{}", error_msg);
failed_details.push(FailedEmlDetail {
failed_details.push(FailedItemDetail {
index,
error_message: error_msg,
});
@@ -144,7 +159,7 @@ impl ImportEmls {
index, size_mb,
);
tracing::warn!("{}", error_msg);
failed_details.push(FailedEmlDetail {
failed_details.push(FailedItemDetail {
index,
error_message: error_msg,
});
@@ -162,7 +177,7 @@ impl ImportEmls {
index, e
);
tracing::error!("{}", error_msg);
failed_details.push(FailedEmlDetail {
failed_details.push(FailedItemDetail {
index,
error_message: error_msg,
});
@@ -178,8 +193,529 @@ impl ImportEmls {
Ok(BatchEmlResult {
total,
success: success_count,
duplicates: 0,
failed: failed_count,
failed_details, // Return the list of failure details
})
}
}
// ── File upload import ──────────────────────────────────────────────
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[cfg_attr(feature = "web-api", derive(poem_openapi::Enum))]
pub enum ImportStatus {
Pending,
Processing,
Completed,
Failed,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "web-api", derive(poem_openapi::Object))]
pub struct ImportProgress {
pub import_id: String,
pub status: ImportStatus,
pub format: String,
pub total: usize,
pub success: usize,
pub duplicates: usize,
pub failed: usize,
pub failed_details: Vec<FailedItemDetail>,
}
static PROGRESS_STORE: std::sync::LazyLock<RwLock<HashMap<String, ImportProgress>>> =
std::sync::LazyLock::new(|| RwLock::new(HashMap::new()));
pub fn get_import_progress(import_id: &str) -> Option<ImportProgress> {
PROGRESS_STORE.read().ok()?.get(import_id).cloned()
}
pub fn update_progress(import_id: &str, progress: ImportProgress) {
if let Ok(mut store) = PROGRESS_STORE.write() {
store.insert(import_id.to_string(), progress);
}
}
/// Check free disk space (in bytes) on the temp directory's filesystem.
pub fn check_temp_disk_space() -> BichonResult<u64> {
use sysinfo::Disks;
let disks = Disks::new_with_refreshed_list();
let temp_path = &DATA_DIR_MANAGER.temp_dir;
// Use the canonical path so we can match mount points
let canonical = std::fs::canonicalize(temp_path).unwrap_or_else(|_| temp_path.clone());
for disk in disks.list() {
if canonical.starts_with(disk.mount_point()) {
return Ok(disk.available_space());
}
}
// Fallback: if we can't find the mount point, report plenty of space
Ok(u64::MAX)
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FileFormat {
Eml,
Mbox,
}
pub fn detect_format(bytes: &[u8], file_name: &str) -> Option<FileFormat> {
// MBOX files start with "From " (note the trailing space after From)
if bytes.starts_with(b"From ") {
// Double-check: look for a valid date after the first "From " line
// MBOX format: "From sender@host DayOfWeek Mon DD HH:MM:SS YYYY"
if let Some(first_newline) = bytes.iter().position(|&b| b == b'\n') {
let from_line = std::str::from_utf8(&bytes[..first_newline]).unwrap_or("");
let parts: Vec<&str> = from_line.split_whitespace().collect();
if parts.len() >= 7 {
return Some(FileFormat::Mbox);
}
}
}
// EML: starts with a header line or "Return-Path:", "Received:", "From:", "Date:", etc.
// Or check extension
if bytes.starts_with(b"Return-Path:")
|| bytes.starts_with(b"Received:")
|| bytes.starts_with(b"Date:")
|| bytes.starts_with(b"From:")
|| bytes.starts_with(b"Subject:")
|| bytes.starts_with(b"To:")
|| bytes.starts_with(b"Message-ID:")
{
return Some(FileFormat::Eml);
}
// Fallback: check file extension
let lower = file_name.to_lowercase();
if lower.ends_with(".eml") {
Some(FileFormat::Eml)
} else if lower.ends_with(".mbox") {
Some(FileFormat::Mbox)
} else {
None
}
}
/// Check whether `bytes` looks like a text file by inspecting the first chunk.
/// Returns `true` if it passes, `false` if it appears to be binary (video, executable, etc.).
///
/// Email files (EML/MBOX) are text-based with printable ASCII, whitespace, and
/// optional UTF-8. Binary files like video contain null bytes and high ratios of
/// non-printable control characters.
pub fn detect_text_file(bytes: &[u8]) -> bool {
let check_len = bytes.len().min(8192);
if check_len == 0 {
return false;
}
let sample = &bytes[..check_len];
// Null bytes are a strong binary indicator
if sample.contains(&0x00) {
return false;
}
let mut printable = 0usize;
let mut total = 0usize;
let mut i = 0;
while i < sample.len() {
total += 1;
let b = sample[i];
if b.is_ascii_graphic() || b.is_ascii_whitespace() {
// Printable ASCII + whitespace (space, tab, CR, LF)
printable += 1;
} else if b == 0x1b {
// ESC — common in terminal sequences, rare in email
// Count as printable to avoid false positives
printable += 1;
} else if b >= 0x80 {
// UTF-8 continuation or multi-byte lead byte — allow.
// Check that we have a valid UTF-8 sequence ahead.
let seq_len = match b {
b if b & 0xE0 == 0xC0 => 2,
b if b & 0xF0 == 0xE0 => 3,
b if b & 0xF8 == 0xF0 => 4,
_ => 0,
};
if seq_len > 0 && i + seq_len <= sample.len() {
let valid = std::str::from_utf8(&sample[i..i + seq_len]).is_ok();
if valid {
printable += 1;
i += 1; // lead byte counted, continuations counted in loop
}
// if invalid, don't count as printable
}
// standalone continuation byte — not printable
}
// Other control characters (0x01-0x1F except whitespace/Esc) are not counted as printable
i += 1;
}
// Require at least 90% printable characters
printable as f64 / total as f64 >= 0.90
}
/// Validate that the target account exists, is enabled, and is NoSync type.
fn validate_import_account(account_id: u64) -> BichonResult<AccountModel> {
let account = AccountModel::check_account_exists(account_id)?;
if !account.enabled {
return Err(raise_error!(
"The account is disabled.".into(),
ErrorCode::InvalidParameter
));
}
if !matches!(account.account_type, AccountType::NoSync) {
return Err(raise_error!(
"Import is only allowed for NoSync accounts. IMAP accounts sync from the server.".into(),
ErrorCode::InvalidParameter
));
}
Ok(account)
}
/// Resolve or create a mailbox/folder for the given account.
fn resolve_mailbox(account: &AccountModel, folder: &str) -> BichonResult<u64> {
match account.account_type {
AccountType::IMAP => {
// Shouldn't reach here (validated above), but handle gracefully
let all_mailboxes = MailBox::list_all(account.id)?;
let mailbox = all_mailboxes.into_iter().find(|m| m.name == folder);
match mailbox {
Some(m) => Ok(m.id),
None => Err(raise_error!(
format!("Mail folder '{}' not found.", folder).into(),
ErrorCode::ResourceNotFound
)),
}
}
AccountType::NoSync => {
let mailbox = MailBox {
id: create_hash(account.id, folder),
account_id: account.id,
name: folder.to_string(),
delimiter: Some("/".to_string()),
attributes: vec![Attribute {
attr: AttributeEnum::Extension,
extension: Some("CreatedByBichon".into()),
}],
exists: 0,
unseen: None,
uid_next: None,
uid_validity: None,
highest_uid: None,
};
let mailbox_id = mailbox.id;
MailBox::batch_upsert(&[mailbox])?;
Ok(mailbox_id)
}
}
}
/// Process an uploaded file (EML or MBOX) and import into the given account/folder.
/// This runs synchronously and should be spawned on a background thread.
///
/// For MBOX files, the file is memory-mapped via `memmap2` and messages are yielded
/// one at a time — the full file is never loaded into RAM. Individual messages
/// exceeding `MAX_SINGLE_EML_BYTES` (100 MB) are skipped.
pub fn process_uploaded_file(
import_id: &str,
file_path: &Path,
file_name: &str,
account_id: u64,
folder: &str,
user_id: u64,
) {
let account = match validate_import_account(account_id) {
Ok(a) => a,
Err(e) => {
let progress = ImportProgress {
import_id: import_id.to_string(),
status: ImportStatus::Failed,
format: "unknown".to_string(),
total: 0,
success: 0,
duplicates: 0,
failed: 0,
failed_details: vec![FailedItemDetail {
index: 0,
error_message: format!("Account validation failed: {:?}", e),
}],
};
update_progress(import_id, progress.clone());
history::save_import_history(user_id, account_id, folder, &progress);
return;
}
};
let mailbox_id = match resolve_mailbox(&account, folder) {
Ok(id) => id,
Err(e) => {
let progress = ImportProgress {
import_id: import_id.to_string(),
status: ImportStatus::Failed,
format: "unknown".to_string(),
total: 0,
success: 0,
duplicates: 0,
failed: 0,
failed_details: vec![FailedItemDetail {
index: 0,
error_message: format!("Mailbox resolution failed: {:?}", e),
}],
};
update_progress(import_id, progress.clone());
history::save_import_history(user_id, account_id, folder, &progress);
return;
}
};
// Read a small prefix for format detection
let format = match detect_format_from_file(file_path, file_name) {
Ok(f) => f,
Err(e) => {
let progress = ImportProgress {
import_id: import_id.to_string(),
status: ImportStatus::Failed,
format: "unknown".to_string(),
total: 0,
success: 0,
duplicates: 0,
failed: 0,
failed_details: vec![FailedItemDetail {
index: 0,
error_message: format!("{:?}", e),
}],
};
update_progress(import_id, progress.clone());
history::save_import_history(user_id, account_id, folder, &progress);
let _ = std::fs::remove_file(file_path);
return;
}
};
match format {
FileFormat::Eml => process_eml_file(import_id, file_path, account_id, mailbox_id, user_id, folder),
FileFormat::Mbox => process_mbox_file(import_id, file_path, account_id, mailbox_id, user_id, folder),
}
}
/// Detect format from a file by reading only the first few KB.
fn detect_format_from_file(file_path: &Path, file_name: &str) -> BichonResult<FileFormat> {
use std::io::Read;
let mut file = std::fs::File::open(file_path).map_err(|e| {
raise_error!(format!("Failed to open file: {}", e), ErrorCode::InternalError)
})?;
let mut buf = vec![0u8; 8192];
let n = file.read(&mut buf).unwrap_or(0);
buf.truncate(n);
detect_format(&buf, file_name).ok_or_else(|| {
raise_error!(
"Unknown file format. Supported: .eml, .mbox".into(),
ErrorCode::InvalidParameter
)
})
}
/// Process a single EML file. The file is at most `MAX_WEB_EML_BYTES` (100 MB),
/// so reading it entirely is safe.
fn process_eml_file(
import_id: &str,
file_path: &Path,
account_id: u64,
mailbox_id: u64,
user_id: u64,
folder: &str,
) {
let file_bytes = match std::fs::read(file_path) {
Ok(b) => b,
Err(e) => {
fail_progress(import_id, "eml", &format!("Failed to read file: {}", e), user_id, account_id, folder);
let _ = std::fs::remove_file(file_path);
return;
}
};
let total = 1;
update_progress(import_id, ImportProgress {
import_id: import_id.to_string(),
status: ImportStatus::Processing,
format: "eml".to_string(),
total,
success: 0,
duplicates: 0,
failed: 0,
failed_details: vec![],
});
let (success_count, failed_details) = process_single_eml(&file_bytes, 0, account_id, mailbox_id);
// Clean up
let _ = std::fs::remove_file(file_path);
let final_progress = ImportProgress {
import_id: import_id.to_string(),
status: ImportStatus::Completed,
format: "eml".to_string(),
total,
success: success_count,
duplicates: 0,
failed: failed_details.len(),
failed_details,
};
history::save_import_history(user_id, account_id, folder, &final_progress);
update_progress(import_id, final_progress);
}
/// Process an MBOX file using memory-mapped I/O. Messages are yielded one at a
/// time by `MboxReader` — the full file is never loaded into RAM.
fn process_mbox_file(
import_id: &str,
file_path: &Path,
account_id: u64,
mailbox_id: u64,
user_id: u64,
folder: &str,
) {
let mbox = match reader::MboxFile::from_file(file_path) {
Ok(m) => m,
Err(e) => {
fail_progress(import_id, "mbox", &format!("Failed to open MBOX file: {}", e), user_id, account_id, folder);
let _ = std::fs::remove_file(file_path);
return;
}
};
// First pass: count total messages (MboxReader is lazy, so this is O(n) but cheap)
let total = mbox.iter().count();
update_progress(import_id, ImportProgress {
import_id: import_id.to_string(),
status: ImportStatus::Processing,
format: "mbox".to_string(),
total,
success: 0,
duplicates: 0,
failed: 0,
failed_details: vec![],
});
let mut success_count = 0usize;
let mut failed_details: Vec<FailedItemDetail> = Vec::new();
for (index, entry) in mbox.iter().enumerate() {
let eml_bytes = entry.data;
if eml_bytes.len() > MAX_SINGLE_EML_BYTES {
let size_mb = eml_bytes.len() as f64 / 1024.0 / 1024.0;
failed_details.push(FailedItemDetail {
index,
error_message: format!(
"Email at index {} is {:.1} MB (limit {} MB). Skipping.",
index,
size_mb,
MAX_SINGLE_EML_BYTES / 1024 / 1024
),
});
continue;
}
match futures::executor::block_on(extract_envelope_from_eml(eml_bytes, account_id, mailbox_id)) {
Ok(_) => {
success_count += 1;
}
Err(e) => {
failed_details.push(FailedItemDetail {
index,
error_message: format!("{:?}", e),
});
}
};
// Update progress every 100 items
if index % 100 == 0 || index == total - 1 {
update_progress(import_id, ImportProgress {
import_id: import_id.to_string(),
status: ImportStatus::Processing,
format: "mbox".to_string(),
total,
success: success_count,
duplicates: 0,
failed: failed_details.len(),
failed_details: failed_details.clone(),
});
}
}
// Clean up temp file (drop the mmap first — MboxFile owns it)
drop(mbox);
let _ = std::fs::remove_file(file_path);
let final_progress = ImportProgress {
import_id: import_id.to_string(),
status: ImportStatus::Completed,
format: "mbox".to_string(),
total,
success: success_count,
duplicates: 0,
failed: failed_details.len(),
failed_details,
};
history::save_import_history(user_id, account_id, folder, &final_progress);
update_progress(import_id, final_progress);
}
/// Process a single EML byte slice and return (success_count, failed_details).
fn process_single_eml(
eml_bytes: &[u8],
index: usize,
account_id: u64,
mailbox_id: u64,
) -> (usize, Vec<FailedItemDetail>) {
if eml_bytes.len() > MAX_SINGLE_EML_BYTES {
let size_mb = eml_bytes.len() as f64 / 1024.0 / 1024.0;
return (0, vec![FailedItemDetail {
index,
error_message: format!(
"Email is {:.1} MB (limit {} MB). Skipping.",
size_mb,
MAX_SINGLE_EML_BYTES / 1024 / 1024
),
}]);
}
match futures::executor::block_on(extract_envelope_from_eml(eml_bytes, account_id, mailbox_id)) {
Ok(_) => (1, vec![]),
Err(e) => (0, vec![FailedItemDetail {
index,
error_message: format!("{:?}", e),
}]),
}
}
/// Record a fatal failure and save history.
fn fail_progress(
import_id: &str,
format: &str,
message: &str,
user_id: u64,
account_id: u64,
folder: &str,
) {
let progress = ImportProgress {
import_id: import_id.to_string(),
status: ImportStatus::Failed,
format: format.to_string(),
total: 0,
success: 0,
duplicates: 0,
failed: 0,
failed_details: vec![FailedItemDetail {
index: 0,
error_message: message.to_string(),
}],
};
update_progress(import_id, progress.clone());
history::save_import_history(user_id, account_id, folder, &progress);
}
+206
View File
@@ -0,0 +1,206 @@
//
// Copyright (c) 2025-2026 rustmailer.com (https://rustmailer.com)
//
// This file is part of the Bichon Email Archiving Project
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
use memmap2::Mmap;
use std::fs;
use std::io;
use std::path::Path;
/// Memory-mapped MBOX file. Messages are yielded one at a time without
/// loading the entire file into RAM.
pub struct MboxFile {
map: Mmap,
}
impl MboxFile {
pub fn from_file(name: &Path) -> io::Result<Self> {
let file = fs::File::open(name)?;
let metadata = file.metadata()?;
if metadata.len() == 0 {
return Err(io::Error::new(
io::ErrorKind::InvalidData,
"Empty MBOX file",
));
}
let map = unsafe { Mmap::map(&file)? };
Ok(Self { map })
}
pub fn iter(&self) -> MboxReader<'_> {
MboxReader::new(&self.map)
}
}
pub struct Entry<'a> {
pub offset: usize,
pub data: &'a [u8],
}
pub struct MboxReader<'a> {
data: &'a [u8],
len: usize,
scan_pos: usize,
body_start: Option<usize>,
}
impl<'a> MboxReader<'a> {
fn new(data: &'a [u8]) -> Self {
Self {
data,
len: data.len(),
scan_pos: 0,
body_start: None,
}
}
fn is_from_line(&self, i: usize) -> bool {
if i + 5 > self.len {
return false;
}
if i == 0 {
&self.data[0..5] == b"From "
} else {
self.data[i - 1] == b'\n' && &self.data[i..i + 5] == b"From "
}
}
fn skip_from_line(&self, mut i: usize) -> usize {
while i < self.len && self.data[i] != b'\n' {
i += 1;
}
if i < self.len {
i += 1;
}
i
}
}
impl<'a> Iterator for MboxReader<'a> {
type Item = Entry<'a>;
fn next(&mut self) -> Option<Self::Item> {
while self.scan_pos < self.len {
if self.is_from_line(self.scan_pos) {
let from_pos = self.scan_pos;
let body_pos = self.skip_from_line(from_pos);
if let Some(start) = self.body_start {
let entry = Entry {
offset: start,
data: &self.data[start..from_pos],
};
self.body_start = Some(body_pos);
self.scan_pos = body_pos;
return Some(entry);
} else {
self.body_start = Some(body_pos);
self.scan_pos = body_pos;
continue;
}
}
self.scan_pos += 1;
}
if let Some(start) = self.body_start.take() {
return Some(Entry {
offset: start,
data: &self.data[start..self.len],
});
}
None
}
}
#[cfg(test)]
mod tests {
use super::*;
fn collect_entries(data: &[u8]) -> Vec<&[u8]> {
let reader = MboxReader::new(data);
reader.map(|e| e.data).collect()
}
#[test]
fn two_mails() {
let data = b"From a\nmail1\nFrom b\nmail2\n";
let e = collect_entries(data);
assert_eq!(e, vec![b"mail1\n", b"mail2\n"]);
}
#[test]
fn no_trailing_newline() {
let data = b"From a\nmail1";
let e = collect_entries(data);
assert_eq!(e, vec![b"mail1"]);
}
#[test]
fn from_inside_body() {
let data = b"From a\nhello\nFrom is here\nbye\n";
let e = collect_entries(data);
assert_eq!(e.len(), 2);
}
#[test]
fn inline_from_not_separator() {
let data = b"From a\nhello From world\n";
let e = collect_entries(data);
assert_eq!(e.len(), 1);
}
#[test]
fn realistic_mbox() {
let data = b"From a\nH:1\n\nbody1\nFrom b\nH:2\n\nbody2\n";
let e = collect_entries(data);
assert_eq!(e.len(), 2);
}
#[test]
fn empty_body() {
let data = b"From a\nFrom b\nbody\n";
let e = collect_entries(data);
assert_eq!(e[0], b"");
assert_eq!(e[1], b"body\n");
}
#[test]
fn only_from_line() {
let data = b"From a\n";
let e = collect_entries(data);
assert_eq!(e.len(), 1);
assert_eq!(e[0], b"");
}
#[test]
fn windows_newlines() {
let data = b"From a\r\nbody\r\nFrom b\r\nbody2\r\n";
let e = collect_entries(data);
assert_eq!(e.len(), 2);
}
#[test]
fn many_small_mails() {
let mut data = Vec::new();
for _ in 0..1000 {
data.extend_from_slice(b"From a\nx\n");
}
let e = collect_entries(&data);
assert_eq!(e.len(), 1000);
}
}
+11
View File
@@ -328,6 +328,17 @@ pub struct Settings {
/// OIDC redirect URI (must match what's registered with the IdP).
#[clap(long, env, help = "OpenID Connect redirect URI")]
pub bichon_oidc_redirect_uri: Option<String>,
/// Maximum HTTP request body size in MB for file uploads (default: 1100 MB).
/// Requests exceeding this limit are rejected at the framework level before
/// the application reads the body, preventing memory exhaustion attacks.
#[clap(
long,
default_value = "1100",
env,
help = "Maximum HTTP request body size in MB for file uploads"
)]
pub bichon_upload_body_limit_mb: u64,
}
impl Settings {
+3
View File
@@ -65,6 +65,8 @@ pub struct SystemConfigurations {
pub bichon_oidc_issuer_url: Option<String>,
pub bichon_oidc_client_id: Option<String>,
pub bichon_oidc_redirect_uri: Option<String>,
pub bichon_upload_body_limit_mb: u64,
}
impl From<&Settings> for SystemConfigurations {
@@ -103,6 +105,7 @@ impl From<&Settings> for SystemConfigurations {
bichon_oidc_issuer_url: s.bichon_oidc_issuer_url.clone(),
bichon_oidc_client_id: s.bichon_oidc_client_id.clone(),
bichon_oidc_redirect_uri: s.bichon_oidc_redirect_uri.clone(),
bichon_upload_body_limit_mb: s.bichon_upload_body_limit_mb,
}
}
}