def serve_file(path_to_file: str,
               offered_filename: str = None,
               content_type: str = None,
               as_attachment: bool = False,
               as_inline: bool = False) -> HttpResponseBase:
    """
    Serve up a file from disk.
    Two methods (chosen by ``settings.XSENDFILE``):
    (a) serve directly
    (b) serve by asking the web server to do so via the X-SendFile directive.
    """
    # http://stackoverflow.com/questions/1156246/having-django-serve-downloadable-files  # noqa
    # https://docs.djangoproject.com/en/dev/ref/request-response/#telling-the-browser-to-treat-the-response-as-a-file-attachment  # noqa
    # https://djangosnippets.org/snippets/365/
    if offered_filename is None:
        offered_filename = os.path.basename(path_to_file) or ''
    if settings.XSENDFILE:
        response = HttpResponse()
        response['X-Sendfile'] = smart_str(path_to_file)
        content_length = os.path.getsize(path_to_file)
    else:
        response = FileResponse(open(path_to_file, mode='rb'))
        content_length = None
    add_http_headers_for_attachment(response,
                                    offered_filename=offered_filename,
                                    content_type=content_type,
                                    as_attachment=as_attachment,
                                    as_inline=as_inline,
                                    content_length=content_length)
    return response
def serve_file ( path_to_file : str , offered_filename : str = None , content_type : str = None , as_attachment : bool = False , as_inline : bool = False ) -> HttpResponseBase : if offered_filename is None : offered_filename = os . path . basename ( path_to_file ) or '' if settings . XSENDFILE : response = HttpResponse ( ) response [ 'X-Sendfile' ] = smart_str ( path_to_file ) content_length = os . path . getsize ( path_to_file ) else : response = FileResponse ( open ( path_to_file , mode = 'rb' ) ) content_length = None add_http_headers_for_attachment ( response , offered_filename = offered_filename , content_type = content_type , as_attachment = as_attachment , as_inline = as_inline , content_length = content_length ) return response
Serve up a file from disk.
    Two methods (chosen by ``settings.XSENDFILE``):
    (a) serve directly
    (b) serve by asking the web server to do so via the X-SendFile directive.