diff --git a/cds/modules/deposit/ext.py b/cds/modules/deposit/ext.py index a9b62addb..bf9724e7c 100644 --- a/cds/modules/deposit/ext.py +++ b/cds/modules/deposit/ext.py @@ -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 ( @@ -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: diff --git a/cds/modules/flows/files.py b/cds/modules/flows/files.py index cc5487f14..b3d7f1322 100644 --- a/cds/modules/flows/files.py +++ b/cds/modules/flows/files.py @@ -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)