Skip to content

Commit

Permalink
fix: use percent-decode instead of serde for path
Browse files Browse the repository at this point in the history
  • Loading branch information
Kneemund committed Dec 29, 2023
1 parent 97fa086 commit 4d07fac
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 7 deletions.
6 changes: 3 additions & 3 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ reqwest = { version = "0.11.22", default-features = false, features = [
rusttype = "0.9.3"
serde = "1.0.188"
serde_json = "1.0.107"
serde_urlencoded = "0.7.1"
percent-encoding = "2.3.1"
serenity = { git = "https://github.com/serenity-rs/serenity", branch = "next", default-features = false, features = [
"builder",
"cache",
Expand Down
8 changes: 5 additions & 3 deletions src/bot/file_preview/github_repositoriy_file.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::error::Error;
use std::path::PathBuf;

use percent_encoding::percent_decode_str;
use reqwest::Url;
use serenity::utils::MessageBuilder;

Expand All @@ -24,7 +25,8 @@ impl GitHubRepositoryFilePreview {
_ => return Err("Malformed GitHub repository URL.".into()),
};

let path: String = serde_urlencoded::from_str(urlencoded_path.as_str())
let path = percent_decode_str(urlencoded_path.as_str())
.decode_utf8()
.map_err(|_| "Failed to decode GitHub URL file path.")?;

let metadata_content = MessageBuilder::new()
Expand All @@ -34,14 +36,14 @@ impl GitHubRepositoryFilePreview {
.push(" (on ")
.push_safe(branch.to_owned())
.push_line(")")
.push_line_safe(path.as_str())
.push_line_safe(path.as_ref())
.build();

let mut raw_url = Url::parse("https://raw.githubusercontent.com/").unwrap();
raw_url
.path_segments_mut()
.unwrap()
.extend(&[author, repository, branch, path.as_str()]);
.extend(&[author, repository, branch, path.as_ref()]);

let file_name = message_url
.path_segments()
Expand Down

0 comments on commit 4d07fac

Please sign in to comment.