From 4981524c9215359a5667a676a679ae1bc699921d Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Sat, 22 Sep 2018 13:35:43 -0400 Subject: [PATCH] Add raw blob downloads --- gitsrht/blueprints/repo.py | 50 +++++++++++++++++++++++++++++++++++-- gitsrht/templates/blob.html | 30 ++++++++++++++++++++++ 2 files changed, 78 insertions(+), 2 deletions(-) diff --git a/gitsrht/blueprints/repo.py b/gitsrht/blueprints/repo.py index d4e4f94..cb6db3b 100644 --- a/gitsrht/blueprints/repo.py +++ b/gitsrht/blueprints/repo.py @@ -2,12 +2,13 @@ import pygit2 import pygments from datetime import datetime, timedelta from jinja2 import Markup -from flask import Blueprint, render_template, abort +from flask import Blueprint, render_template, abort, send_file from flask_login import current_user from gitsrht.access import get_repo, has_access, UserAccess from gitsrht.redis import redis from gitsrht.git import CachedRepository, commit_time, annotate_tree from gitsrht.types import User, Repository +from io import BytesIO from pygments import highlight from pygments.lexers import guess_lexer_for_filename, TextLexer from pygments.formatters import HtmlFormatter @@ -116,7 +117,12 @@ def tree(owner, repo, branch, path): tree = annotate_tree(git_repo, tree, commit) commit = next(e.commit for e in tree if e.name == entry.name) blob = git_repo.get(entry.id) - data = blob.data.decode() + data = None + if not blob.is_binary: + try: + data = blob.data.decode() + except: + pass return render_template("blob.html", view="tree", owner=owner, repo=repo, branch=branch, path=path, branch_name=branch_name, entry=entry, blob=blob, data=data, @@ -129,3 +135,43 @@ def tree(owner, repo, branch, path): return render_template("tree.html", view="tree", owner=owner, repo=repo, branch=branch, branch_name=branch_name, commit=commit, tree=tree, path=path) + +@repo.route("///blob//") +def raw_blob(owner, repo, branch, path): + owner, repo = get_repo(owner, repo) + if not repo: + abort(404) + if not has_access(repo, UserAccess.read): + abort(401) + git_repo = CachedRepository(repo.path) + if branch is None: + branch = git_repo.default_branch() + else: + branch = git_repo.branches.get(branch) + if not branch: + abort(404) + branch_name = branch.name[len("refs/heads/"):] + commit = git_repo.get(branch.target) + + blob = None + entry = None + tree = commit.tree + path = path.split("/") + for part in path: + if part == "": + continue + if part not in tree: + abort(404) + entry = tree[part] + if entry.type == "blob": + tree = annotate_tree(git_repo, tree, commit) + commit = next(e.commit for e in tree if e.name == entry.name) + blob = git_repo.get(entry.id) + break + tree = git_repo.get(entry.id) + + if not blob: + abort(404) + + return send_file(BytesIO(blob.data), + as_attachment=blob.is_binary, attachment_filename=entry.name) diff --git a/gitsrht/templates/blob.html b/gitsrht/templates/blob.html index 00d0f90..9c33d85 100644 --- a/gitsrht/templates/blob.html +++ b/gitsrht/templates/blob.html @@ -13,6 +13,20 @@ path=path_join(*path[:loop.index]))}}" >{{part}}/{% endif %}{% endfor %} + + {{stat.filemode(entry.filemode)}} + + + {{humanize.naturalsize(blob.size, + binary=True).replace("Byte", "byte")}} + + + + View raw + +
{{commit.id.hex[:8]}} — @@ -26,6 +40,7 @@