Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 16 additions & 14 deletions cds/modules/deposit/ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
from invenio_records_files.utils import sorted_files_from_bucket
from srt_to_vtt import srt_to_vtt

from ..flows.files import move_file_into_local
from ..invenio_deposit.signals import post_action
from .indexer import cdsdeposit_indexer_receiver
from .receivers import (
Expand Down Expand Up @@ -68,25 +69,26 @@ def _create_vtt_from_srt(srt_obj):
if not srt_obj.file or not srt_obj.file.uri:
return None

srt_path = srt_obj.file.uri
tmp_dir = None
try:
# Create temporary directory for VTT file
# Create temporary directory for SRT and VTT file
tmp_dir = tempfile.mkdtemp()
vtt_path = os.path.join(tmp_dir, vtt_key)

# Convert using srt-to-vtt library
srt_to_vtt(srt_path, vtt_path)

# Create VTT ObjectVersion
vtt_obj = ObjectVersion.create(
bucket=srt_obj.bucket,
key=vtt_key,
stream=open(vtt_path, "rb"),
size=os.path.getsize(vtt_path),
)
_create_tags(vtt_obj)
return vtt_obj
with move_file_into_local(srt_obj, tmp_dir=tmp_dir) as local_srt_path:
# Convert using srt-to-vtt library
srt_to_vtt(local_srt_path, vtt_path)

# Create VTT ObjectVersion
vtt_obj = ObjectVersion.create(
bucket=srt_obj.bucket,
key=vtt_key,
stream=open(vtt_path, "rb"),
size=os.path.getsize(vtt_path),
)
_create_tags(vtt_obj)

return vtt_obj
except (OSError, IOError, AttributeError, Exception):
return None
finally:
Expand Down
6 changes: 4 additions & 2 deletions cds/modules/flows/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,16 +85,18 @@ def dispose_object_version(object_version):


@contextmanager
def move_file_into_local(obj, delete=True):
def move_file_into_local(obj, delete=True, tmp_dir=None):
"""Move file from XRootD accessed file system into a local path

:param obj: Object version to make locally available.
:param delete: Whether or not the tmp file should be deleted on exit.
"""
if not tmp_dir:
tmp_dir = current_app.config["CDS_FILES_TMP_FOLDER"]
if os.path.exists(obj.file.uri):
yield obj.file.uri
else:
tmp_path = os.path.join(current_app.config["CDS_FILES_TMP_FOLDER"], str(obj.file_id))
tmp_path = os.path.join(tmp_dir, str(obj.file_id))
if not os.path.exists(tmp_path):
os.makedirs(tmp_path)

Expand Down