forked from gogobook/dockerizing-django
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
67 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,15 @@ | ||
from django.conf.urls import include, url | ||
from django.contrib import admin | ||
|
||
from utils.gridfs import prepare_mongodb_settings | ||
from utils.media import serve_from_storage | ||
|
||
__author__ = 'pahaz' | ||
|
||
prepare_mongodb_settings() | ||
|
||
urlpatterns = [ | ||
url(r'^admin/', include(admin.site.urls)), | ||
url(r'^media/(?P<path>.+)', serve_from_storage), | ||
url(r'^', include('todo.urls')), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
from mimetypes import guess_type | ||
from urllib.parse import unquote | ||
import os | ||
import posixpath | ||
|
||
from django.http import HttpResponseRedirect, FileResponse, Http404 | ||
from django.core.files.storage import default_storage as storage | ||
|
||
|
||
def _normpath(path): | ||
newpath = '' | ||
for part in path.split('/'): | ||
if not part: | ||
# Strip empty path components. | ||
continue | ||
_, part = os.path.splitdrive(part) | ||
_, part = os.path.split(part) | ||
if part in (os.curdir, os.pardir): | ||
# Strip '.' and '..' in path. | ||
continue | ||
newpath = os.path.join(newpath, part).replace('\\', '/') | ||
return newpath | ||
|
||
|
||
def serve_from_storage(request, path): | ||
path = posixpath.normpath(unquote(path)) | ||
path = path.lstrip('/') | ||
newpath = _normpath(path) | ||
if path != newpath: | ||
return HttpResponseRedirect(newpath) | ||
|
||
content_type, encoding = guess_type(path) | ||
content_type = content_type or 'application/octet-stream' | ||
|
||
try: | ||
file = storage.open(path) | ||
except Exception: # noqa | ||
raise Http404() | ||
response = FileResponse(file, content_type=content_type) | ||
# response["Last-Modified"] = http_date(statobj.st_mtime) | ||
response["Content-Length"] = storage.size(path) | ||
if encoding: | ||
response["Content-Encoding"] = encoding | ||
return response |