Skip to content

Commit

Permalink
refactor allow manual deletion file
Browse files Browse the repository at this point in the history
  • Loading branch information
robatipoor committed Feb 25, 2024
1 parent cb29de1 commit 7c2b0e5
Show file tree
Hide file tree
Showing 14 changed files with 53 additions and 42 deletions.
28 changes: 14 additions & 14 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@ opt-level = "z"
strip = true

[workspace.dependencies]
anyhow = "1.0.79"
anyhow = "1.0.80"
argon2 = "0.5.3"
assert_cmd = "2.0.13"
assert_cmd = "2.0.14"
async-stream = "0.3.5"
async-trait = "0.1.77"
axum = { version = "0.7.4", features = ["multipart"] }
axum-extra = { version = "0.9.2", features = ["async-read-body"] }
hyper = { version = "1.1.0", features = ["full"] }
hyper-util = { version = "0.1" }
rustls-pemfile = "2.0.0"
hyper = { version = "1.2.0", features = ["full"] }
hyper-util = { version = "0.1.3" }
rustls-pemfile = "2.1.0"
tokio = { version = "1.36.0", features = [
"macros",
"time",
Expand All @@ -42,46 +42,46 @@ tokio = { version = "1.36.0", features = [
tokio-util = { version = "0.7.10", features = ["io"] }
tokio-rustls = "0.25.0"
tower = { version = "0.4.13", features = ["util", "make"] }
tower-http = { version = "0.5.1", features = ["fs", "cors"] }
tower-http = { version = "0.5.2", features = ["fs", "cors"] }
tower-service = "0.3.2"
base64 = "0.21.7"
bincode = "1.3.3"
build_html = "2.4.0"
chrono = { version = "0.4.33", features = ["serde"] }
clap = { version = "4.4.18", features = ["derive"] }
chrono = { version = "0.4.34", features = ["serde"] }
clap = { version = "4.5.1", features = ["derive"] }
config = { version = "0.14.0", default-features = false, features = ["toml"] }
chacha20poly1305 = { version = "0.10.1", features = ["stream"] }
cuid2 = "0.1.2"
fake = { version = "2.9.2", features = ['derive', 'uuid', 'chrono'] }
futures-util = "0.3.30"
indicatif = "0.17.7"
indicatif = "0.17.8"
log = "0.4.20"
log-derive = "0.4.1"
mime_guess = "2.0.4"
once_cell = { version = "1.19.0" }
qrcode = "0.13"
image = "0.24.8"
image = "0.24.9"
rand = "0.8.5"
reqwest = { version = "0.11.24", default-features = false, features = [
"json",
"multipart",
"stream",
"rustls-tls",
] }
serde = { version = "1.0.196", features = ["derive"] }
serde_json = "1.0.113"
serde = { version = "1.0.197", features = ["derive"] }
serde_json = "1.0.114"
sled = "0.34.7"
strum = { version = "0.26.1", features = ["derive"] }
test-context = "0.1.4"
thiserror = "1.0.56"
thiserror = "1.0.57"
tracing = "0.1.40"
tracing-subscriber = "0.3.18"
url = "2.5.0"
garde = { version = "0.18.0", features = ["full"] }
yew = { version = "0.21.0", features = ["csr"] }
yew-router = "0.18.0"
yew-hooks = "0.3.0"
wasm-bindgen = "0.2.90"
wasm-bindgen = "0.2.91"
wasm-logger = "0.2.0"
gloo = "0.11.0"
js-sys = "0.3.68"
Expand Down
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@ The service provides a convenient means of sharing files without the necessity f
* Anonymous Uploads
* File Expiration
* Burn After Reading
* Large File Support
* QR code Generator
* Command Line Interface (CLI)
* Command Line Interface
* ChaCha20-Poly1305 Encryption
* Built-in TLS Server


**Run Backend Service Locally**

```sh
Expand Down Expand Up @@ -82,7 +85,7 @@ $ curl -F "file=@{file_name}" 127.0.0.1:8080/upload\?max_download=10
$ curl -F "file=@{file_name}" 127.0.0.1:8080/upload\?code_length=5

# Upload a file and prevent manual deletion until expiration.
$ curl -F "file=@{file_name}" 127.0.0.1:8080/upload\?delete_manually=false
$ curl -F "file=@{file_name}" 127.0.0.1:8080/upload\?allow_manual_deletion=false

# Get metadata for a file.
$ curl -X GET http://127.0.0.1:8080/info/{code}/{file_name}
Expand All @@ -104,6 +107,9 @@ default_code_length = 3
# Default expiration time in seconds
default_expire_secs = 7200

# Allow manual deletion of files.
allow_manual_deletion = true

# Server configuration section
[server]
# Communication protocol (e.g., "http" or "https")
Expand Down
2 changes: 2 additions & 0 deletions api/settings/base.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ max_upload_bytes_size = 1000_000_000 # 1GB
default_code_length = 3
# Default expiration time in seconds
default_expire_secs = 7200
# Allow manual deletion of files.
allow_manual_deletion = true

[server]
# Communication protocol (e.g., "http" or "https")
Expand Down
2 changes: 1 addition & 1 deletion api/src/configure/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub struct ApiConfig {
pub max_upload_bytes_size: usize,
pub default_code_length: usize,
pub default_expire_secs: u64,
// TODO add default_delete_manually:bool
pub allow_manual_deletion: bool,
}

#[derive(Debug, Deserialize, Clone)]
Expand Down
4 changes: 2 additions & 2 deletions api/src/database/meta_data_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub struct MetaDataFile {
pub created_at: DateTime<Utc>,
pub expire_date_time: DateTime<Utc>,
pub secret: Option<SecretHash>,
pub delete_manually: bool,
pub allow_manual_deletion: bool,
pub max_download: Option<u32>,
pub count_downloads: u32,
}
Expand Down Expand Up @@ -63,7 +63,7 @@ impl From<&MetaDataFile> for MetaDataFileResponse {
MetaDataFileResponse {
created_at: value.created_at,
expire_date_time: value.expire_date_time,
delete_manually: value.delete_manually,
allow_manual_deletion: value.allow_manual_deletion,
max_download: value.max_download,
count_downloads: value.count_downloads,
}
Expand Down
14 changes: 7 additions & 7 deletions api/src/database/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ mod tests {
created_at: Utc::now(),
expire_date_time: Utc::now() + chrono::Duration::seconds(10),
secret: None,
delete_manually: true,
allow_manual_deletion: true,
max_download: None,
count_downloads: 1,
};
Expand All @@ -224,7 +224,7 @@ mod tests {
created_at: Utc::now(),
expire_date_time: Utc::now() + chrono::Duration::seconds(10),
secret: None,
delete_manually: true,
allow_manual_deletion: true,
max_download: None,
count_downloads: 0,
};
Expand Down Expand Up @@ -257,7 +257,7 @@ mod tests {
created_at: Utc::now(),
expire_date_time: Utc::now() + chrono::Duration::seconds(10),
secret: None,
delete_manually: true,
allow_manual_deletion: true,
max_download: None,
count_downloads: 0,
};
Expand All @@ -278,7 +278,7 @@ mod tests {
created_at: Utc::now(),
expire_date_time: Utc::now(),
secret: None,
delete_manually: true,
allow_manual_deletion: true,
max_download: None,
count_downloads: 0,
};
Expand All @@ -300,7 +300,7 @@ mod tests {
created_at: Utc::now(),
expire_date_time: Utc::now() + chrono::Duration::seconds(10),
secret: None,
delete_manually: true,
allow_manual_deletion: true,
max_download: None,
count_downloads: 0,
};
Expand All @@ -322,7 +322,7 @@ mod tests {
created_at: Utc::now(),
expire_date_time: Utc::now() + chrono::Duration::seconds(10),
secret: None,
delete_manually: true,
allow_manual_deletion: true,
max_download: None,
count_downloads: 0,
};
Expand All @@ -345,7 +345,7 @@ mod tests {
created_at: Utc::now(),
expire_date_time: Utc::now() + chrono::Duration::seconds(10),
secret: None,
delete_manually: true,
allow_manual_deletion: true,
max_download: None,
count_downloads: 0,
};
Expand Down
16 changes: 9 additions & 7 deletions api/src/service/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ pub async fn store(
let meta = MetaDataFile {
created_at: now,
expire_date_time,
delete_manually: param.delete_manually.unwrap_or(true),
allow_manual_deletion: param
.allow_manual_deletion
.unwrap_or(state.config.allow_manual_deletion),
max_download: param.max_download,
secret,
count_downloads: 0,
Expand Down Expand Up @@ -201,7 +203,7 @@ pub async fn delete(
file_name: file_name.to_string(),
};
if let Some(meta) = state.db.fetch(&path)? {
if meta.delete_manually {
if meta.allow_manual_deletion {
authorize_user(secret, &meta.secret)?;
let file_path = state.config.fs.base_dir.join::<PathBuf>((&path).into());
tokio::fs::remove_file(file_path).await?;
Expand Down Expand Up @@ -261,7 +263,7 @@ mod tests {
max_download: None,
code_length: None,
expire_secs: None,
delete_manually: Some(false),
allow_manual_deletion: Some(false),
qr_code_format: None,
};
let file_name = format!("{}.txt", Faker.fake::<String>());
Expand All @@ -279,7 +281,7 @@ mod tests {
max_download: Some(1),
code_length: None,
expire_secs: None,
delete_manually: Some(false),
allow_manual_deletion: Some(false),
qr_code_format: None,
};
let file_name = format!("{}.txt", Faker.fake::<String>());
Expand All @@ -304,7 +306,7 @@ mod tests {
max_download: None,
code_length: None,
expire_secs: None,
delete_manually: Some(true),
allow_manual_deletion: Some(true),
qr_code_format: None,
};
let file_name = format!("{}.txt", Faker.fake::<String>());
Expand All @@ -328,7 +330,7 @@ mod tests {
max_download: None,
code_length: None,
expire_secs: None,
delete_manually: Some(true),
allow_manual_deletion: Some(true),
qr_code_format: None,
};
let file_name = format!("{}.txt", Faker.fake::<String>());
Expand Down Expand Up @@ -366,7 +368,7 @@ mod tests {
max_download: None,
code_length: Some(code_length),
expire_secs: None,
delete_manually: Some(false),
allow_manual_deletion: Some(false),
qr_code_format: None,
};
let file_name = format!("{}.txt", Faker.fake::<String>());
Expand Down
2 changes: 1 addition & 1 deletion api/tests/api/helper/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ impl ApiTestContext {
max_download: max,
code_length: len,
expire_secs: exp,
delete_manually: del,
allow_manual_deletion: del,
qr_code_format: qr,
};
let (_, resp) = self
Expand Down
2 changes: 1 addition & 1 deletion api/tests/api/info_api_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub async fn test_info(ctx: &mut ApiTestContext) {
let (status, resp) = ctx.info(&file.url_path, None).await.unwrap();
let resp = unwrap!(resp);
assert!(status.is_success(), "status: {status}");
assert!(resp.delete_manually);
assert!(resp.allow_manual_deletion);
}

#[test_context(ApiTestContext)]
Expand Down
2 changes: 1 addition & 1 deletion cli/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ pub enum SubCommand {
#[clap(short, long)]
max_download: Option<u32>,
#[clap(short, long)]
delete_manually: Option<bool>,
allow_manual_deletion: Option<bool>,
#[clap(default_value_t = UploadOutput::Json, short, long)]
output: UploadOutput,
#[clap(default_value_t = false, short, long)]
Expand Down
4 changes: 2 additions & 2 deletions cli/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub struct UploadArguments {
pub code_length: Option<usize>,
pub progress_bar: bool,
pub expire: Option<u64>,
pub delete_manually: Option<bool>,
pub allow_manual_deletion: Option<bool>,
pub max_download: Option<u32>,
pub output: UploadOutput,
pub source_file: PathBuf,
Expand Down Expand Up @@ -46,7 +46,7 @@ pub async fn upload(args: UploadArguments) {
max_download: args.max_download,
code_length: args.code_length,
expire_secs: args.expire,
delete_manually: args.delete_manually,
allow_manual_deletion: args.allow_manual_deletion,
qr_code_format: None,
};
let client = CommandLineClient::new(args.server_addr);
Expand Down
4 changes: 2 additions & 2 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ async fn main() {
code_length,
progress_bar,
expire,
delete_manually,
allow_manual_deletion,
max_download,
output,
source_file,
Expand All @@ -33,7 +33,7 @@ async fn main() {
code_length,
progress_bar,
expire,
delete_manually,
allow_manual_deletion,
max_download,
output,
source_file,
Expand Down
2 changes: 1 addition & 1 deletion sdk/src/dto/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub struct UploadQueryParam {
#[garde(range(min = 1, max = 100_000_000))]
pub expire_secs: Option<u64>,
#[garde(skip)]
pub delete_manually: Option<bool>,
pub allow_manual_deletion: Option<bool>,
#[garde(skip)]
pub qr_code_format: Option<QrCodeFormat>,
}
Expand Down
3 changes: 2 additions & 1 deletion sdk/src/dto/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@ impl MessageResponse {
pub struct UploadResponse {
pub expire_date_time: DateTime<Utc>,
pub url: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub qr_code: Option<String>,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct MetaDataFileResponse {
pub created_at: DateTime<Utc>,
pub expire_date_time: DateTime<Utc>,
pub delete_manually: bool,
pub allow_manual_deletion: bool,
pub max_download: Option<u32>,
pub count_downloads: u32,
}
Expand Down

0 comments on commit 7c2b0e5

Please sign in to comment.