From 5a6c0797d3a4e75af59ebca3fc8ce1834f718bb5 Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Sat, 22 Sep 2018 16:24:39 -0400 Subject: [PATCH] Implement tarball downloads --- gitsrht/blueprints/repo.py | 45 ++++++++++++++++++++++++++++++++++ gitsrht/templates/summary.html | 4 ++- 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/gitsrht/blueprints/repo.py b/gitsrht/blueprints/repo.py index 04b435e..c1719e6 100644 --- a/gitsrht/blueprints/repo.py +++ b/gitsrht/blueprints/repo.py @@ -1,5 +1,7 @@ +import os import pygit2 import pygments +import subprocess from datetime import datetime, timedelta from jinja2 import Markup from flask import Blueprint, render_template, abort, send_file @@ -182,3 +184,46 @@ def raw_blob(owner, repo, ref, path): return send_file(BytesIO(blob.data), as_attachment=blob.is_binary, attachment_filename=entry.name) + +@repo.route("///archive/") +def archive(owner, repo, ref): + 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) + ref, commit = resolve_ref(git_repo, ref) + + path = f"/tmp/{commit.id.hex}.tar.gz" + try: + args = [ + "git", + "--git-dir", repo.path, + "archive", + "--format=tar.gz", + "--prefix", f"{repo.name}-{ref}" + "-o", path, ref + ] + print(args) + subp = subprocess.run(args, timeout=30, + stdout=subprocess.PIPE, stderr=subprocess.PIPE) + except: + try: + os.unlink(path) + except: + pass + return "Error preparing archive", 500 + + if subp.returncode != 0: + print(subp.stdout, subp.stderr) + try: + os.unlink(path) + except: + pass + return "Error preparing archive", 500 + + f = open(path, "rb") + os.unlink(path) + return send_file(f, mimetype="application/tar+gzip", as_attachment=True, + attachment_filename=f"{repo.name}-{ref}.tar.gz") diff --git a/gitsrht/templates/summary.html b/gitsrht/templates/summary.html index 96b4c97..b8dcf89 100644 --- a/gitsrht/templates/summary.html +++ b/gitsrht/templates/summary.html @@ -45,7 +45,9 @@ browse {{icon("caret-right")}} - .tar.gz {{icon("caret-right")}} + .tar.gz {{icon("caret-right")}} announcement {{icon("caret-right")}} {% endif %} -- 2.38.4