From 8c54214848dac3176cc406ef10ffac1e05d0ed0e Mon Sep 17 00:00:00 2001 From: Sebastiano Barezzi Date: Sun, 26 Jun 2022 19:02:32 +0200 Subject: fix(utils): Handle programming languages with spaces Change-Id: Ic0edc6bd6c94e0368dcf6910c8cbb56866127402 --- Cargo.toml | 1 + src/utils.rs | 12 +++++++----- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 0cf1dd4..7da9617 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -34,6 +34,7 @@ serde_json = "1.0.79" getrandom = { version = "0.2.6", features = ["js"] } rand = "0.8.5" dotenv = "0.15.0" +urlparse = "0.7.3" # Worker worker = "0.0.9" diff --git a/src/utils.rs b/src/utils.rs index c42c15c..242cd77 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -20,6 +20,9 @@ use std::{lazy::SyncLazy, sync::Mutex}; use worker::Cors; +use urlparse::quote; +use urlparse::unquote; + use crate::{ constants, structures::{GitHubAPIResponse, Type}, @@ -130,8 +133,8 @@ pub async fn filter_images_by_language( ) -> Vec { let mut images = vec![]; - // URL (percent) encoding of pound symbol to pound symbol - let language = language.replace("%23", "#"); + // URL (percent) decoding + let language = unquote(language).ok().unwrap(); for item in github_api(repository.clone()).await.unwrap().tree { if item.path.split('/').collect::>()[0] == language @@ -144,9 +147,8 @@ pub async fn filter_images_by_language( } else { &*boys::GITHUB_USER_CONTENT }, - // Pound symbols to URL (percent) encoding of pound symbol because we - // are pushing a URL, not a string - item.path.replace('#', "%23") + // URL (percent) encoding because we are pushing a URL, not a string + quote(item.path, b"").ok().unwrap() )); } } -- cgit v1.2.3