Skip to content

Commit

Permalink
Fixup image test and remove cemerick.url
Browse files Browse the repository at this point in the history
  • Loading branch information
devth committed Apr 19, 2023
1 parent 83dc4aa commit 922361b
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 22 deletions.
2 changes: 0 additions & 2 deletions project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,6 @@
[dec "1.0.1"]
; retry
[robert/bruce "0.8.0"]
;; TODO remove com.cemerick/url in favor of lambdaisland/uri
[com.cemerick/url "0.1.1"]
[lambdaisland/uri "1.15.125"]
;; [io.aviso/pretty "0.1.34"] ; pretty stacktraces

Expand Down
6 changes: 0 additions & 6 deletions src/yetibot/core/commands/url.clj
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,12 @@
(:require
[clojure.spec.alpha :as s]
[yetibot.core.config :refer [get-config]]
[cemerick.url :refer [url]]
[yetibot.core.hooks :refer [cmd-hook]]))

(s/def ::config string?)

(defn config [] (get-config ::config [:url]))

(defn yetibot-url
"Given a path build a fully qualified URL"
[& paths]
(str (apply url (config) paths)))

(defn url-cmd
"url # get Yetibot's configured web address"
[_]
Expand Down
29 changes: 15 additions & 14 deletions src/yetibot/core/util.clj
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
(ns yetibot.core.util
(:require
[clojure.string :as s]
[cemerick.url :refer [url]]))
[clojure.string :as s]
[lambdaisland.uri :refer [uri query-map]]
[taoensso.timbre :refer [info]]))

(defn filter-nil-vals
"Takes a map and returns all of its non-nil values"
Expand Down Expand Up @@ -132,25 +133,25 @@

(defn image?
"Simple utility to detect if a URL represents an image purely by looking at
the file extension (i.e. not too smart)."
the file extension (i.e. not too smart)."
[possible-url]
(try
(let [{:keys [query path]} (url possible-url)]
(let [{:keys [path]} (uri possible-url)
query (query-map possible-url)]
(prn {:query query :path path})
(or
(re-find image-pattern path)
; some platforms like slack embed the url in a query param, e.g.
; https://slack-imgs.com/?c=1&o1=ro&url=https%3A%2F%2Fi.imgflip.com%2F7infdj.jpg
(re-find image-pattern (get query "url"))
; we indicate images from Wolfram are jpgs by tossing a &t=.jpg on it
(= ".jpg" (get query "t"))))
(catch Exception _
false)))
(re-find image-pattern (or (:url query) ""))
; we indicate images from Wolfram are jpgs by tossing a &t=.jpg on it
(= ".jpg" (:t query))))
(catch Exception e
(do
(info "Failed to parse possible image URL:" possible-url e)
false))))

(comment
(-> (url "https://slack-imgs.com/?c=1&o1=ro&url=https%3A%2F%2Fi.imgflip.com%2F7infdj.jpg")
:query
(get "url"))
(image? "https://i.imgflip.com/2v045r.jpg")
(image? "https://i.imgflip.com/2v045r.jpg?foo=bar")
(image? "http://www5b.wolframalpha.com/Calculate/MSP/MSP6921ei892gfhh9i9649000058fg83ii266d342i?MSPStoreType=image/gif&s=46&t=.jpg")
)
(image? "https://www6b3.wolframalpha.com/Calculate/MSP/MSP910019f0hg0cd56717360000651930h50d32cehf?MSPStoreType=image/gif&s=18"))

0 comments on commit 922361b

Please sign in to comment.