diff options
| author | eltrac <hi@eltr.ac> | 2026-10-13 17:19:44 +0800 |
|---|---|---|
| committer | eltrac <hi@eltr.ac> | 2026-10-13 17:19:44 +0800 |
| commit | 5d851c82f9575f1709ddeb559c48f9daad449bbd (patch) | |
| tree | 33fe13105793720f167d435b5c88da9697e9dbf9 /src | |
| parent | e26cc530c5e51154117883894ff5779c8a8e3ba3 (diff) | |
wrapper: fix not downloading photo with relative urls
Diffstat (limited to 'src')
| -rw-r--r-- | src/weepinbell/processor/wrapper.clj | 20 | ||||
| -rw-r--r-- | src/weepinbell/utils/url.clj | 8 |
2 files changed, 21 insertions, 7 deletions
diff --git a/src/weepinbell/processor/wrapper.clj b/src/weepinbell/processor/wrapper.clj index 202ac02..8e2a8e2 100644 --- a/src/weepinbell/processor/wrapper.clj +++ b/src/weepinbell/processor/wrapper.clj @@ -3,7 +3,7 @@ It adds microformats and extra data and return complete webmention map ready for storage."
(:require [clojure.java.io :as io]
[weepinbell.http.downloader :refer [download-file]]
- [weepinbell.utils.url :refer [valid-http-url?]]
+ [weepinbell.utils.url :refer [valid-http-url? get-base relative? normalize]]
[weepinbell.microformats.searcher :refer [use-items search-photo find-h-entry find-h-card]]
[mf2clj.core :as mf-parser]
[taoensso.timbre :as timbre]
@@ -38,7 +38,7 @@ (or (->> mf use-items find-h-entry search-photo)
(->> mf use-items find-h-card search-photo)))
-(defn- proxy-mf-photo [raw-photo base-url]
+(defn- proxy-mf-photo [photo-base raw-photo]
(if raw-photo
(let [photo-url (str raw-photo)
file-ext (or (re-find #"\.(?:jpg|jpeg|png|gif|webp)" photo-url) ".jpg")
@@ -49,22 +49,28 @@ (when-not (.exists (io/file local-path))
(timbre/info "Downloading microformats photo to proxy external image.")
(download-file photo-url local-path))
- (str base-url public-path))
+ (str photo-base public-path))
nil))
+(defn complete-url [source-base url]
+ (if (relative? url)
+ (normalize (str source-base url))
+ url))
+
(defn- wrap-webmention
"Parse microformats and collect essential info.
Wrap source, target and collected data into a map and return it."
- [id source-html source target base-url]
+ [id source-html source target photo-base]
(timbre/info "parsing microformats and wrapping up webmention" id)
(let [mf (-> source-html mf-parser/parse :items)
wm {:source source
:target target
:id (digest (str source target))
:modified (System/currentTimeMillis)}
- proxied-photo (-> mf
- find-mf-photo
- (proxy-mf-photo base-url))
+ proxied-photo (->> mf
+ find-mf-photo
+ (complete-url (get-base source))
+ (proxy-mf-photo photo-base))
data {:webmention wm
:microformats mf
:extra {:proxied-photo proxied-photo}}]
diff --git a/src/weepinbell/utils/url.clj b/src/weepinbell/utils/url.clj index a0de304..265509c 100644 --- a/src/weepinbell/utils/url.clj +++ b/src/weepinbell/utils/url.clj @@ -16,6 +16,10 @@ (catch MalformedURLException _ false)
(catch URISyntaxException _ false)))
+(defn relative? [url]
+ (or (string/starts-with? url "/")
+ (string/starts-with? url ".")))
+
(defn get-domain [url]
(when (some? url)
(-> url URI/create .getHost)))
@@ -24,6 +28,10 @@ (when (some? url)
(-> url URI/create .getPath)))
+(defn get-base [url]
+ (let [uri (URI. url)]
+ (str (.getScheme uri) "://" (.getAuthority uri))))
+
(defn get-target [wm]
(get-in wm [:webmention :target]))
|
