From e390f46b42a5519246ece4324476274bbef7e790 Mon Sep 17 00:00:00 2001 From: Toyam Cox Date: Thu, 5 Dec 2024 17:43:02 -0500 Subject: [PATCH] feat(server): allow named url shortening (#40) --- .../config.toml | 8 ++++++ .../test-url-upload-override-filename/test.sh | 24 ++++++++++++++++ src/paste.rs | 28 +++++++++++++++++-- src/server.rs | 2 +- 4 files changed, 58 insertions(+), 4 deletions(-) create mode 100644 fixtures/test-url-upload-override-filename/config.toml create mode 100755 fixtures/test-url-upload-override-filename/test.sh diff --git a/fixtures/test-url-upload-override-filename/config.toml b/fixtures/test-url-upload-override-filename/config.toml new file mode 100644 index 00000000..d919f12b --- /dev/null +++ b/fixtures/test-url-upload-override-filename/config.toml @@ -0,0 +1,8 @@ +[server] +address = "127.0.0.1:8000" +max_content_length = "10MB" +upload_path = "./upload" + +[paste] +default_extension = "txt" +duplicate_files = true diff --git a/fixtures/test-url-upload-override-filename/test.sh b/fixtures/test-url-upload-override-filename/test.sh new file mode 100755 index 00000000..71808f22 --- /dev/null +++ b/fixtures/test-url-upload-override-filename/test.sh @@ -0,0 +1,24 @@ +#!/usr/bin/env bash + +url="https://orhun.dev/" + +url2="https://orhun.dev/does/not/exist" + +setup() { + :; +} + +run_test() { + curl -s -F "url=$url" -H "filename: abc" localhost:8000 > /dev/null + test "$url" = "$(cat upload/url/abc)" + + curl -s -F "url=$url2" -H "filename: abc" localhost:8000 > /dev/null + test "$url2" = "$(cat upload/url/abc)" + + curl -s -F "url=$url2" -H "filename: what-a-great-link" localhost:8000 > /dev/null + test "$url2" = "$(cat upload/url/what-a-great-link)" +} + +teardown() { + rm -r upload +} diff --git a/src/paste.rs b/src/paste.rs index 0b6261d3..01da4504 100644 --- a/src/paste.rs +++ b/src/paste.rs @@ -254,7 +254,12 @@ impl Paste { /// /// [`random_url.enabled`]: crate::random::RandomURLConfig::enabled #[allow(deprecated)] - pub fn store_url(&self, expiry_date: Option, config: &Config) -> IoResult { + pub fn store_url( + &self, + expiry_date: Option, + header_filename: Option, + config: &Config, + ) -> IoResult { let data = str::from_utf8(&self.data) .map_err(|e| IoError::new(IoErrorKind::Other, e.to_string()))?; let url = Url::parse(data).map_err(|e| IoError::new(IoErrorKind::Other, e.to_string()))?; @@ -264,6 +269,9 @@ impl Paste { file_name = random_text; } } + if let Some(header_filename) = header_filename { + file_name = header_filename; + } let mut path = util::safe_path_join(self.type_.get_path(&config.server.upload_path)?, &file_name)?; if let Some(timestamp) = expiry_date { @@ -461,7 +469,7 @@ mod tests { data: url.as_bytes().to_vec(), type_: PasteType::Url, }; - let file_name = paste.store_url(None, &config)?; + let file_name = paste.store_url(None, None, &config)?; let file_path = PasteType::Url .get_path(&config.server.upload_path) .expect("Bad upload path") @@ -474,7 +482,21 @@ mod tests { data: url.as_bytes().to_vec(), type_: PasteType::Url, }; - assert!(paste.store_url(None, &config).is_err()); + assert!(paste.store_url(None, None, &config).is_err()); + + let url = String::from("https://orhun.dev/"); + let paste = Paste { + data: url.as_bytes().to_vec(), + type_: PasteType::Url, + }; + let prepared_result = paste.store_url(None, Some("prepared-name".to_string()), &config)?; + let file_path = PasteType::Url + .get_path(&config.server.upload_path) + .expect("Bad upload path") + .join(&prepared_result); + assert_eq!(prepared_result, "prepared-name"); + assert_eq!(url, fs::read_to_string(&file_path)?); + fs::remove_file(file_path)?; config.server.max_content_length = Byte::from_str("30k").expect("cannot parse byte"); let url = String::from("https://upload.wikimedia.org/wikipedia/en/a/a9/Example.jpg"); diff --git a/src/server.rs b/src/server.rs index 7e1173d3..2f963c7c 100644 --- a/src/server.rs +++ b/src/server.rs @@ -296,7 +296,7 @@ async fn upload( let config = config .read() .map_err(|_| error::ErrorInternalServerError("cannot acquire config"))?; - paste.store_url(expiry_date, &config)? + paste.store_url(expiry_date, header_filename, &config)? } }; info!(