Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor thumbor URL to fix compatibility with thumbor 7.5.0 #318

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions src/milia/utils/images.cljc
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
(ns milia.utils.images
(:require [chimera.urls :refer [url]]
[clojure.string :refer [starts-with?]]
[milia.utils.remote :refer [thumbor-server]]))

(defn resize-image
Expand All @@ -9,6 +10,11 @@
([image-url width-px height-px]
(resize-image image-url width-px height-px thumbor-server))
([image-url width-px height-px image-server-url]
(str image-server-url
(url "unsafe"
(str width-px "x" height-px) "smart" image-url))))
(let [thumbor-server-prefix (str image-server-url "/image/")]
(str image-server-url
(url
"unsafe"
(str width-px "x" height-px) "smart"
(if (starts-with? image-url thumbor-server-prefix)
(subs image-url (count thumbor-server-prefix))
image-url))))))
32 changes: 21 additions & 11 deletions test/clj/milia/utils/images_test.clj
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
(ns milia.utils.images-test
#_{:clj-kondo/ignore [:refer-all]}
(:require [midje.sweet :refer :all]
[milia.utils.images :refer :all]))
[milia.utils.images :refer [resize-image]]))

(facts "about resize-image"
(fact "should return with image-url and edge dimension"
(resize-image "s3.aws.org" 80) =>
"https://images.ona.io/unsafe/80x80/smart/s3.aws.org")
(facts
"about resize-image"
(fact "should return with image-url and edge dimension"
(resize-image "s3.aws.org" 80) =>
"https://images.ona.io/unsafe/80x80/smart/s3.aws.org")

(fact "should return with image-url, width and height dimension"
(resize-image "s3.aws.org" 80 56)
=> "https://images.ona.io/unsafe/80x56/smart/s3.aws.org")
(fact "should return with image-url, width and height dimension"
(resize-image "s3.aws.org" 80 56)
=> "https://images.ona.io/unsafe/80x56/smart/s3.aws.org")

(fact "should return with image-url, width and height dimension as well
(fact "should return with image-url, width and height dimension as well
as optional image-server-url"
(resize-image "s3.aws.org" 80 56 "https://images.test.org")
=> "https://images.test.org/unsafe/80x56/smart/s3.aws.org"))
(resize-image "s3.aws.org" 80 56 "https://images.test.org")
=> "https://images.test.org/unsafe/80x56/smart/s3.aws.org")
(fact
"Works with thumbor 7.5.0"
(resize-image
"https://images.test.org/image/some-hash/image-name.jpg"
80
56
"https://images.test.org")
=> "https://images.test.org/unsafe/80x56/smart/some-hash/image-name.jpg"))
Loading