Skip to content

Commit e2ead42

Browse files
committed
Chunk size in config file, back to 1mb default
1 parent c08912d commit e2ead42

File tree

3 files changed

+19
-10
lines changed

3 files changed

+19
-10
lines changed

cryptify-back-end/src/config.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ pub struct RawCryptifyConfig {
1010
smtp_credentials: Option<(String, String)>,
1111
allowed_origins: String,
1212
pkg_url: String,
13+
chunk_size: Option<u64>,
1314
s3_endpoint: Option<String>,
1415
s3_access_key: Option<String>,
1516
s3_secret_key: Option<String>,
@@ -28,6 +29,7 @@ pub struct CryptifyConfig {
2829
smtp_credentials: Option<(String, String)>,
2930
allowed_origins: String,
3031
pkg_url: String,
32+
chunk_size: u64,
3133

3234
s3_endpoint: Option<String>,
3335
s3_access_key: Option<String>,
@@ -37,7 +39,10 @@ pub struct CryptifyConfig {
3739
}
3840

3941
impl From<RawCryptifyConfig> for CryptifyConfig {
40-
fn from(config: RawCryptifyConfig) -> Self {
42+
fn from(mut config: RawCryptifyConfig) -> Self {
43+
// deoption chunk_size to default value if not set
44+
let chunk_size = config.chunk_size.take().unwrap_or(1024 * 1024); // 1 MB default for backward compatibility with older configs and front-ends
45+
4146
CryptifyConfig {
4247
server_url: config.server_url,
4348
data_dir: config.data_dir,
@@ -50,6 +55,7 @@ impl From<RawCryptifyConfig> for CryptifyConfig {
5055
smtp_credentials: config.smtp_credentials,
5156
allowed_origins: config.allowed_origins,
5257
pkg_url: config.pkg_url,
58+
chunk_size,
5359
s3_endpoint: config.s3_endpoint,
5460
s3_access_key: config.s3_access_key,
5561
s3_secret_key: config.s3_secret_key,
@@ -91,6 +97,10 @@ impl CryptifyConfig {
9197
pub fn pkg_url(&self) -> &str {
9298
&self.pkg_url
9399
}
100+
101+
pub fn chunk_size(&self) -> u64 {
102+
self.chunk_size
103+
}
94104

95105
pub fn s3_endpoint(&self) -> Option<&str> {
96106
self.s3_endpoint.as_deref()

cryptify-back-end/src/main.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,6 @@ use rocket_cors::{AllowedOrigins, CorsOptions};
3838
use serde::{Deserialize, Serialize};
3939
use store::{FileState, Store};
4040

41-
const CHUNK_SIZE: u64 = (1024 * 1024)*5; // 5 MB
42-
4341
#[derive(Serialize, Deserialize)]
4442
struct InitBody {
4543
recipient: String,
@@ -301,7 +299,7 @@ async fn upload_chunk(
301299
)));
302300
}
303301

304-
if end - start > CHUNK_SIZE {
302+
if end - start > config.chunk_size() {
305303
return Err(Error::BadRequest(Some(
306304
"File chunk too large; the maximum is 1 MB".to_owned(),
307305
)));
@@ -329,7 +327,7 @@ async fn upload_chunk(
329327

330328
let byte_stream = data.into_inner();
331329

332-
let part_number = (start / CHUNK_SIZE + 1) as i32;
330+
let part_number = (start / config.chunk_size() + 1) as i32;
333331
let upload_part_res = client
334332
.upload_part()
335333
.bucket(bucket_name)
@@ -469,7 +467,7 @@ async fn upload_finalize(
469467
.into_async_read()
470468
.compat();
471469
} else {
472-
// read with the s3 client so it's the same type as above but still works for local files
470+
// read with the s3 client, so it's the same type as above but still works for local files
473471
file = aws_sdk_s3::primitives::ByteStream::from_path(Path::new(config.data_dir()).join(uuid))
474472
.await
475473
.map_err(|_| Error::InternalServerError(Some("could not open file".to_string())))?
@@ -500,7 +498,7 @@ async fn upload_finalize(
500498
}
501499

502500
#[get("/<uuid>")]
503-
async fn s3fileServer(
501+
async fn s3_file_server(
504502
_config: &State<CryptifyConfig>,
505503
_store: &State<Store>,
506504
s3_client: &State<Option<store::S3Client>>,
@@ -575,8 +573,8 @@ async fn rocket() -> _ {
575573

576574
// conditionally mount the file download routes
577575
if s3_client.is_some() {
578-
rocket = rocket.mount("/filedownload", routes![s3fileServer]);
579-
if CHUNK_SIZE < 5 * 1024 * 1024 {
576+
rocket = rocket.mount("/filedownload", routes![s3_file_server]);
577+
if config.chunk_size() < 5 * 1024 * 1024 { // 5 MB is the usual minimum for S3 multipart uploads
580578
log::warn!("S3 storage is enabled, but CHUNK_SIZE is less than 5 MB. This may lead to failing uploads.");
581579
}
582580
} else {

cryptify-front-end/src/Constants.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {browserName, browserVersion, isMobile} from "react-device-detect";
33
type ConfigFile = {
44
PKG_URL?: string;
55
BACKEND_URL?: string;
6+
UPLOAD_CHUNK_SIZE?: number;
67
};
78
const rawConfig: unknown = (window as any).__APP_CONFIG__;
89
const configFile: ConfigFile = rawConfig && typeof rawConfig === "object" ? (rawConfig as ConfigFile) : {};
@@ -12,7 +13,7 @@ export const MAX_UPLOAD_SIZE: number = 2 * 1000 * 1000 * 1000;
1213

1314
// 1Mb chunks
1415
export const FILEREAD_CHUNK_SIZE: number = 1024 * 1024;
15-
export const UPLOAD_CHUNK_SIZE: number = (1024 * 1024)*5;
16+
export const UPLOAD_CHUNK_SIZE: number = configFile.UPLOAD_CHUNK_SIZE ?? 1024 * 1024;
1617

1718
// progress bar smooth time in seconds.
1819
export const SMOOTH_TIME: number = 2;

0 commit comments

Comments
 (0)