When issuing a GET or HEAD request on a format folder that contains a
single artifact file, the response is an HTTP redirection to that
file. In that case, add a "Content-Disposition" HTTP header to the
response, so that it can be used with e.g. "curl -JOL" to save the
downloaded file using the actual file name instead of that of the
format folder.
Signed-off-by: Julien Floret <julien.floret@6wind.com>
Acked-by: Thomas Faivre <thomas.faivre@6wind.com>
---
dlrepo/views/fmt.py | 21 ++++++++++-----------
1 file changed, 10 insertions(+), 11 deletions(-)
diff --git a/dlrepo/views/fmt.py b/dlrepo/views/fmt.py
index 0c48289b2e6f..5a2cb1105d2f 100644
--- a/dlrepo/views/fmt.py
+++ b/dlrepo/views/fmt.py
@@ -4,6 +4,7 @@
import asyncio
import logging
+import os
from typing import Callable
from aiohttp import web
@@ -149,16 +150,7 @@ class FormatFileView(BaseView):
yield "/~{user}/products/{product}/{variant}/{product_branch}/{version}/{format}"
async def head(self):
- fmt = _get_format(self.repo(), self.request.match_info, self.access_granted)
- if fmt.is_dirty():
- raise web.HTTPNotFound()
- url = fmt.url()
- files = list(fmt.get_digests().keys())
- if len(files) == 1:
- url += files[0]
- if url != self.request.path:
- raise web.HTTPFound(url)
- return web.Response()
+ return await self.get()
async def get(self):
"""
@@ -168,9 +160,16 @@ class FormatFileView(BaseView):
redirect to /branches/$branch/$tag/$job/$format/
"""
fmt = _get_format(self.repo(), self.request.match_info, self.access_granted)
+ if fmt.is_dirty():
+ raise web.HTTPNotFound()
files = list(fmt.get_digests().keys())
if len(files) == 1 and files[0] != "index.html":
- return web.HTTPFound(fmt.url() + files[0])
+ return web.HTTPFound(
+ fmt.url() + files[0],
+ headers={
+ "Content-Disposition": f"attachment; filename={os.path.basename(files[0])}"
+ },
+ )
return web.HTTPFound(fmt.url())
--
2.39.2
Julien Floret <julien.floret@6wind.com> wrote:
> When issuing a GET or HEAD request on a format folder that contains a
> single artifact file, the response is an HTTP redirection to that
> file. In that case, add a "Content-Disposition" HTTP header to the
> response, so that it can be used with e.g. "curl -JOL" to save the
> downloaded file using the actual file name instead of that of the
> format folder.
>
> Signed-off-by: Julien Floret <julien.floret@6wind.com>
> Acked-by: Thomas Faivre <thomas.faivre@6wind.com>
> ---
Acked-by: Robin Jarry <robin@jarry.cc>
Applied, thanks.
To git@git.sr.ht:~rjarry/dlrepo
75d45a112ab8..7d50989d3d91 main -> main