Skip to content

Commit 7f186c1

Browse files
zubeydecivelekzzacharo
authored andcommitted
subtitles: copy srt file to local to convert vtt
1 parent 50b83aa commit 7f186c1

2 files changed

Lines changed: 20 additions & 16 deletions

File tree

cds/modules/deposit/ext.py

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
from invenio_records_files.utils import sorted_files_from_bucket
4040
from srt_to_vtt import srt_to_vtt
4141

42+
from ..flows.files import move_file_into_local
4243
from ..invenio_deposit.signals import post_action
4344
from .indexer import cdsdeposit_indexer_receiver
4445
from .receivers import (
@@ -68,25 +69,26 @@ def _create_vtt_from_srt(srt_obj):
6869
if not srt_obj.file or not srt_obj.file.uri:
6970
return None
7071

71-
srt_path = srt_obj.file.uri
7272
tmp_dir = None
7373
try:
74-
# Create temporary directory for VTT file
74+
# Create temporary directory for SRT and VTT file
7575
tmp_dir = tempfile.mkdtemp()
7676
vtt_path = os.path.join(tmp_dir, vtt_key)
7777

78-
# Convert using srt-to-vtt library
79-
srt_to_vtt(srt_path, vtt_path)
80-
81-
# Create VTT ObjectVersion
82-
vtt_obj = ObjectVersion.create(
83-
bucket=srt_obj.bucket,
84-
key=vtt_key,
85-
stream=open(vtt_path, "rb"),
86-
size=os.path.getsize(vtt_path),
87-
)
88-
_create_tags(vtt_obj)
89-
return vtt_obj
78+
with move_file_into_local(srt_obj, tmp_dir=tmp_dir) as local_srt_path:
79+
# Convert using srt-to-vtt library
80+
srt_to_vtt(local_srt_path, vtt_path)
81+
82+
# Create VTT ObjectVersion
83+
vtt_obj = ObjectVersion.create(
84+
bucket=srt_obj.bucket,
85+
key=vtt_key,
86+
stream=open(vtt_path, "rb"),
87+
size=os.path.getsize(vtt_path),
88+
)
89+
_create_tags(vtt_obj)
90+
91+
return vtt_obj
9092
except (OSError, IOError, AttributeError, Exception):
9193
return None
9294
finally:

cds/modules/flows/files.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,16 +85,18 @@ def dispose_object_version(object_version):
8585

8686

8787
@contextmanager
88-
def move_file_into_local(obj, delete=True):
88+
def move_file_into_local(obj, delete=True, tmp_dir=None):
8989
"""Move file from XRootD accessed file system into a local path
9090
9191
:param obj: Object version to make locally available.
9292
:param delete: Whether or not the tmp file should be deleted on exit.
9393
"""
94+
if not tmp_dir:
95+
tmp_dir = current_app.config["CDS_FILES_TMP_FOLDER"]
9496
if os.path.exists(obj.file.uri):
9597
yield obj.file.uri
9698
else:
97-
tmp_path = os.path.join(current_app.config["CDS_FILES_TMP_FOLDER"], str(obj.file_id))
99+
tmp_path = os.path.join(tmp_dir, str(obj.file_id))
98100
if not os.path.exists(tmp_path):
99101
os.makedirs(tmp_path)
100102

0 commit comments

Comments
 (0)