Skip to content

Commit

Permalink
fix: render library v2 assets with whitespace (#35974)
Browse files Browse the repository at this point in the history
Assets that contain whitespace fail to be rendered when uploaded to library v2
  • Loading branch information
DanielVZ96 authored Dec 24, 2024
1 parent 81f825f commit 85f4128
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,13 @@ def test_asset_filenames(self):
file_name = "a////////b"
self._set_library_block_asset(block_id, file_name, SVG_DATA, expect_response=400)

# Names with spaces are allowed but replaced with underscores
file_name_with_space = "o w o.svg"
self._set_library_block_asset(block_id, file_name_with_space, SVG_DATA)
file_name = "o_w_o.svg"
assert self._get_library_block_asset(block_id, file_name)['path'] == file_name
assert self._get_library_block_asset(block_id, file_name)['size'] == file_size

def test_video_transcripts(self):
"""
Test that video blocks can read transcript files out of learning core.
Expand Down
1 change: 1 addition & 0 deletions openedx/core/djangoapps/content_libraries/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -799,6 +799,7 @@ def put(self, request, usage_key_str, file_path):
"""
Replace a static asset file belonging to this block.
"""
file_path = file_path.replace(" ", "_") # Messes up url/name correspondence due to URL encoding.
usage_key = LibraryUsageLocatorV2.from_string(usage_key_str)
api.require_permission_for_library_key(
usage_key.lib_key, request.user, permissions.CAN_EDIT_THIS_CONTENT_LIBRARY,
Expand Down
20 changes: 16 additions & 4 deletions openedx/core/djangoapps/xblock/runtime/learning_core_runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import logging
from collections import defaultdict
from datetime import datetime, timezone
from urllib.parse import unquote

from django.core.exceptions import ObjectDoesNotExist, ValidationError
from django.db.transaction import atomic
Expand Down Expand Up @@ -446,9 +447,20 @@ def _lookup_asset_url(self, block: XBlock, asset_path: str) -> str | None:
.get(key=f"static/{asset_path}")
)
except ObjectDoesNotExist:
# This means we see a path that _looks_ like it should be a static
# asset for this Component, but that static asset doesn't really
# exist.
return None
try:
# Retry with unquoted path. We don't always unquote because it would not
# be backwards-compatible, but we need to try both.
asset_path = unquote(asset_path)
content = (
component_version
.componentversioncontent_set
.filter(content__has_file=True)
.get(key=f"static/{asset_path}")
)
except ObjectDoesNotExist:
# This means we see a path that _looks_ like it should be a static
# asset for this Component, but that static asset doesn't really
# exist.
return None

return self._absolute_url_for_asset(component_version, asset_path)

0 comments on commit 85f4128

Please sign in to comment.