]> git.ipfire.org Git - pbs.git/commitdiff
packages: Fix viewing files
authorMichael Tremer <michael.tremer@ipfire.org>
Thu, 14 Jul 2022 15:18:36 +0000 (15:18 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Thu, 14 Jul 2022 15:18:36 +0000 (15:18 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
Makefile.am
configure.ac
src/templates/packages/view-file.html
src/web/__init__.py
src/web/packages.py
src/web/ui_modules.py

index b13473757095fa16137eefcc70717f17ddd813ce..3bf33e364eb9472334cab555ebe01a0af0a3cd77 100644 (file)
@@ -349,6 +349,7 @@ dist_static_DATA = \
 staticdir = $(datadir)/static
 
 static_css_DATA = \
+       src/static/css/highlight.css \
        src/static/css/site.css
 
 static_cssdir = $(staticdir)/css
@@ -461,6 +462,10 @@ UGLIFYJS_PROCESS = \
        $(AM_V_GEN)$(MKDIR_P) $(dir $@) && \
                $(SASSC) --style compressed $< > $@
 
+src/static/css/highlight.css: Makefile
+       $(AM_V_GEN)$(MKDIR_P) $(dir $@) && \
+               $(PYGMENTIZE) -S xcode -f html -a .highlight > $@
+
 %.min.js: %.js
        $(UGLIFYJS_PROCESS)
 
index b1c35614745f9b92d7d930f3d16c31ea3819557b..2998c1ce2fc437714dff45b6e203fb06c7fa4ff4 100644 (file)
@@ -54,6 +54,12 @@ AC_PROG_SED
 # Python
 AM_PATH_PYTHON([3.9])
 
+# pygmentize (from pygments)
+AC_CHECK_PROG(PYGMENTIZE, [pygmentize], [pygmentize])
+if test -z "${PYGMENTIZE}"; then
+       AC_MSG_ERROR([pygmentize is required])
+fi
+
 # SASSC
 AC_CHECK_PROG(SASSC, [sassc], [sassc])
 if test -z "${SASSC}"; then
@@ -77,6 +83,7 @@ m4_pattern_forbid([^_?PKG_[A-Z_]+$],[*** pkg.m4 missing, please install pkg-conf
 
 AX_PYTHON_MODULE([boto3], [fatal])
 AX_PYTHON_MODULE([location], [fatal])
+AX_PYTHON_MODULE([pygments], [fatal])
 
 # ------------------------------------------------------------------------------
 
index ba5b53c6ce7c4f39e9b7542a01d577e364769d6b..831b4d48ba2b7b5fa59ef1db8df70ec736ed683a 100644 (file)
@@ -1,53 +1,28 @@
 {% extends "../base.html" %}
 
-{% block title %}{{ _("View %(filename)s from %(pkg)s") % { "filename" : filename, "pkg" : pkg.friendly_name } }}{% end block %}
+{% block title %}{{ _("Package") }} - {{ package }} - {{ file }}{% end block %}
 
-{% block body %}
-       <ul class="breadcrumb">
-               <li>
-                       <a href="/">{{ _("Home") }}</a>
-                       <span class="divider">/</span>
-               </li>
-               <li>
-                       <a href="/packages">{{ _("Packages") }}</a>
-                       <span class="divider">/</span>
-               </li>
-               <li>
-                       <a href="/package/{{ pkg.name }}">{{ pkg.name }}</a>
-                       <span class="divider">/</span>
-               </li>
-               {% if pkg.build %}
+{% block container %}
+       <nav aria-label="{{ _("You are here:") }}" role="navigation">
+               <ul class="breadcrumbs">
                        <li>
-                               <a href="/build/{{ pkg.build.uuid }}">{{ pkg.friendly_version }}</a>
-                               <span class="divider">/</span>
+                               <a href="/">{{ _("Home") }}</a>
                        </li>
-               {% end %}
-               {% if pkg.job %}
                        <li>
-                               <a href="/job/{{ pkg.job.uuid }}">{{ pkg.job.arch }}</a>
-                               <span class="divider">/</span>
+                               <a href="/packages">{{ _("Packages") }}</a>
                        </li>
-               {% end %}
-               <li>
-                       <a href="/package/{{ pkg.uuid }}">{{ pkg.friendly_name }}</a>
-                       <span class="divider">/</span>
-               </li>
-               <li class="active">
-                       {{ _("View file") }}
-               </li>
-       </ul>
-
-       <div class="page-header">
-               <h2>
-                       {{ filename }}<br>
-                       <small><a href="/package/{{ pkg.uuid }}">{{ pkg.friendly_name }}</a> - {{ pkg.summary }}</small>
-               </h2>
-       </div>
+                       <li>
+                               <a href="/packages/{{ package.uuid }}">{{ package }}</a>
+                       </li>
+                       <li>
+                               <span class="show-for-sr">{{ _("Current") }}: </span> {{ file.name }}
+                       </li>
+               </ul>
+       </nav>
 
-       <pre {% if not filesize >= 1024 ** 2 %}class="prettyprint linenums"{% end %}>{{ content }}</pre>
+       {% module Highlight(payload) %}
 
-       <p class="muted ac">
-               {{ _("Mimetype: %s") % mimetype }} - {{ _("Size: %s") % format_size(filesize) }} -
-               <a href="/package/{{ pkg.uuid }}/download{{ filename }}">{{ _("Download file") }}</a>
-       </p>
+       <a class="expanded primary button" href="/package/{{ package.uuid }}/download{{ file.name }}">
+               {{ _("Download (%s)") % format_size(file.size) }}
+       </a>
 {% end block %}
index e02c8b8d80a8b726d262152389e0ead3d196a094..5c9afbbf449c1db3a8b164a148e54d8a4b9f30f8 100644 (file)
@@ -36,6 +36,7 @@ class Application(tornado.web.Application):
                        template_path = TEMPLATESDIR,
                        static_path = STATICDIR,
                        ui_modules = {
+                               "Highlight"          : ui_modules.HighlightModule,
                                "Text"               : ui_modules.TextModule,
                                "Modal"              : ui_modules.ModalModule,
 
@@ -136,7 +137,8 @@ class Application(tornado.web.Application):
                        (r"/packages/([\w\-\+]+)", packages.NameHandler),
                        (r"/package/([\w]{8}-[\w]{4}-[\w]{4}-[\w]{4}-[\w]{12})/download(.*)",
                                packages.FileDownloadHandler),
-                       (r"/package/([\w]{8}-[\w]{4}-[\w]{4}-[\w]{4}-[\w]{12})/view(.*)", packages.PackageFileViewHandler),
+                       (r"/package/([\w]{8}-[\w]{4}-[\w]{4}-[\w]{4}-[\w]{12})/view(.*)",
+                               packages.FileViewHandler),
                        (r"/package/([\w\-\+]+)/properties", packages.PackagePropertiesHandler),
 
                        # Files
index 1dd88aaeec23d17af3f200d86f040964ebe11518..e56ae0eb66f5725c83067e9cafa61ca492bba030 100644 (file)
@@ -120,19 +120,28 @@ class FileDownloadHandler(base.BaseHandler):
                self.finish(payload)
 
 
-class PackageFileViewHandler(FileDownloadHandler):
-       def get(self, pkg_uuid, filename):
-               pkg, f, mimetype = self.get_file(pkg_uuid, filename)
+class FileViewHandler(base.BaseHandler):
+       async def get(self, uuid, path):
+               package = self.backend.packages.get_by_uuid(uuid)
+               if not package:
+                       raise tornado.web.HTTPError(404, "Could not find package: %s" % uuid)
 
-               # Read in the data.
-               content = f.read()
-               f.close()
+               # Fetch the file
+               file = package.get_file(path)
+               if not file:
+                       raise tornado.web.HTTPError(404, "Could not find file %s in %s" % (path, package))
+
+               # Is this file viewable?
+               if not file.is_viewable():
+                       raise tornado.web.HTTPError(400, "%s cannot be viewed" % file)
 
                # These pages should not be indexed
                self.add_header("X-Robots-Tag", "noindex")
 
-               self.render("packages/view-file.html", pkg=pkg, filename=filename,
-                       mimetype=mimetype, content=content, filesize=f.size)
+               # Fetch payload
+               payload = await file.get_payload()
+
+               self.render("packages/view-file.html", package=package, file=file, payload=payload)
 
 
 class DependenciesModule(ui_modules.UIModule):
index 55f3fb30ec165b9160f2978140e86a2d2a7d9d2f..a0ddc5efc008038917f65c5016a64f1b20445959 100644 (file)
@@ -1,9 +1,10 @@
 #!/usr/bin/python
 
-
-
 import datetime
 import math
+import pygments
+import pygments.formatters
+import pygments.lexers
 import pytz
 import re
 import tornado.web
@@ -48,6 +49,25 @@ class TextModule(UIModule):
                return self.LINK % ("http://cve.mitre.org/cgi-bin/cvename.cgi?name=%s" % m.group(1), m.group(0))
 
 
+class HighlightModule(UIModule):
+       def render(self, text):
+               # Find a lexer
+               try:
+                       lexer = pygments.lexers.guess_lexer(text)
+               except pygments.util.ClassNotFound as e:
+                       lexer = pygments.lexers.special.TextLexer()
+
+               # Find a formatter
+               formatter = pygments.formatters.HtmlFormatter(linenos="table")
+
+               return pygments.highlight(text, lexer, formatter)
+
+       def css_files(self):
+               return (
+                       "css/highlight.css",
+               )
+
+
 class CommitMessageModule(UIModule):
        def render(self, commit):
                return self.render_string("modules/commit-message.html", commit=commit)