~rjarry/dlrepo

7d50989d3d91ad314f5cf25e8b95786f161249b2 — Julien Floret 7 months ago 75d45a1
fmt: add content disposition header to redirection

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>
1 files changed, 10 insertions(+), 11 deletions(-)

M dlrepo/views/fmt.py
M dlrepo/views/fmt.py => dlrepo/views/fmt.py +10 -11
@@ 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())