Exemplo n.º 1
0
    def get_file_response(self, file_response: HapicFile, http_code: int):
        if file_response.file_path:
            from pyramid.response import FileResponse

            # TODO - G.M - 2019-03-27 - add support for overriding parameters of
            # file_response like content_length
            # Extended support for file response:
            # https://github.com/algoo/hapic/issues/171
            response = FileResponse(
                path=file_response.file_path,
                # INFO - G.M - 2018-09-13 - If content_type is no, mimetype
                # is automatically guessed
                content_type=file_response.mimetype or None,
            )
        else:
            from pyramid.response import FileIter
            from pyramid.response import Response

            response = Response(status=http_code)
            response.content_type = file_response.mimetype
            response.app_iter = FileIter(file_response.file_object)

        response.conditional_response = file_response.use_conditional_response
        if file_response.content_length:
            response.content_length = file_response.content_length
        if file_response.last_modified:
            response.last_modified = file_response.last_modified
        if file_response.etag:
            response.etag = file_response.etag

        response.status_code = http_code

        # INFO - G.M - 2020-06-03 - Accept bytes range if conditional_response
        # is accepted.
        if file_response.use_conditional_response:
            response.accept_ranges = "bytes"
        else:
            response.accept_ranges = "none"
        response.content_disposition = file_response.get_content_disposition_header_value(
        )

        return response
Exemplo n.º 2
0
    def _stream_file(self, filename, content_type):
        request = self.request
        if request.range:
            if request.range.start == 0 and request.range.end == None:
                # it's ff or chrome seeing if we support range requests, return
                # a smaller amount because initial requests from those browsers
                # drop the connection and throw any data we send anyway.
                request.range = 'bytes=0-4096'

        response = FileResponse(
            filename,
            request=self.request,
            content_type=content_type,
            cache_max_age=0,
        )
        response.accept_ranges = 'bytes'
        return response