Permalink
Browse files

Serving video formats from the private server

  • Loading branch information...
1 parent 73e625a commit 08cd171e223e985b7f4a9cc174d1fb86d6836584 @nmfm committed Oct 28, 2011
Showing with 57 additions and 20 deletions.
  1. +12 −15 filer/fields/multistorage_file.py
  2. +1 −1 filer/models/videomodels.py
  3. +3 −0 filer/server/urls.py
  4. +27 −0 filer/server/views.py
  5. +14 −4 filer/utils/video.py
@@ -2,11 +2,12 @@
import os
from django.core.files.base import File
from django.core.files.storage import Storage
+from django.db.models.fields.files import FieldFile
from easy_thumbnails import fields as easy_thumbnails_fields, \
files as easy_thumbnails_files
from filer import settings as filer_settings
from filer.utils.filer_easy_thumbnails import ThumbnailerNameMixin
-
+from filer.utils.video import get_format_name as get_video_name
STORAGES = {
'public': filer_settings.FILER_PUBLICMEDIA_STORAGE,
@@ -36,22 +37,24 @@ def generate_filename_multistorage(instance, filename):
class FormatNameMixin(object):
def get_format_name(self, ext):
- path, source_filename = os.path.split(self.name)
- filename, extension = os.path.splitext(source_filename)
- newfilename = '%s.%s' % (filename, ext)
- return os.path.join(path, newfilename)
+ return get_video_name(self.name, ext)
def get_format_url(self, ext):
base_name = self.get_format_name(ext)
if self.format_storage.exists(base_name):
- return os.path.join(self.format_storage.base_url, base_name)
+ return self.format_storage.url(base_name)
else:
raise NameError
def get_poster_url(self):
- original_path, filename = os.path.split(self.name)
- basename = os.path.splitext(filename)[0]
- return self.format_storage.url(os.path.join(original_path, basename + '.png'))
+ return self.get_format_url('png')
+
+
+class FormatFieldFile(FieldFile):
+ """Used when serving formats through serve_protected_format"""
+ def __init__(self, instance, field, name, storage):
+ super(FormatFieldFile, self).__init__(instance, field, name)
+ self.storage = storage
class MultiStorageFieldFile(ThumbnailerNameMixin,
@@ -94,10 +97,6 @@ def format_storage(self):
else:
return self.format_storages['private']
- def get_format(self, options, save=True):
- #wtf is opaque vs transparent???
- pass
-
class MultiStorageFileField(easy_thumbnails_fields.ThumbnailerField):
attr_class = MultiStorageFieldFile
@@ -112,5 +111,3 @@ def __init__(self, verbose_name=None, name=None, upload_to_dict=None,
verbose_name=verbose_name, name=name,
upload_to=generate_filename_multistorage,
storage=None, **kwargs)
-
- # make versions devia estar aki!!!!
@@ -93,7 +93,7 @@ def formats_html5(self):
_formats = []
for fmt, url in self.formats.items():
if fmt in HTML5_FORMATS:
- _formats.append({'format': fmt, 'url': url, 'mimetype': HTML5_FORMATS[format]})
+ _formats.append({'format': fmt, 'url': url, 'mimetype': HTML5_FORMATS[fmt]})
return _formats
def format_flash(self):
View
@@ -8,4 +8,7 @@
url(r'^' + filer_settings.FILER_PRIVATEMEDIA_THUMBNAIL_STORAGE.base_url.lstrip('/') + r'(?P<path>.*)$',
'serve_protected_thumbnail',),
+
+ url(r'^' + filer_settings.FILER_PRIVATEMEDIA_FORMATS_STORAGE.base_url.lstrip('/') + r'(?P<path>.*)$',
+ 'serve_protected_format',),
)
View
@@ -4,11 +4,14 @@
from django.http import Http404
from easy_thumbnails.files import ThumbnailFile
from filer import settings as filer_settings
+from filer.fields.multistorage_file import FormatFieldFile
from filer.models import File
from filer.utils.filer_easy_thumbnails import thumbnail_to_original_filename
+from filer.utils.video import format_to_original_filename
server = filer_settings.FILER_PRIVATEMEDIA_SERVER
thumbnail_server = filer_settings.FILER_PRIVATEMEDIA_THUMBNAIL_SERVER
+format_server = filer_settings.FILER_PRIVATEMEDIA_FORMATS_SERVER
def serve_protected_file(request, path):
@@ -49,3 +52,27 @@ def serve_protected_thumbnail(request, path):
return thumbnail_server.serve(request, thumbnail, save_as=False)
except Exception:
raise Http404('File not found')
+
+
+def serve_protected_format(request, path):
+ """
+ Serve protected video formats to authenticated users.
+ If the user doesn't have read permissions, redirect to a static image.
+ """
+ source_path = format_to_original_filename(path)
+ if not source_path:
+ raise Http404('File not found')
+ try:
+ thefile = File.objects.get(file=source_path, is_public=False)
+ except File.DoesNotExist:
+ raise Http404('File not found')
+ if not thefile.has_read_permission(request):
+ if settings.DEBUG:
+ raise PermissionDenied
+ else:
+ raise Http404('File not found')
+ try:
+ fmt = FormatFieldFile(name=path, field=thefile.file, instance=thefile, storage=thefile.file.format_storage)
+ return format_server.serve(request, fmt, save_as=False)
+ except Exception:
+ raise Http404('File not found')
View
@@ -12,6 +12,18 @@
from filer import settings as filer_settings
FFMPEG_DIMENSIONS_RE = re.compile(r'Stream.*Video.*([0-9]{3,})x([0-9]{3,})')
+LAST_UNDERSCORE_RE = re.compile(r'\_(?=[^_]*$)')
+
+def get_format_name(name, ext):
+ path, source_filename = os.path.split(name)
+ filename, original_ext = os.path.splitext(source_filename)
+ newfilename = u'%s%s.%s' % (filename, original_ext.replace('.', '_'), ext)
+ return os.path.join(path, newfilename)
+
+
+def format_to_original_filename(name):
+ filename, fmt_ext = os.path.splitext(name)
+ return LAST_UNDERSCORE_RE.sub('.', filename)
def get_dimensions(sourcefile):
@@ -61,8 +73,7 @@ def convert_video(sourcefile, path, extension, dimensions):
original_path, filename = os.path.split(sourcefile)
if sourcefile is None:
return True, "Sourcefile does not exist"
- filebase, original_ext = os.path.splitext(filename)
- convfilename = "%s.%s" % (filebase, extension)
+ convfilename = get_format_name(filename, extension)
targetfile = os.path.join(path, convfilename)
if not os.path.exists(path):
os.makedirs(path)
@@ -80,8 +91,7 @@ def grab_poster(sourcefile, path, dimensions):
original_path, filename = os.path.split(sourcefile)
if sourcefile is None:
return True, "Sourcefile does not exist"
- filebase, original_ext = os.path.splitext(filename)
- thumbnailfilename = "%s.png" % filebase
+ thumbnailfilename = get_format_name(filename, 'png')
thumbnailfile = os.path.join(path, thumbnailfilename)
if not os.path.exists(path):
os.makedirs(path)

0 comments on commit 08cd171

Please sign in to comment.