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)

        if file_response.content_length:
            response.content_length = file_response.content_length
        if file_response.last_modified:
            response.last_modified = file_response.last_modified

        response.status_code = http_code
        response.content_disposition = file_response.get_content_disposition_header_value(
        )
        return response