Skip to content

Commit 93839f2

Browse files
committedJun 11, 2022
Update project info and format code
1 parent 9895ef0 commit 93839f2

File tree

8 files changed

+78
-89
lines changed

8 files changed

+78
-89
lines changed
 

‎.vscode/extensions.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
"adpyke.vscode-sql-formatter",
44
"editorconfig.editorconfig",
55
"esbenp.prettier-vscode",
6-
"matklad.rust-analyzer"
6+
"rust-lang.rust-analyzer"
77
]
88
}

‎.vscode/settings.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"editor.formatOnSave": false
77
},
88
"[rust]": {
9-
"editor.defaultFormatter": "matklad.rust-analyzer"
9+
"editor.defaultFormatter": "rust-lang.rust-analyzer"
1010
},
1111
"[sql]": {
1212
"editor.defaultFormatter": "adpyke.vscode-sql-formatter"

‎Cargo.lock

+37-49
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Cargo.toml

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
[package]
22
name = "dekinai"
3-
description = "Dekinai is a simple and minimalistic file uploading API.\nIt provides the bare minimum feature set for file hosting services, while also being fast and portable."
43
version = "1.1.0"
4+
description = "Dekinai is a simple and minimalistic file uploading API.\nIt provides the bare minimum feature set for file hosting services, while also being fast and portable."
5+
categories = ["filesystem", "network-programming", "web-programming::http-server"]
6+
keywords = ["file", "upload", "host", "hoster", "hosting"]
57
authors = ["Johann Rekowski <johann.rekowski@gmail.com>"]
8+
repository = "https://github.com/HitomiTenshi/dekinai.git"
69
license = "MIT"
710
edition = "2021"
811
rust-version = "1.59"

‎src/cli.rs

+13-14
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,11 @@ pub fn get_matches() -> ArgMatches {
1616
.parse::<usize>()
1717
.map(|port| PORT_RANGE.contains(&port))
1818
.map_err(|err| err.to_string())
19-
.and_then(|result| match result {
20-
true => Ok(()),
21-
false => Err(format!(
22-
"Port needs to be a number between {} and {}",
23-
PORT_RANGE.start(),
24-
PORT_RANGE.end()
25-
)),
26-
})
19+
.and_then(|result| if result { Ok(()) } else { Err(format!(
20+
"Port needs to be a number between {} and {}",
21+
PORT_RANGE.start(),
22+
PORT_RANGE.end()
23+
)) })
2724
}),
2825
)
2926
.arg(arg!(-u --unix <FILE> "Sets the unix socket to listen on (Unix only)").required(false))
@@ -34,9 +31,10 @@ pub fn get_matches() -> ArgMatches {
3431
.validator(validate_dir)
3532
)
3633
.arg(arg!(--password <TEXT> "Sets a password for API requests").required(false).env("DEKINAI_PASSWORD").validator(|value| {
37-
match value.is_ascii() {
38-
true => Ok(()),
39-
false => Err("Password needs to contain only ASCII characters")
34+
if value.is_ascii() {
35+
Ok(())
36+
} else {
37+
Err("Password needs to contain only ASCII characters")
4038
}
4139
}))
4240
.arg(arg!(-b --blacklist <FILE_EXTENSIONS> "Sets a list of disallowed file extensions\nUsage: --blacklist asp html php").required(false).multiple_values(true))
@@ -45,8 +43,9 @@ pub fn get_matches() -> ArgMatches {
4543
}
4644

4745
fn validate_dir(path: &str) -> Result<(), String> {
48-
match Path::new(path).is_dir() {
49-
true => Ok(()),
50-
false => Err(format!("Cannot access directory \"{}\"", path)),
46+
if Path::new(path).is_dir() {
47+
Ok(())
48+
} else {
49+
Err(format!("Cannot access directory \"{}\"", path))
5150
}
5251
}

‎src/config.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ impl From<&ArgMatches> for AppConfig {
3838
Self {
3939
blacklist: matches
4040
.values_of("blacklist")
41-
.map(|values| values.map(|str| str.to_lowercase()).collect()),
41+
.map(|values| values.map(str::to_lowercase).collect()),
4242
output: PathBuf::from(matches.value_of("output").unwrap()),
4343
password_hash: matches
4444
.value_of("password")

‎src/lib.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,16 @@ pub enum Error {
1919

2020
#[allow(non_snake_case)]
2121
impl Error {
22-
pub fn BadRequest<T>(_: T) -> Error {
23-
Error::BAD_REQUEST
22+
pub fn BadRequest<T>(_: T) -> Self {
23+
Self::BAD_REQUEST
2424
}
2525

26-
pub fn InternalServerError<T>(_: T) -> Error {
27-
Error::INTERNAL_SERVER_ERROR
26+
pub fn InternalServerError<T>(_: T) -> Self {
27+
Self::INTERNAL_SERVER_ERROR
2828
}
2929

30-
pub fn Unauthorized<T>(_: T) -> Error {
31-
Error::UNAUTHORIZED
30+
pub fn Unauthorized<T>(_: T) -> Self {
31+
Self::UNAUTHORIZED
3232
}
3333
}
3434

@@ -39,10 +39,10 @@ impl ResponseError for Error {
3939

4040
fn status_code(&self) -> StatusCode {
4141
match *self {
42-
Error::BAD_REQUEST => StatusCode::BAD_REQUEST,
43-
Error::INTERNAL_SERVER_ERROR => StatusCode::INTERNAL_SERVER_ERROR,
44-
Error::NOT_FOUND => StatusCode::NOT_FOUND,
45-
Error::UNAUTHORIZED => StatusCode::UNAUTHORIZED,
42+
Self::BAD_REQUEST => StatusCode::BAD_REQUEST,
43+
Self::INTERNAL_SERVER_ERROR => StatusCode::INTERNAL_SERVER_ERROR,
44+
Self::NOT_FOUND => StatusCode::NOT_FOUND,
45+
Self::UNAUTHORIZED => StatusCode::UNAUTHORIZED,
4646
}
4747
}
4848
}

‎src/route.rs

+11-12
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,11 @@ pub async fn upload(req: HttpRequest, mut payload: Multipart) -> Result<web::Jso
5656
.await?;
5757

5858
while let Some(chunk) = &field.next().await {
59-
match chunk {
60-
Ok(bytes) => file.write_all(bytes).await.map_err(Error::InternalServerError)?,
61-
_ => {
62-
let _ = fs::remove_file(&file_path).await;
63-
return Err(Error::INTERNAL_SERVER_ERROR);
64-
}
59+
if let Ok(bytes) = chunk {
60+
file.write_all(bytes).await.map_err(Error::InternalServerError)?;
61+
} else {
62+
let _unused = fs::remove_file(&file_path).await;
63+
return Err(Error::INTERNAL_SERVER_ERROR);
6564
}
6665
}
6766

@@ -85,7 +84,7 @@ pub async fn delete(req: HttpRequest, path: web::Path<(String, String)>) -> Resu
8584
if let Some(deletion_password) = &db::get_deletion_password(pool, file_stem, &file_extension).await? {
8685
validate_password(deletion_password, &path.1)?;
8786
db::delete_file(pool, file_stem, &file_extension).await?;
88-
let _ = fs::remove_file(file_path).await;
87+
let _unused = fs::remove_file(file_path).await;
8988
Ok("File has been deleted.")
9089
} else {
9190
Err(Error::UNAUTHORIZED)
@@ -114,10 +113,10 @@ async fn create_random_file(
114113
let file_stem = lib::get_random_text(rng, 8);
115114
let deletion_password = lib::get_random_text(rng, 24);
116115

117-
let filename = if !file_extension.is_empty() {
118-
file_stem.to_owned() + "." + file_extension
116+
let filename = if file_extension.is_empty() {
117+
file_stem.clone()
119118
} else {
120-
file_stem.to_owned()
119+
file_stem.clone() + "." + file_extension
121120
};
122121

123122
let file_path = config.output.join(&filename);
@@ -139,9 +138,9 @@ async fn create_random_file(
139138
filename,
140139
deletion_password,
141140
));
142-
} else {
143-
db::delete_file(pool, &file_stem, file_extension).await?;
144141
}
142+
143+
db::delete_file(pool, &file_stem, file_extension).await?;
145144
}
146145

147146
create_random_file(config, rng, pool, file_extension).await

0 commit comments

Comments
 (0)
Please sign in to comment.