From 11f1b4fe90d202082ba82340665b5416a6cff202 Mon Sep 17 00:00:00 2001 From: mvdbeek Date: Thu, 23 Nov 2023 12:29:15 +0100 Subject: [PATCH] Add HEAD route to job_files endpoint --- lib/galaxy/web/framework/base.py | 6 +++++- lib/galaxy/webapps/galaxy/buildapp.py | 8 ++++++++ test/integration/test_job_files.py | 4 ++++ 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/lib/galaxy/web/framework/base.py b/lib/galaxy/web/framework/base.py index 70b5bb769e24..baa1a07128e4 100644 --- a/lib/galaxy/web/framework/base.py +++ b/lib/galaxy/web/framework/base.py @@ -548,6 +548,9 @@ def send_file(start_response, trans, body): trans.response.headers["accept-ranges"] = "bytes" start = None end = None + if trans.request.method == "HEAD": + trans.response.headers["content-length"] = os.path.getsize(body.name) + body = b"" if trans.request.range: start = int(trans.request.range.start) file_size = int(trans.response.headers["content-length"]) @@ -555,7 +558,8 @@ def send_file(start_response, trans, body): trans.response.headers["content-length"] = str(end - start) trans.response.headers["content-range"] = f"bytes {start}-{end - 1}/{file_size}" trans.response.status = 206 - body = iterate_file(body, start, end) + if body: + body = iterate_file(body, start, end) start_response(trans.response.wsgi_status(), trans.response.wsgi_headeritems()) return body diff --git a/lib/galaxy/webapps/galaxy/buildapp.py b/lib/galaxy/webapps/galaxy/buildapp.py index 4bf0b12179ba..b541ffcc7ecd 100644 --- a/lib/galaxy/webapps/galaxy/buildapp.py +++ b/lib/galaxy/webapps/galaxy/buildapp.py @@ -1061,6 +1061,14 @@ def connect_invocation_endpoint(endpoint_name, endpoint_suffix, action, conditio parent_resources=dict(member_name="job", collection_name="jobs"), ) + webapp.mapper.connect( + "index", + "/api/jobs/{job_id}/files", + controller="job_files", + action="index", + conditions=dict(method=["HEAD"]), + ) + webapp.mapper.resource( "port", "ports", diff --git a/test/integration/test_job_files.py b/test/integration/test_job_files.py index fd933e720ea9..a05b0c11b6ac 100644 --- a/test/integration/test_job_files.py +++ b/test/integration/test_job_files.py @@ -61,6 +61,10 @@ def test_read_by_state(self): job_id, job_key = self._api_job_keys(job) data = {"path": self.input_hda.file_name, "job_key": job_key} get_url = self._api_url(f"jobs/{job_id}/files", use_key=True) + head_response = requests.head(get_url, params=data) + api_asserts.assert_status_code_is_ok(head_response) + assert head_response.text == "" + assert head_response.headers["content-length"] == str(len(TEST_INPUT_TEXT)) response = requests.get(get_url, params=data) api_asserts.assert_status_code_is_ok(response) assert response.text == TEST_INPUT_TEXT