Exemplo n.º 1
0
def index_html(request):
    frontend_build = get_frontend_build()
    fn = os.path.join(frontend_build, 'index.html')
    response = FileResponse(fn,
                            request=request,
                            content_type='text/html;charset=utf-8')
    response.cache_control = 'no-store, no-cache'
    return response
Exemplo n.º 2
0
def make_static_file_response(fn, request, cache_control='no-store'):
    pos = fn.rfind('.')
    if pos >= 0:
        ext = fn[pos:]
    else:
        ext = ''

    if not os.path.exists(fn):
        raise HTTPNotFound()

    if (ext not in ('.jpg', '.png', '.gif')
            and 'gzip' in request.headers.get('Accept-Encoding', '')):
        mtime = os.path.getmtime(fn)
        size = os.path.getsize(fn)
        gzipped = gzip_cache.get(fn)

        if gzipped:
            if mtime != gzipped['mtime'] or size != gzipped['size']:
                gzipped = None

        if not gzipped:
            f = open(fn, 'rb')
            content = f.read()
            f.close()
            gzipped = {
                'mtime': mtime,
                'size': size,
                'gzipped_content': gzip.compress(content),
            }
            gzip_cache[fn] = gzipped

        if gzipped:
            content_type, _ = mimetypes.guess_type(fn, strict=False)
            if content_type is None:
                content_type = 'application/octet/stream'
            body = gzipped['gzipped_content']
            return Response(body=body,
                            conditional_response=True,
                            content_type=content_type,
                            content_encoding='gzip',
                            last_modified=mtime,
                            content_length=len(body),
                            cache_control=cache_control)

    response = FileResponse(fn, request=request)
    response.cache_control = cache_control
    return response