Skip to content

Commit

Permalink
update dependecies and settings file
Browse files Browse the repository at this point in the history
  • Loading branch information
robatipoor committed Mar 12, 2024
1 parent 976e05c commit 1d734ba
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 32 deletions.
16 changes: 8 additions & 8 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ opt-level = "z"
strip = true

[workspace.dependencies]
anyhow = "1.0.80"
anyhow = "1.0.81"
argon2 = "0.5.3"
assert_cmd = "2.0.14"
async-stream = "0.3.5"
axum = { version = "0.7.4", features = ["multipart"] }
axum-extra = { version = "0.9.2", features = ["async-read-body"] }
hyper = { version = "1.2.0", features = ["full"] }
hyper-util = { version = "0.1.3" }
rustls-pemfile = "2.1.0"
rustls-pemfile = "2.1.1"
tokio = { version = "1.36.0", features = [
"macros",
"time",
Expand All @@ -47,21 +47,21 @@ tower-service = "0.3.2"
base64 = "0.22.0"
bincode = "1.3.3"
build_html = "2.4.0"
chrono = { version = "0.4.34", features = ["serde"] }
clap = { version = "4.5.1", features = ["derive"] }
chrono = { version = "0.4.35", features = ["serde"] }
clap = { version = "4.5.2", 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.8"
log = "0.4.20"
log = "0.4.21"
mime_guess = "2.0.4"
once_cell = { version = "1.19.0" }
qrcode = "0.13"
image = "0.24.9"
rand = "0.8.5"
reqwest = { version = "0.11.24", default-features = false, features = [
reqwest = { version = "0.11.25", default-features = false, features = [
"json",
"multipart",
"stream",
Expand All @@ -70,9 +70,9 @@ reqwest = { version = "0.11.24", default-features = false, features = [
serde = { version = "1.0.197", features = ["derive"] }
serde_json = "1.0.114"
sled = "0.34.7"
strum = { version = "0.26.1", features = ["derive"] }
strum = { version = "0.26.2", features = ["derive"] }
test-context = "0.3.0"
thiserror = "1.0.57"
thiserror = "1.0.58"
tracing = "0.1.40"
tracing-subscriber = "0.3.18"
url = "2.5.0"
Expand Down
21 changes: 9 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# PasteFile 📁 📥
The service provides a convenient means of sharing files with easy setup. The code includes the server API and CLI client tool, encompassing everything you need to create your own instance.
The service provides a convenient and efficient way to share files. The code includes the server API and CLI client tool, encompassing everything you need to create your own instance.

![License](https://img.shields.io/github/license/robatipoor/pf)
[![Lines Of Code](https://tokei.rs/b1/github/robatipoor/pf?category=code)](https://github.com/robatipoor/pf)
Expand All @@ -22,7 +22,7 @@ The service provides a convenient means of sharing files with easy setup. The co
* Built-in TLS Server


**Run Backend Service Locally**
**Run API Service Locally**

```sh
# Clone the project
Expand All @@ -37,7 +37,7 @@ $ ./target/release/api --settings api/settings/base.toml
# Alternatively, Run backend with cargo
$ cargo run --bin api
```
**Run Backend Service via Docker**
**Run API Service via Docker**

```sh
# Build docker image
Expand Down Expand Up @@ -96,7 +96,7 @@ $ curl -X GET http://127.0.0.1:8080/info/{code}/{file_name}
$ curl -X DELETE http://127.0.0.1:8080/{code}/{file_name}
```

**Backend settings**
**API settings**

```toml
# api/settings/base.toml
Expand All @@ -123,8 +123,8 @@ host = "127.0.0.1"
# Port number for the server
port = 8080

# Domain URL
domain = "http://localhost:8080"
# Domain name URL
domain_name = "http://localhost:8080"

# TLS key file path
file_tls_key_path = "key.pem"
Expand Down Expand Up @@ -163,20 +163,17 @@ $ mv ~/.cargo/bin/cli ~/.cargo/bin/pf
$ alias pf="pf --server-addr http://localhost:8080"

# Encrypt and upload a file and retrieve the corresponding download URL.
$ pf upload --source-file ~/example-file.txt --key-nonce "{key}:{nonce}" --progress-bar
$ pf upload --source-file ~/example-file.txt --key-nonce "{key}:{nonce}" --progress-bar

# Upload a file with basic authentication and progress bar option.
$ pf --auth "username:password" upload --source-file ~/example-file.txt
# Download and decrypt a file.
$ pf download --destination ~/example-dir/ --url-path "{code}/{file_name}" --key-nonce "{key}:{nonce}"

# Upload a text with an expiration time of 10 minutes.
$ echo 'Hello World!' | pf copy --expire "10 minute"

# Upload a file and then display the QR code.
$ pf upload --source-file ~/example-file.txt --output qr-code

# Download a file with progress bar option.
$ pf download --destination ~/example-dir/ --url-path "{code}/{file_name}" --progress-bar

# Paste a file.
$ pf paste --url-path "{code}/{file_name}" > my_file.txt

Expand Down
6 changes: 4 additions & 2 deletions api/settings/base.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ schema = "http"
host = "127.0.0.1"
# Port number for the server
port = 8080
# Domain URL
domain = "http://localhost:8080"
# Domain name URL
domain_name = "http://localhost:8080"
# Public IP address
public_addr = "http://127.0.0.1:8080"
# TLS key file path
file_tls_key_path = "key.pem"
# TLS certificate file path
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 @@ -29,7 +29,7 @@ pub struct ApiConfig {

#[derive(Debug, Deserialize, Clone)]
pub struct ServerConfig {
pub domain: String,
pub domain_name: String,
pub schema: UrlSchema,
pub host: String,
pub port: u16,
Expand Down
2 changes: 1 addition & 1 deletion api/src/handler/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub async fn upload(
let (file_path, expire_date_time) =
service::file::store(&state, &param, secret, multipart).await?;
let url = create_url(
&state.config.server.domain,
&state.config.server.domain_name,
&file_path.code,
&file_path.file_name,
)?
Expand Down
2 changes: 1 addition & 1 deletion api/src/handler/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub struct IndexPage<'a> {

pub async fn page(State(state): State<ApiState>) -> ApiResult<Html<String>> {
let page = IndexPage {
domain: &state.config.server.domain,
domain: &state.config.server.domain_name,
};
Ok(Html(page.render()?))
}
7 changes: 4 additions & 3 deletions api/src/router/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ pub fn get_router(state: ApiState) -> ApiResult<Router> {
.route("/:code/:file_name", get(handler::file::download))
.route("/:code/:file_name", delete(handler::file::delete))
.route("/", get(handler::index::page))
.layer(cors_layer(&state.config.server.domain)?)
.layer(cors_layer(&state.config.server.domain_name)?)
.with_state(state),
)
}

fn cors_layer(domain: &str) -> ApiResult<tower_http::cors::CorsLayer> {
fn cors_layer(domain_name: &str) -> ApiResult<tower_http::cors::CorsLayer> {
Ok(
tower_http::cors::CorsLayer::new()
.allow_methods([
Expand All @@ -31,7 +31,8 @@ fn cors_layer(domain: &str) -> ApiResult<tower_http::cors::CorsLayer> {
hyper::Method::DELETE,
])
.allow_origin(tower_http::cors::AllowOrigin::exact(
HeaderValue::from_str(domain).map_err(|e| anyhow!("Invalid domain url, Error: {e}"))?,
HeaderValue::from_str(domain_name)
.map_err(|e| anyhow!("Invalid domain url, Error: {e}"))?,
))
.allow_headers([hyper::header::CONTENT_TYPE]),
)
Expand Down
9 changes: 6 additions & 3 deletions api/src/service/file.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::configure::ApiConfig;
use crate::database::file_path::FilePath;
use crate::database::meta_data_file::MetaDataFile;
use crate::error::invalid_input_error;
use crate::error::{
result::{ApiResult, ToApiResult},
ApiError,
Expand Down Expand Up @@ -37,7 +38,7 @@ pub async fn store(
.expire_secs
.unwrap_or(state.config.default_expire_secs) as i64;
let now = Utc::now();
let expire_date_time = calc_expiration_date(now, expire_secs);
let expire_date_time = calc_expiration_date(now, expire_secs)?;
let mut code_length = param
.code_length
.unwrap_or(state.config.default_code_length);
Expand Down Expand Up @@ -237,8 +238,10 @@ pub fn authorize_user(secret: Option<Secret>, secret_hash: &Option<SecretHash>)
Ok(())
}

pub fn calc_expiration_date(now: DateTime<Utc>, secs: i64) -> DateTime<Utc> {
now + chrono::Duration::seconds(secs)
pub fn calc_expiration_date(now: DateTime<Utc>, expire_secs: i64) -> ApiResult<DateTime<Utc>> {
chrono::Duration::try_seconds(expire_secs)
.map(|s| now + s)
.ok_or_else(|| invalid_input_error("expire_secs", "Invalid expire seconds"))
}

#[cfg(test)]
Expand Down
2 changes: 1 addition & 1 deletion api/tests/api/index_page_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ use test_context::test_context;
#[tokio::test]
pub async fn test_index_page(ctx: &mut ApiTestContext) {
let (status, html) = ctx.index_page().await.unwrap();
assert!(html.contains(&ctx.state.config.server.domain));
assert!(html.contains(&ctx.state.config.server.domain_name));
assert!(status.is_success(), "status: {status}");
}

0 comments on commit 1d734ba

Please sign in to comment.