]> git.ipfire.org Git - ipfire.org.git/commitdiff
docs: Render images (and other uploaded files)
authorMichael Tremer <michael.tremer@ipfire.org>
Tue, 27 Jun 2023 09:01:09 +0000 (09:01 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Tue, 27 Jun 2023 09:01:09 +0000 (09:01 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/backend/wiki.py
src/web/__init__.py
src/web/docs.py
src/web/wiki.py

index c392d1a032f542cc10e6507e206f9392e6d03d86..95849ee13979de6d8cf958fe7a4809a3e58ff11e 100644 (file)
@@ -614,11 +614,16 @@ class WikiRenderer(misc.Object):
        def _render_image(self, m):
                alt_text, url, caption = m.groups()
 
+               html = """
+                       <figure class="image">
+                               <img src="/docs%s" alt="%s">
+                               <figcaption class="figure-caption">%s</figcaption>
+                       </figure>
+               """
+
                # Skip any absolute and external URLs
                if url.startswith("/") or url.startswith("https://") or url.startswith("http://"):
-                       return """<figure class="figure"><img src="%s" class="figure-img img-fluid rounded" alt="%s">
-                               <figcaption class="figure-caption">%s</figcaption></figure>
-                       """ % (url, alt_text, caption or "")
+                       return html % (url, alt_text, caption or "")
 
                # Try to split query string
                url, delimiter, qs = url.partition("?")
@@ -638,9 +643,11 @@ class WikiRenderer(misc.Object):
                if not "s" in args:
                        args["s"] = "920"
 
-               return """<figure class="figure"><img src="%s?%s" class="figure-img img-fluid rounded" alt="%s">
-               <figcaption class="figure-caption">%s</figcaption></figure>
-               """ % (url, urllib.parse.urlencode(args), caption, caption or "")
+               # Append arguments to the URL
+               if args:
+                       url = "%s?%s" % (url, urllib.parse.urlencode(args))
+
+               return html % (url, caption, caption or "")
 
        def render(self, text):
                logging.debug("Rendering %s" % self.path)
index 5e95bc933155f67124820742ce67a8c868cd997f..91a8cdc23f10848edf549f99fd8b3c825de80479 100644 (file)
@@ -139,6 +139,7 @@ class Application(tornado.web.Application):
                        (r"/blog/([0-9a-z\-\._]+)/publish", blog.PublishHandler),
 
                        # Docs
+                       (r"/docs((?:[A-Za-z0-9\-_\/]+)?(?:.*)\.(?:\w+))$", docs.FileHandler),
                        (r"/docs([A-Za-z0-9\-_\/]+)?", docs.PageHandler),
 
                        # Downloads
@@ -353,7 +354,6 @@ class Application(tornado.web.Application):
 
                        # Media
                        (r"([A-Za-z0-9\-_\/]+)?/_files", wiki.FilesHandler),
-                       (r"((?!/static)(?:[A-Za-z0-9\-_\/]+)?(?:.*)\.(?:\w+))$", wiki.FileHandler),
 
                        # Serve any static files
                        (r"/static/(.*)", tornado.web.StaticFileHandler, { "path" : self.settings.get("static_path") }),
index 4f20b5d975241f96b4541b1114b196a0e562e9e1..01e168c86c7da206ae26b71a70686f53328235d8 100644 (file)
@@ -89,6 +89,55 @@ class PageHandler(base.BaseHandler):
                self.render("docs/page.html", page=page, latest_revision=latest_revision)
 
 
+class FileHandler(base.BaseHandler):
+       @property
+       def action(self):
+               return self.get_argument("action", None)
+
+       def get(self, path):
+               # Check permissions
+               if not self.backend.wiki.check_acl(path, self.current_user):
+                       raise tornado.web.HTTPError(403, "Access to %s not allowed for %s" % (path, self.current_user))
+
+               # Check if we are asked to render a certain revision
+               revision = self.get_argument("revision", None)
+
+               # Fetch the file
+               file = self.backend.wiki.get_file_by_path(path, revision=revision)
+               if not file:
+                       raise tornado.web.HTTPError(404, "Could not find %s" % path)
+
+               # Render detail page
+               if self.action == "detail":
+                       page = None
+
+                       for breadcrumb, title in self.backend.wiki.make_breadcrumbs(path):
+                               page = self.backend.wiki.get_page(breadcrumb)
+                               if page:
+                                       break
+
+                       self.render("wiki/files/detail.html", page=page, file=file)
+                       return
+
+               size = self.get_argument_int("s", None)
+
+               # Check if image should be resized
+               if size and file.is_bitmap_image():
+                       blob = file.get_thumbnail(size)
+               else:
+                       blob = file.blob
+
+               # Set headers
+               self.set_header("Content-Type", file.mimetype or "application/octet-stream")
+               self.set_header("Content-Length", len(blob))
+
+               # Set expires
+               self.set_expires(3600)
+
+               # Deliver content
+               self.finish(blob)
+
+
 class HeaderModule(ui_modules.UIModule):
        @property
        def page(self):
index 674ebba08429004fd8ec3c9f536f7d03717eb5b2..d9ba122abd7d7919ce6a016abacfb995f03051a9 100644 (file)
@@ -211,55 +211,6 @@ class FilesHandler(base.BaseHandler):
                self.render("wiki/files/index.html", path=path, files=files)
 
 
-class FileHandler(base.BaseHandler):
-       @property
-       def action(self):
-               return self.get_argument("action", None)
-
-       def get(self, path):
-               # Check permissions
-               if not self.backend.wiki.check_acl(path, self.current_user):
-                       raise tornado.web.HTTPError(403, "Access to %s not allowed for %s" % (path, self.current_user))
-
-               # Check if we are asked to render a certain revision
-               revision = self.get_argument("revision", None)
-
-               # Fetch the file
-               file = self.backend.wiki.get_file_by_path(path, revision=revision)
-               if not file:
-                       raise tornado.web.HTTPError(404, "Could not find %s" % path)
-
-               # Render detail page
-               if self.action == "detail":
-                       page = None
-
-                       for breadcrumb, title in self.backend.wiki.make_breadcrumbs(path):
-                               page = self.backend.wiki.get_page(breadcrumb)
-                               if page:
-                                       break
-
-                       self.render("wiki/files/detail.html", page=page, file=file)
-                       return
-
-               size = self.get_argument_int("s", None)
-
-               # Check if image should be resized
-               if size and file.is_bitmap_image():
-                       blob = file.get_thumbnail(size)
-               else:
-                       blob = file.blob
-
-               # Set headers
-               self.set_header("Content-Type", file.mimetype or "application/octet-stream")
-               self.set_header("Content-Length", len(blob))
-
-               # Set expires
-               self.set_expires(3600)
-
-               # Deliver content
-               self.finish(blob)
-
-
 class PageHandler(base.BaseHandler):
        @property
        def action(self):