From c667ae0afb0c75ab2291253b1d57ce6446c3ffc9 Mon Sep 17 00:00:00 2001 From: Peter Sanchez Date: Tue, 21 Jan 2020 10:30:53 -0800 Subject: [PATCH] Adding support for path variable when fetching blobs. --- gitsrht/blueprints/api.py | 14 +++++++++++++- gitsrht/blueprints/repo.py | 2 +- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/gitsrht/blueprints/api.py b/gitsrht/blueprints/api.py index 0853afa..b6b40b4 100644 --- a/gitsrht/blueprints/api.py +++ b/gitsrht/blueprints/api.py @@ -193,7 +193,12 @@ def repo_blob_GET(username, reponame, ref, path): user = get_user(username) repo = get_repo(user, reponame) + if "/" in ref: + ref, _, path = ref.partition("/") + with GitRepository(repo.path) as git_repo: + # lookup_ref will cycle through the path to separate + # the actual ref from the actual path commit, ref, path = lookup_ref(git_repo, ref, path) if not commit: abort(404) @@ -222,9 +227,16 @@ def repo_blob_GET(username, reponame, ref, path): if not blob: abort(404) + attachment_filename = entry.name if entry else None + if not attachment_filename: + if path: + attachment_filename = path.split("/")[-1] + else: + attachment_filename = blob.id.hex + ".bin" + return send_file(BytesIO(blob.data), as_attachment=blob.is_binary, - attachment_filename=entry.name if entry else blob.id.hex + ".bin", + attachment_filename=attachment_filename, mimetype="text/plain" if not blob.is_binary else "application/x-octet-stream") diff --git a/gitsrht/blueprints/repo.py b/gitsrht/blueprints/repo.py index ea3a151..df23874 100644 --- a/gitsrht/blueprints/repo.py +++ b/gitsrht/blueprints/repo.py @@ -128,7 +128,7 @@ def go_get(owner, repo, path): def lookup_ref(git_repo, ref, path): ref = ref or git_repo.default_branch().name[len("refs/heads/"):] - if path is None: + if not path: path = [] else: path = path.split("/") -- 2.38.4