Merge remote-tracking branch 'origin/master' into feature/full-async

This commit is contained in:
Jake Howard 2024-03-15 18:50:15 +00:00
commit dd2a635574
No known key found for this signature in database
GPG Key ID: 57AFB45680EDD477
6 changed files with 102 additions and 7 deletions

7
.github/mergify.yml vendored Normal file
View File

@ -0,0 +1,7 @@
pull_request_rules:
- name: Automatic merge for Dependabot pull requests
conditions:
- author=dependabot[bot]
actions:
merge:
method: squash

View File

@ -0,0 +1,9 @@
[server]
address = "127.0.0.1:8000"
max_content_length = "10MB"
upload_path = "./upload"
[paste]
random_url = { type = "alphanumeric", length = "4", suffix_mode = true }
default_extension = "txt"
duplicate_files = true

View File

@ -0,0 +1,23 @@
#!/usr/bin/env bash
content="test data"
setup() {
echo "$content" > file
}
run_test() {
file_url=$(curl -s -F "file=@file" -H "filename:fn_from_header.txt" localhost:8000)
test "$file_url" = "http://localhost:8000/fn_from_header.txt"
test "$content" = "$(cat upload/fn_from_header.txt)"
test "$content" = "$(curl -s $file_url)"
file_url=$(curl -s -F "file=@file" -H "filename:fn_from_header.txt" localhost:8000)
test "$file_url" = "file already exists"
status_code=$(curl -s -F "file=@file" -H "filename:fn_from_header.txt" -w "%{response_code}" -o /dev/null localhost:8000)
test "$status_code" = "409"
}
teardown() {
rm file
rm -r upload
}

View File

@ -11,6 +11,7 @@ setup() {
run_test() {
test "this file type is not permitted" = "$(curl -s -F "file=@file.html" localhost:8000)"
test "this file type is not permitted" = "$(curl -s -F "file=@file.xml" localhost:8000)"
test "415" = "$(curl -s -F "file=@file.xml" -w "%{response_code}" -o /dev/null localhost:8000)"
file_url=$(curl -s -F "file=@file.txt" localhost:8000)
test "$content" = "$(curl -s $file_url)"
}

View File

@ -99,14 +99,13 @@ impl Paste {
expiry_date: Option<u128>,
header_filename: Option<String>,
config: &Config,
) -> IoResult<String> {
) -> Result<String, Error> {
let file_type = infer::get(&self.data);
if let Some(file_type) = file_type {
for mime_type in &config.paste.mime_blacklist {
if mime_type == file_type.mime_type() {
return Err(IoError::new(
IoErrorKind::Other,
String::from("this file type is not permitted"),
return Err(error::ErrorUnsupportedMediaType(
"this file type is not permitted",
));
}
}
@ -177,6 +176,12 @@ impl Paste {
.map(|v| v.to_string_lossy())
.unwrap_or_default()
.to_string();
let file_path = util::glob_match_file(path.clone())
.await
.map_err(|_| IoError::new(IoErrorKind::Other, String::from("path is not valid")))?;
if file_path.is_file() && file_path.exists() {
return Err(error::ErrorConflict("file already exists\n"));
}
if let Some(timestamp) = expiry_date {
path.set_file_name(format!("{file_name}.{timestamp}"));
}
@ -243,9 +248,7 @@ impl Paste {
.to_string());
}
}
Ok(self
.store_file(file_name, expiry_date, None, config)
.await?)
self.store_file(file_name, expiry_date, None, config).await
}
/// Writes an URL to a file in upload directory.

View File

@ -885,6 +885,58 @@ mod tests {
Ok(())
}
#[actix_web::test]
async fn test_upload_same_filename() -> Result<(), Error> {
let mut config = Config::default();
config.server.upload_path = env::current_dir()?;
let app = test::init_service(
App::new()
.app_data(Data::new(RwLock::new(config)))
.app_data(Data::new(Client::default()))
.configure(configure_routes),
)
.await;
let file_name = "test_file.txt";
let header_filename = "fn_from_header.txt";
let timestamp = util::get_system_time()?.as_secs().to_string();
let response = test::call_service(
&app,
get_multipart_request(&timestamp, "file", file_name)
.insert_header((
header::HeaderName::from_static("filename"),
header::HeaderValue::from_static("fn_from_header.txt"),
))
.to_request(),
)
.await;
assert_eq!(StatusCode::OK, response.status());
assert_body(
response.into_body(),
&format!("http://localhost:8080/{header_filename}\n"),
)
.await?;
let timestamp = util::get_system_time()?.as_secs().to_string();
let response = test::call_service(
&app,
get_multipart_request(&timestamp, "file", file_name)
.insert_header((
header::HeaderName::from_static("filename"),
header::HeaderValue::from_static("fn_from_header.txt"),
))
.to_request(),
)
.await;
assert_eq!(StatusCode::CONFLICT, response.status());
assert_body(response.into_body(), "file already exists\n").await?;
fs::remove_file(header_filename).await?;
Ok(())
}
#[actix_web::test]
#[allow(deprecated)]
async fn test_upload_duplicate_file() -> Result<(), Error> {