aboutsummaryrefslogtreecommitdiff
path: root/middleware/ignismiddleware.py
blob: e7cf0e82fa148c6f5a8c1299ea907844ee6ab23c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# Debug Only Middleware for Ignis Compatibility
from django.conf import settings
from django.http import HttpResponseRedirect


class IgnisRedirectMiddleware:
    def __init__(self, get_response):
        self.get_response = get_response

    def __call__(self, request):
        if settings.DEBUG and request.path.startswith("/ignis/"):
            new_url = f"https://shi.foo{request.path}"
            return HttpResponseRedirect(new_url)

        return self.get_response(request)


#  [DEPRECATED]
# # Ignis Middleware
# # Scans all 'img' links
# # if they start with '/ignis/<rest of the path>' > replaces them with env 'ignis.IGNIS_CACHE_ENDPOINT/<rest of the path>'
# # if they start with '/static/<rest of the path>' > replaces them with env 'static.INGIS_STATIC_ENDPOINT/<rest of the path>'

# import os
# import re
# from django.utils.deprecation import MiddlewareMixin
# from bs4 import BeautifulSoup
# from dotenv import load_dotenv

# load_dotenv()

# IGNIS_CACHE_ENDPOINT = os.getenv("IGNIS_CACHE_ENDPOINT")
# IGNIS_CACHE_PROTOCOL = os.getenv("IGNIS_CACHE_PROTOCOL")

# DYNAMIC_ENDPOINT = f"{IGNIS_CACHE_PROTOCOL}://ignis.{IGNIS_CACHE_ENDPOINT}"
# STATIC_ENDPOINT = f"{IGNIS_CACHE_PROTOCOL}://static.{IGNIS_CACHE_ENDPOINT}"


# class IgnisMiddleware(MiddlewareMixin):
#     def __init__(self, get_response):
#         self.get_response = get_response

#     def __call__(self, request):
#         response = self.get_response(request)

#         # if request is from localhost or 127.0.0.1, do not process
#         if re.match(r"^localhost", request.get_host()) or re.match(
#             r"^127.0.0.1", request.get_host()
#         ):
#             return response

#         # if request is made to /anidata or /admin, do not process
#         if re.match(r"^/anidata", request.path) or re.match(r"^/admin", request.path):
#             return response

#         # Do not process non-HTML responses
#         if not response["Content-Type"].startswith("text/html"):
#             return response

#         response.content = self.process_response(response)
#         return response

#     def process_response(self, response):
#         content = response.content.decode("utf-8")
#         soup = BeautifulSoup(content, "html.parser")

#         for image in soup.find_all("img"):
#             src = image.get("src")
#             if src.startswith("/ignis/"):
#                 image["src"] = f"{DYNAMIC_ENDPOINT}{src[6:]}"
#             elif src.startswith("/static/"):
#                 image["src"] = f"{STATIC_ENDPOINT}{src[7:]}"

#         return str(soup)