aboutsummaryrefslogtreecommitdiff
path: root/lisp/mastodon-media.el
diff options
context:
space:
mode:
authormarty hiatt <martianhiatus@riseup.net>2024-08-08 14:05:10 +0200
committermarty hiatt <martianhiatus@riseup.net>2024-08-08 14:14:09 +0200
commit499c03aa783628ff5937b77cb48d3aeaa83f0ae3 (patch)
tree2c006ea4d27ca129c340f8ba00c62fea7b9faa54 /lisp/mastodon-media.el
parentb68a82b47206e6bb9b61e2326f6e0de299c57e96 (diff)
refactor process-image-or-cached
Diffstat (limited to 'lisp/mastodon-media.el')
-rw-r--r--lisp/mastodon-media.el38
1 files changed, 21 insertions, 17 deletions
diff --git a/lisp/mastodon-media.el b/lisp/mastodon-media.el
index 570be02..d386462 100644
--- a/lisp/mastodon-media.el
+++ b/lisp/mastodon-media.el
@@ -357,6 +357,20 @@ STATUS-PLIST is a plist of status events as per `url-retrieve'."
(switch-to-buffer-other-window (current-buffer))
(image-transform-fit-both))))))
+(defun mastodon-media--image-or-cached (url process-fun args)
+ "Fetch URL from cache or fro host.
+Call PROCESS-FUN on it with ARGS."
+ (if (and mastodon-media--enable-image-caching
+ (url-is-cached url)) ;; if cached, decompress and use:
+ (with-current-buffer (url-fetch-from-cache url)
+ (set-buffer-multibyte nil)
+ (goto-char (point-min))
+ (zlib-decompress-region
+ (goto-char (search-forward "\n\n")) (point-max))
+ (funcall process-fun url args))
+ ;; fetch as usual and process-image-response will cache it
+ (url-retrieve url process-fun (cdr args))))
+
(defun mastodon-media--load-image-from-url (url media-type start region-length)
"Take a URL and MEDIA-TYPE and load the image asynchronously.
MEDIA-TYPE is a symbol and either `avatar' or `media-link'.
@@ -373,24 +387,14 @@ REGION-LENGTH is the range from start to propertize."
(url-show-status nil)) ; stop url.el from spamming us about connecting
(condition-case nil
;; catch errors in url-retrieve to not break our caller
- (if (and mastodon-media--enable-image-caching
- (url-is-cached url)) ;; if cached, decompress and use:
- (with-current-buffer (url-fetch-from-cache url)
- (set-buffer-multibyte nil)
- (goto-char (point-min))
- (zlib-decompress-region
- (goto-char (search-forward "\n\n")) (point-max))
- (mastodon-media--process-image-response
- nil marker image-options region-length url))
- ;; else fetch as usual and process-image-response will cache it
- (url-retrieve url #'mastodon-media--process-image-response
- (list marker image-options region-length url)))
+ (mastodon-media--image-or-cached
+ url
+ #'mastodon-media--process-image-response
+ (list nil marker image-options region-length url))
(error (with-current-buffer buffer
- ;; TODO: Consider adding retries
- (put-text-property marker
- (+ marker region-length)
- 'media-state
- 'loading-failed)
+ ;; TODO: Add retries
+ (put-text-property marker (+ marker region-length)
+ 'media-state 'loading-failed)
:loading-failed)))))
(defun mastodon-media--select-next-media-line (end-pos)