From: Michael Tremer Date: Thu, 14 Jul 2022 15:18:36 +0000 (+0000) Subject: packages: Fix viewing files X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=a2c89f4cbacf6ebc565dbbf05ab6fbfbdf35569d;p=pbs.git packages: Fix viewing files Signed-off-by: Michael Tremer --- diff --git a/Makefile.am b/Makefile.am index b1347375..3bf33e36 100644 --- a/Makefile.am +++ b/Makefile.am @@ -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) diff --git a/configure.ac b/configure.ac index b1c35614..2998c1ce 100644 --- a/configure.ac +++ b/configure.ac @@ -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]) # ------------------------------------------------------------------------------ diff --git a/src/templates/packages/view-file.html b/src/templates/packages/view-file.html index ba5b53c6..831b4d48 100644 --- a/src/templates/packages/view-file.html +++ b/src/templates/packages/view-file.html @@ -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 %} - + -
= 1024 ** 2 %}class="prettyprint linenums"{% end %}>{{ content }}
+ {% module Highlight(payload) %} -

- {{ _("Mimetype: %s") % mimetype }} - {{ _("Size: %s") % format_size(filesize) }} - - {{ _("Download file") }} -

+ + {{ _("Download (%s)") % format_size(file.size) }} + {% end block %} diff --git a/src/web/__init__.py b/src/web/__init__.py index e02c8b8d..5c9afbbf 100644 --- a/src/web/__init__.py +++ b/src/web/__init__.py @@ -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 diff --git a/src/web/packages.py b/src/web/packages.py index 1dd88aae..e56ae0eb 100644 --- a/src/web/packages.py +++ b/src/web/packages.py @@ -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): diff --git a/src/web/ui_modules.py b/src/web/ui_modules.py index 55f3fb30..a0ddc5ef 100644 --- a/src/web/ui_modules.py +++ b/src/web/ui_modules.py @@ -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)