Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: render library v2 assets with whitespace #35974

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Loading