pathlib api stuff, zstd support and gracefull error handling for the get_cache_package
This commit is contained in:
parent
a16d37a2f8
commit
eaed69ad1c
2 changed files with 13 additions and 9 deletions
|
@ -39,7 +39,7 @@ class HistoryFileExtConverter:
|
||||||
|
|
||||||
|
|
||||||
class ArchiveFileExtConverter:
|
class ArchiveFileExtConverter:
|
||||||
regex = r'(tar|tar\.gz|tar\.xz)'
|
regex = r'tar(\.(gz|xz|zstd))?'
|
||||||
|
|
||||||
def to_python(self, value):
|
def to_python(self, value):
|
||||||
return value
|
return value
|
||||||
|
|
|
@ -8,6 +8,7 @@ from django.core.cache import cache
|
||||||
from django.core.exceptions import PermissionDenied
|
from django.core.exceptions import PermissionDenied
|
||||||
from django.http import Http404, HttpResponse, HttpResponseNotModified, StreamingHttpResponse
|
from django.http import Http404, HttpResponse, HttpResponseNotModified, StreamingHttpResponse
|
||||||
from django.shortcuts import get_object_or_404
|
from django.shortcuts import get_object_or_404
|
||||||
|
from django.utils.http import content_disposition_header
|
||||||
from django.views.decorators.http import etag
|
from django.views.decorators.http import etag
|
||||||
|
|
||||||
from c3nav.mapdata.middleware import no_language
|
from c3nav.mapdata.middleware import no_language
|
||||||
|
@ -168,15 +169,18 @@ def map_history(request, level, mode, filetype):
|
||||||
def get_cache_package(request, filetype):
|
def get_cache_package(request, filetype):
|
||||||
enforce_tile_secret_auth(request)
|
enforce_tile_secret_auth(request)
|
||||||
|
|
||||||
filename = settings.CACHE_ROOT / ('package.'+filetype)
|
filename = 'package.' + filetype
|
||||||
f = open(filename, 'rb')
|
cache_package = settings.CACHE_ROOT / filename
|
||||||
|
try:
|
||||||
f.seek(0, os.SEEK_END)
|
size = cache_package.stat().st_size
|
||||||
size = f.tell()
|
f = cache_package.open('rb')
|
||||||
f.seek(0)
|
except FileNotFoundError:
|
||||||
|
raise Http404
|
||||||
content_type = 'application/' + {'tar': 'x-tar', 'tar.gz': 'gzip', 'tar.xz': 'x-xz'}[filetype]
|
|
||||||
|
|
||||||
|
content_type = 'application/' + {'tar': 'x-tar', 'tar.gz': 'gzip', 'tar.xz': 'x-xz', 'tar.zst': 'zstd'}[filetype]
|
||||||
response = StreamingHttpResponse(FileWrapper(f), content_type=content_type)
|
response = StreamingHttpResponse(FileWrapper(f), content_type=content_type)
|
||||||
|
response.file_to_stream = f # This causes django to use the wsgi.file_wrapper if provided by the wsgi server.
|
||||||
response['Content-Length'] = size
|
response['Content-Length'] = size
|
||||||
|
if content_disposition := content_disposition_header(False, filename):
|
||||||
|
response["Content-Disposition"] = content_disposition
|
||||||
return response
|
return response
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue