Skip to content

Commit

Permalink
feat: Upload transcript file as static asset in Learning Core in libr…
Browse files Browse the repository at this point in the history
…ary components
  • Loading branch information
ChrisChV committed Jan 17, 2025
1 parent 1c2a575 commit 74f027f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
2 changes: 1 addition & 1 deletion openedx/core/djangoapps/xblock/rest_api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
Expand Down
18 changes: 16 additions & 2 deletions xmodule/video_block/video_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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()
Expand All @@ -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={
Expand Down

0 comments on commit 74f027f

Please sign in to comment.