Skip to content

Commit

Permalink
Video._move_file (used when enabling/disabling permissions in a file)…
Browse files Browse the repository at this point in the history
… and Video._copy_file (when copying files via admin) first version. Still lots of problems.

I fear I have broken Video.formats somehow (unconfirmed)
  • Loading branch information
nmfm committed Oct 28, 2011
1 parent 0731efd commit 0e3e158
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 5 deletions.
83 changes: 82 additions & 1 deletion filer/models/videomodels.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
import os
import mimetypes
from django.db import models
from django.core.files.base import ContentFile
from django.utils.translation import ugettext_lazy as _
from filer import settings as filer_settings
from filer.models.filemodels import File
from filer.utils.video import convert_video, grab_poster, get_dimensions
from filer.utils.video import convert_video, grab_poster, get_dimensions, get_format_name


VIDEO_STATUS_TYPE = (
Expand Down Expand Up @@ -149,3 +150,83 @@ def convert(self):
error = error or res
output.append(out)
return error, "\n".join(output)

def _move_file(self):
"""
Move the file from src to dst.
"""
src_file_name = self.file.name
dst_file_name = self._meta.get_field('file').generate_filename(
self, self.original_filename)

if self.is_public:
src_storage = self.file.storages['private']
dst_storage = self.file.storages['public']
src_fmt_storage = self.file.format_storages['private']
dst_fmt_storage = self.file.format_storages['public']
else:
src_storage = self.file.storages['public']
dst_storage = self.file.storages['private']
src_fmt_storage = self.file.format_storages['public']
dst_fmt_storage = self.file.format_storages['private']
extensions = [f['format'] for f in self.formats]
if self.poster['filepath']:
extensions.append('png')

# delete the thumbnail
# We are toggling the is_public to make sure that easy_thumbnails can
# delete the thumbnails
self.is_public = not self.is_public
self.file.delete_thumbnails()
self.is_public = not self.is_public
# This is needed because most of the remote File Storage backend do not
# open the file.
src_file = src_storage.open(src_file_name)
src_file.open()
newfile = dst_storage.save(dst_file_name,
ContentFile(src_file.read()))
src_storage.delete(src_file_name)
for ext in extensions:
src_fmt_name = get_format_name(src_file_name, ext)
dst_fmt_name = get_format_name(newfile, ext)
src_file = src_fmt_storage.open(src_fmt_name)
src_file.open()
dst_fmt_storage.save(dst_fmt_name,
ContentFile(src_file.read()))
src_file.close()
src_fmt_storage.delete(src_fmt_name)
self.file = newfile

def _copy_file(self, destination, overwrite=False):
"""
Copies the file to a destination files and returns it.
"""
if overwrite:
# If the destination file already exists default storage backend
# does not overwrite it but generates another filename.
# TODO: Find a way to override this behavior.
raise NotImplementedError

src_file_name = self.file.name
stg_type = 'public' if self.is_public else 'private'
storage = self.file.storages[stg_type]
fmt_storage = self.file.format_storages[stg_type]
extensions = [f['format'] for f in self.formats]
if self.poster['filepath']:
extensions.append('png')

# This is needed because most of the remote File Storage backend do not
# open the file.
src_file = storage.open(src_file_name)
src_file.open()
newfile = storage.save(destination, ContentFile(src_file.read()))

for ext in extensions:
src_fmt_name = get_format_name(src_file_name, ext)
dst_fmt_name = get_format_name(newfile, ext)
src_file = fmt_storage.open(src_fmt_name)
src_file.open()
fmt_storage.save(dst_fmt_name, ContentFile(src_file.read()))
src_file.close()

return newfile
8 changes: 4 additions & 4 deletions filer/utils/video.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,21 @@
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)
path, original_ext = os.path.splitext(name)
return u'%s%s.%s' % (path, original_ext.replace('.', '_'), ext)


def format_to_original_filename(name):
filename, fmt_ext = os.path.splitext(name)
return LAST_UNDERSCORE_RE.sub('.', filename)


def check_ffmpeg_available():
status, output = execute_ffmpeg_command('ffmpeg','')
res = re.search('usage: ffmpeg', output)
return True if res else False


def get_dimensions(sourcefile):
"""Returns the video dimensions for a video file"""
ffmpeg = filer_settings.FFMPEG_CHECK_CMD % {'input_file': commands.mkarg(sourcefile)}
Expand Down

0 comments on commit 0e3e158

Please sign in to comment.