Skip to content

Commit

Permalink
use gridfs as default file storage
Browse files Browse the repository at this point in the history
  • Loading branch information
pahaz committed Jul 10, 2016
1 parent ca577be commit a92def0
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 2 deletions.
17 changes: 15 additions & 2 deletions sources/_project_/settings.py
Expand Up @@ -21,6 +21,9 @@
DATA_DIR = normpath(os.environ.get('DATA_DIR', join(BASE_DIR, '__data__')))

REDIS_HOST = 'redis'
POSTGRES_HOST = os.environ.get('DB_SERVICE', 'postgres')
MONGO_HOST = '127.0.0.1'
DB_NAME = os.environ.get('DB_NAME', 'default')

# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.8/howto/deployment/checklist/
Expand Down Expand Up @@ -99,17 +102,25 @@
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': os.environ['DB_NAME'],
'NAME': DB_NAME,
'USER': os.environ['DB_USER'],
'PASSWORD': os.environ['DB_PASS'],
'HOST': os.environ['DB_SERVICE'],
'HOST': POSTGRES_HOST,
'PORT': os.environ['DB_PORT']
}
}
else:
raise RuntimeError('Bad django configuration. Invalid DATABASE type')


MONGODB_DATABASES = {
'default': {
'name': DB_NAME,
'serverSelectionTimeoutMS': 200,
'host': MONGO_HOST,
},
}

# Internationalization
# https://docs.djangoproject.com/en/1.8/topics/i18n/

Expand All @@ -126,6 +137,8 @@
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.8/howto/static-files/

MEDIA_URL = '/media/'
DEFAULT_FILE_STORAGE = 'utils.gridfs.GridFSStorage'
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(PROJECT_ROOT, 'static'),
Expand Down
8 changes: 8 additions & 0 deletions sources/_project_/urls.py
@@ -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')),
]
44 changes: 44 additions & 0 deletions sources/utils/media.py
@@ -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

0 comments on commit a92def0

Please sign in to comment.