Skip to content

Commit

Permalink
fix: prevent using non ascii character in filename
Browse files Browse the repository at this point in the history
  • Loading branch information
leo91000 committed Jun 19, 2022
1 parent f07cb6a commit ae89a69
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 6 deletions.
8 changes: 8 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions crates/server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ aws-smithy-async = { version = "0.43.0", features = ["rt-tokio"] }
image = { version = "0.24.2" }
openssl = { version = "0.10.40", features = ["vendored"] }
openssl-probe = "0.1.5"
deunicode = { version = "1.3.1" }
lazy_static = { version = "1.4.0" }

[dev-dependencies]
portpicker = "0.1.1"
Expand Down
4 changes: 3 additions & 1 deletion crates/server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ async fn main() -> io::Result<()> {
.await
.build()?;

server.run_until_stopped().await?;
server.run_until_stopped().await.ok();

opentelemetry::global::shutdown_tracer_provider();

Ok(())
}
3 changes: 2 additions & 1 deletion crates/server/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ impl Server {

impl ActiveServer {
pub async fn run_until_stopped(self) -> std::io::Result<()> {
self.server.await
let res = self.server.await;
res
}
}
22 changes: 18 additions & 4 deletions crates/server/src/services/image/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use actix_web::{delete, get, post, web, Error as ActixError, HttpResponse};
use aws_sdk_s3::model::ObjectCannedAcl::PublicRead;
use aws_sdk_s3::types::ByteStream;
use aws_smithy_http::body::SdkBody;
use deunicode::deunicode;
use entity::image::{Column, Entity, LazyImageLink, Model};
use futures::future::ready;
use futures::{StreamExt, TryFutureExt};
Expand Down Expand Up @@ -90,13 +91,26 @@ async fn compress_and_upload(
image_output
};

let file_stem = p_filename
.file_stem()
.and_then(OsStr::to_str)
.map(|v| {
// Cleanup filename
deunicode(v)
.chars()
.filter(|c| c.is_ascii())
.map(|c| match c {
' ' => '_',
_ => c,
})
.collect::<String>()
})
.unwrap_or("unknown".to_string());

let key = format!(
"{}__{}{}.{}",
id,
p_filename
.file_stem()
.and_then(OsStr::to_str)
.unwrap_or("unknown"),
file_stem,
if lazy { "__lazy" } else { "" },
"jpeg"
);
Expand Down

0 comments on commit ae89a69

Please sign in to comment.