From 74f027f2cf82197a4f0df735720577c69fe83ba1 Mon Sep 17 00:00:00 2001 From: XnpioChV Date: Fri, 17 Jan 2025 18:51:35 -0500 Subject: [PATCH] feat: Upload transcript file as static asset in Learning Core in library components --- .../core/djangoapps/xblock/rest_api/views.py | 2 +- xmodule/video_block/video_handlers.py | 18 ++++++++++++++++-- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/openedx/core/djangoapps/xblock/rest_api/views.py b/openedx/core/djangoapps/xblock/rest_api/views.py index edcbf22e0d3d..05a16adb1a6f 100644 --- a/openedx/core/djangoapps/xblock/rest_api/views.py +++ b/openedx/core/djangoapps/xblock/rest_api/views.py @@ -175,7 +175,7 @@ def xblock_handler( """ # To support sandboxed XBlocks, custom frontends, and other use cases, we # authenticate requests using a secure token in the URL. see - # openedx.core.djangoapps.xblock.utils.get_secure_hash_for_xblock_handler + # openedx.core.djangoapps.xblock.utils.get_secure_token_for_xblock_handler # for details and rationale. if not validate_secure_token_for_xblock_handler(user_id, str(usage_key), secure_token): raise PermissionDenied("Invalid/expired auth token.") diff --git a/xmodule/video_block/video_handlers.py b/xmodule/video_block/video_handlers.py index b7857e881ece..076b073a8566 100644 --- a/xmodule/video_block/video_handlers.py +++ b/xmodule/video_block/video_handlers.py @@ -13,13 +13,15 @@ from django.core.files.base import ContentFile from django.utils.timezone import now from edxval.api import create_external_video, create_or_update_video_transcript, delete_video_transcript -from opaque_keys.edx.locator import CourseLocator +from opaque_keys.edx.locator import CourseLocator, LibraryLocatorV2 +from opaque_keys.edx.keys import UsageKeyV2 from webob import Response from xblock.core import XBlock from xblock.exceptions import JsonHandlerError from xmodule.exceptions import NotFoundError from xmodule.fields import RelativeTime +from openedx.core.djangoapps.content_libraries import api as lib_api from .transcripts_utils import ( Transcript, @@ -517,8 +519,9 @@ def studio_transcript(self, request, dispatch): try: # Convert SRT transcript into an SJSON format # and upload it to S3. + content = transcript_file.read() sjson_subs = Transcript.convert( - content=transcript_file.read().decode('utf-8'), + content=content.decode('utf-8'), input_format=Transcript.SRT, output_format=Transcript.SJSON ).encode() @@ -541,6 +544,17 @@ def studio_transcript(self, request, dispatch): self.transcripts.pop(language_code, None) self.transcripts[new_language_code] = f'{edx_video_id}-{new_language_code}.srt' response = Response(json.dumps(payload), status=201) + + if isinstance(self.scope_ids.usage_id, UsageKeyV2): + usage_key = self.scope_ids.usage_id + if isinstance(usage_key.context_key, LibraryLocatorV2): + # Save transcript as static asset in Learning Core if is a library component + filename = f"static/{self.transcripts[new_language_code]}" + lib_api.add_library_block_static_asset_file( + usage_key, + filename, + content, + ) except (TranscriptsGenerationException, UnicodeDecodeError): response = Response( json={