Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions conf/config.dev.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ usage_db = "/app/data/usage.db"
# pkg_url = "https://postguard-main.cs.ru.nl/pkg"
# pkg_url = "https://pkg.staging.yivi.app"
pkg_url = "http://postguard-pkg:8087"
chunk_size = 5000000
7 changes: 7 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pub struct RawCryptifyConfig {
smtp_tls: Option<bool>,
allowed_origins: String,
pkg_url: String,
chunk_size: Option<u64>,
}

#[derive(Debug, Deserialize)]
Expand All @@ -27,6 +28,7 @@ pub struct CryptifyConfig {
smtp_tls: bool,
allowed_origins: String,
pkg_url: String,
chunk_size: u64,
}

impl From<RawCryptifyConfig> for CryptifyConfig {
Expand All @@ -45,6 +47,7 @@ impl From<RawCryptifyConfig> for CryptifyConfig {
smtp_tls: config.smtp_tls.unwrap_or(true),
allowed_origins: config.allowed_origins,
pkg_url: config.pkg_url,
chunk_size: config.chunk_size.unwrap_or(5_000_000),
}
}
}
Expand Down Expand Up @@ -89,4 +92,8 @@ impl CryptifyConfig {
pub fn pkg_url(&self) -> &str {
&self.pkg_url
}

pub fn chunk_size(&self) -> u64 {
self.chunk_size
}
}
26 changes: 13 additions & 13 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ use rocket_cors::{AllowedOrigins, CorsOptions};
use serde::{Deserialize, Serialize};
use store::{FileState, Store};

const CHUNK_SIZE: u64 = 10 * 1024 * 1024; // 10 MiB

#[derive(Serialize, Deserialize)]
struct InitBody {
recipient: String,
Expand Down Expand Up @@ -308,9 +306,9 @@ async fn upload_chunk(
)));
}

if end - start > CHUNK_SIZE {
if end - start > config.chunk_size() {
return Err(Error::BadRequest(Some(
"File chunk too large; the maximum is 10 MiB".to_owned(),
format!("File chunk too large; the maximum is {} bytes", config.chunk_size()),
)));
}

Expand Down Expand Up @@ -544,22 +542,24 @@ fn usage(store: &State<Store>, api_key: ApiKeyPresent, email: String) -> Json<Us

#[launch]
async fn rocket() -> _ {
// Extract config first so we can use chunk_size for Rocket's body-size limits.
let config = rocket::Config::figment()
.extract::<CryptifyConfig>()
.expect("Missing configuration");

// Raise Rocket's default body-size limits so chunked uploads up to
// CHUNK_SIZE do not trip "Data limit reached while reading the request
// chunk_size do not trip "Data limit reached while reading the request
// body". `data.open((end - start).bytes())` already caps the per-request
// read; this lifts the framework-level cap that runs before it.
// A small headroom above CHUNK_SIZE leaves room for HTTP overhead.
// A small headroom above chunk_size leaves room for HTTP overhead.
let chunk_size = config.chunk_size();
let limits = rocket::data::Limits::default()
.limit("bytes", (CHUNK_SIZE + 1024 * 1024).bytes())
.limit("data-form", (CHUNK_SIZE + 1024 * 1024).bytes())
.limit("file", (CHUNK_SIZE + 1024 * 1024).bytes());
.limit("bytes", (chunk_size + 1024 * 1024).bytes())
.limit("data-form", (chunk_size + 1024 * 1024).bytes())
.limit("file", (chunk_size + 1024 * 1024).bytes());

let figment = rocket::Config::figment().merge(("limits", limits));
let rocket = rocket::custom(figment);
let config = rocket
.figment()
.extract::<CryptifyConfig>()
.expect("Missing configuration");

let pkg_params_url = format!("{}/v2/sign/parameters", config.pkg_url());
let response = minreq::get(&pkg_params_url)
Expand Down