Skip to content

Commit

Permalink
feat: [AXM-343] Transfer offline download solution for HTML xblock
Browse files Browse the repository at this point in the history
  • Loading branch information
NiedielnitsevIvan authored and GlugovGrGlib committed Jun 11, 2024
1 parent a50ce23 commit 0d3c27b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 9 deletions.
16 changes: 7 additions & 9 deletions openedx/features/offline_mode/assets_management.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,22 +48,20 @@ def save_asset_file(temp_dir, xblock, path, filename):
path (str): The path where the asset is located.
filename (str): The name of the file to be saved.
"""
if filename.endswith('djangojs.js'):
return

try:
if '/' in filename:
if filename.startswith('assets/'):
asset_filename = filename.split('/')[-1]
asset_key = StaticContent.get_asset_key_from_path(xblock.location.course_key, asset_filename)
content = AssetManager.find(asset_key).data
file_path = os.path.join(temp_dir, filename)
else:
static_path = get_static_file_path(filename)
content = read_static_file(static_path)
else:
asset_key = StaticContent.get_asset_key_from_path(xblock.location.course_key, path)
content = AssetManager.find(asset_key).data
file_path = os.path.join(temp_dir, 'assets', filename)
except (ItemNotFoundError, NotFoundError):
log.info(f"Asset not found: {filename}")

else:
assets_path = os.path.join(temp_dir, 'assets')
file_path = os.path.join(assets_path, filename)
create_subdirectories_for_asset(file_path)
with open(file_path, 'wb') as file:
file.write(content)
Expand Down
17 changes: 17 additions & 0 deletions openedx/features/offline_mode/html_manipulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ def process_html(self):
Changes links to static files to paths to pre-generated static files for offline use.
"""
self._replace_asset_links()
self._replace_static_links()
self._replace_mathjax_link()

Expand All @@ -53,6 +54,22 @@ def _replace_static_links(self):
pattern = re.compile(fr'{static_links_pattern}')
self.html_data = pattern.sub(self._replace_link, self.html_data)

def _replace_asset_links(self):
"""
Replace static links with local links.
"""
pattern = re.compile(r'/assets/[\w./@:+-]+')
self.html_data = pattern.sub(self._replace_asset_link, self.html_data)

def _replace_asset_link(self, match):
"""
Returns the local path of the asset file.
"""
link = match.group()
filename = link[1:] if link.startswith('/') else link # Remove the leading '/'
save_asset_file(self.temp_dir, self.xblock, link, filename)
return filename

def _replace_link(self, match):
"""
Returns the local path of the asset file.
Expand Down

0 comments on commit 0d3c27b

Please sign in to comment.