Skip to content

Commit

Permalink
Merge 7a58a05 into d024533
Browse files Browse the repository at this point in the history
  • Loading branch information
lifenautjoe committed Oct 12, 2020
2 parents d024533 + 7a58a05 commit 66a9083
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 7 deletions.
18 changes: 13 additions & 5 deletions video_encoding/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from django.core.files import File

from video_encoding.utils import get_fieldfile_local_path
from .backends import get_backend


Expand Down Expand Up @@ -41,9 +42,16 @@ def _get_video_info(self):
"""
if not hasattr(self, '_info_cache'):
encoding_backend = get_backend()
try:
path = os.path.abspath(self.path)
except AttributeError:
path = os.path.abspath(self.name)
self._info_cache = encoding_backend.get_media_info(path)

local_path, local_tmp_file = \
get_fieldfile_local_path(fieldfile=self)

info_cache = encoding_backend.get_media_info(local_path)

if local_tmp_file:
os.unlink(local_tmp_file.name)
local_tmp_file.close()

self._info_cache = info_cache

return self._info_cache
11 changes: 9 additions & 2 deletions video_encoding/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from django.contrib.contenttypes.models import ContentType
from django.core.files import File

from video_encoding.utils import get_fieldfile_local_path
from .backends import get_backend
from .config import settings
from .exceptions import VideoEncodingError
Expand Down Expand Up @@ -40,8 +41,10 @@ def convert_video(fieldfile, force=False):
instance = fieldfile.instance
field = fieldfile.field

filename = os.path.basename(fieldfile.path)
source_path = fieldfile.path
local_path, temp_file = get_fieldfile_local_path(fieldfile=fieldfile)

filename = os.path.basename(local_path)
source_path = local_path

encoding_backend = get_backend()

Expand Down Expand Up @@ -92,3 +95,7 @@ def convert_video(fieldfile, force=False):

# remove temporary file
os.remove(target_path)

if temp_file:
os.unlink(temp_file.name)
temp_file.close()
38 changes: 38 additions & 0 deletions video_encoding/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import os
import tempfile


def get_fieldfile_local_path(fieldfile):
local_temp_file = None

if hasattr(fieldfile, 'storage'):
storage = fieldfile.storage
try:
# Try to access with path
storage_local_path = storage.path(
fieldfile.path
)
except (NotImplementedError, AttributeError):
# Storage doesnt support absolute paths, download
# file to a temp local dir
storage_file = storage.open(
fieldfile.name, 'r'
)

local_temp_file = tempfile.NamedTemporaryFile(
delete=False
)
local_temp_file.write(storage_file.read())
local_temp_file.seek(0)

storage_local_path = local_temp_file.name
else:
# Its a local file with no storage abstraction
try:
path = os.path.abspath(fieldfile.path)
except AttributeError:
path = os.path.abspath(fieldfile.name)

storage_local_path = path

return storage_local_path, local_temp_file

0 comments on commit 66a9083

Please sign in to comment.