summaryrefslogtreecommitdiffstats
path: root/server
diff options
context:
space:
mode:
authorDessalines <dessalines@users.noreply.github.com>2020-05-12 15:23:48 -0400
committerGitHub <noreply@github.com>2020-05-12 15:23:48 -0400
commitf971e31171b8019b8c92490f4a64c844acfe9d9c (patch)
tree837eff66d500848f029ba9963d7604d027f49cb9 /server
parentc22310bdaf0d289e83a99aa34e1c015c7078b10e (diff)
Changing image_content_type function to Result. (#711)
* Changing image_content_type function to Result. * Changing image to image/
Diffstat (limited to 'server')
-rw-r--r--server/src/lib.rs29
1 files changed, 16 insertions, 13 deletions
diff --git a/server/src/lib.rs b/server/src/lib.rs
index 02c5452b..d1531d7e 100644
--- a/server/src/lib.rs
+++ b/server/src/lib.rs
@@ -73,13 +73,17 @@ pub fn is_email_regex(test: &str) -> bool {
EMAIL_REGEX.is_match(test)
}
-pub fn is_image_content_type(test: &str) -> bool {
- match isahc::get(test) {
- Ok(res) => match res.headers().get("Content-Type") {
- Some(header) => header.to_str().unwrap_or("not_an_img").contains("image"),
- None => false,
- },
- Err(_) => false,
+pub fn is_image_content_type(test: &str) -> Result<(), failure::Error> {
+ if isahc::get(test)?
+ .headers()
+ .get("Content-Type")
+ .ok_or_else(|| format_err!("No Content-Type header"))?
+ .to_str()?
+ .starts_with("image/")
+ {
+ Ok(())
+ } else {
+ Err(format_err!("Not an image type."))
}
}
@@ -190,9 +194,7 @@ pub struct PictshareResponse {
}
pub fn fetch_pictshare(image_url: &str) -> Result<PictshareResponse, failure::Error> {
- if !is_image_content_type(image_url) {
- return Err(format_err!("Not an image type."));
- }
+ is_image_content_type(image_url)?;
let fetch_url = format!(
"http://pictshare/api/geturl.php?url={}",
@@ -276,10 +278,11 @@ mod tests {
#[test]
fn test_image() {
- assert!(is_image_content_type("https://1734811051.rsc.cdn77.org/data/images/full/365645/as-virus-kills-navajos-in-their-homes-tribal-women-provide-lifeline.jpg?w=600?w=650"));
- assert!(!is_image_content_type(
+ assert!(is_image_content_type("https://1734811051.rsc.cdn77.org/data/images/full/365645/as-virus-kills-navajos-in-their-homes-tribal-women-provide-lifeline.jpg?w=600?w=650").is_ok());
+ assert!(is_image_content_type(
"https://twitter.com/BenjaminNorton/status/1259922424272957440?s=20"
- ));
+ )
+ .is_err());
}
#[test]