Repos / pytaku / f96156b693
commit f96156b69341664d940f28b718738425d8109f74
Author: Bùi Thành Nhân <hi@imnhan.com>
Date:   Sun Aug 30 13:09:18 2020 +0700

    allow custom hostname for proxied urls
    
    Even with the filesystem cache, serving proxied images through the web
    worker is still suboptimal.
    
    This prefix allows me to optionally point all proxied urls to a CDN's
    hostname instead. I'm using BunnyCDN, for example.
    
    I'd prefer to solve this on the caddy server side instead, but
    apparently caddy server doesn't support the `file server that falls back
    to reverse proxy if file not found` use case.
    
    Of course everything should still work out of the box without any CDN by
    default.

diff --git a/src/pytaku/conf.py b/src/pytaku/conf.py
index fd4e2fd..3ceb731 100644
--- a/src/pytaku/conf.py
+++ b/src/pytaku/conf.py
@@ -7,6 +7,9 @@ class Config(GoodConf):
     MANGADEX_USERNAME = Value()
     MANGADEX_PASSWORD = Value()
     FLASK_SECRET_KEY = Value(initial=lambda: token_urlsafe(50))
+
+    PROXY_PREFIX = Value(default="")
+    # ^ use this to e.g. point to a CDN's domain
     PROXY_CACHE_DIR = Value(default="proxy_cache")
     PROXY_CACHE_MAX_SIZE = Value(default=1024 * 1024 * 1024)  # 1GiB in bytes
     PROXY_CACHE_MAX_AGE = Value(default=3600 * 24 * 7)  # 7 weeks in seconds
diff --git a/src/pytaku/main.py b/src/pytaku/main.py
index ec0749e..5cb3b6d 100644
--- a/src/pytaku/main.py
+++ b/src/pytaku/main.py
@@ -80,6 +80,11 @@ def _is_manga_img_url(
     return pattern.match(url)
 
 
+def proxied(url) -> str:
+    path = url_for("proxy_view", b64_url=_encode_proxy_url(url))
+    return config.PROXY_PREFIX + path
+
+
 @app.route("/proxy/<b64_url>")
 def proxy_view(b64_url):
     """
@@ -201,9 +206,7 @@ def _title(site, title_id, user_id=None):
         print("Loading title", title_id, "from db")
     title["cover"] = title_cover(site, title_id, title["cover_ext"])
     if site == "mangadex":
-        title["cover"] = url_for(
-            "proxy_view", b64_url=_encode_proxy_url(title["cover"])
-        )
+        title["cover"] = proxied(title["cover"])
     title["source_url"] = title_source_url(site, title_id)
     return title
 
@@ -237,9 +240,7 @@ def spa_chapter_view(site, title_id, chapter_id):
     title = load_title(site, title_id)
     title["cover"] = title_cover(site, title_id, title["cover_ext"])
     if site == "mangadex":
-        title["cover"] = url_for(
-            "proxy_view", b64_url=_encode_proxy_url(title["cover"])
-        )
+        title["cover"] = proxied(title["cover"])
 
     chapter["site"] = site
     return render_template(
@@ -273,18 +274,13 @@ def api_chapter(site, title_id, chapter_id):
         print("Loading chapter", chapter_id, "from db")
 
     if site in ("mangadex", "mangasee"):
-        chapter["pages"] = [
-            url_for("proxy_view", b64_url=_encode_proxy_url(p))
-            for p in chapter["pages"]
-        ]
+        chapter["pages"] = [proxied(p) for p in chapter["pages"]]
 
     # YIIIIKES
     title = load_title(site, title_id)
     title["cover"] = title_cover(site, title_id, title["cover_ext"])
     if site == "mangadex":
-        title["cover"] = url_for(
-            "proxy_view", b64_url=_encode_proxy_url(title["cover"])
-        )
+        title["cover"] = proxied(title["cover"])
     prev_chapter, next_chapter = get_prev_next_chapters(title, chapter)
     chapter["prev_chapter"] = prev_chapter
     chapter["next_chapter"] = next_chapter
@@ -367,7 +363,7 @@ def api_follows():
     for title in titles:
         thumbnail = title_thumbnail(title["site"], title["id"])
         if title["site"] == "mangadex":
-            thumbnail = url_for("proxy_view", b64_url=_encode_proxy_url(thumbnail))
+            thumbnail = proxied(thumbnail)
         title["thumbnail"] = thumbnail
     return jsonify({"titles": titles})
 
@@ -378,9 +374,7 @@ def api_search(query):
 
     if "mangadex" in results:
         for title in results["mangadex"]:
-            title["thumbnail"] = url_for(
-                "proxy_view", b64_url=_encode_proxy_url(title["thumbnail"])
-            )
+            title["thumbnail"] = proxied(title["thumbnail"])
     return results