Skip to content

Commit

Permalink
Merge pull request #318 from onaio/changes-to-work-with-thumbor-7.5.0
Browse files Browse the repository at this point in the history
Refactor thumbor URL to fix compatibility with thumbor 7.5.0
  • Loading branch information
FrankApiyo authored Sep 19, 2023
2 parents 34254e5 + fc97378 commit e32a95a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 14 deletions.
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"))

0 comments on commit e32a95a

Please sign in to comment.