From 9b14946259ba2cc88d64d94595516bd6808f63fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=86=D0=B2=D0=B0=D0=BD=20=D0=9D=D1=94=D0=B4=D1=94=D0=BB?= =?UTF-8?q?=D1=8C=D0=BD=D1=96=D1=86=D0=B5=D0=B2?= Date: Mon, 10 Jun 2024 20:11:42 +0300 Subject: [PATCH] feat: [AXM-343] Transfer offline download solution for HTML xblock --- .../features/offline_mode/assets_management.py | 16 +++++++--------- .../features/offline_mode/html_manipulator.py | 17 +++++++++++++++++ 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/openedx/features/offline_mode/assets_management.py b/openedx/features/offline_mode/assets_management.py index fea45a8e8480..e8b73e9213e6 100644 --- a/openedx/features/offline_mode/assets_management.py +++ b/openedx/features/offline_mode/assets_management.py @@ -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) diff --git a/openedx/features/offline_mode/html_manipulator.py b/openedx/features/offline_mode/html_manipulator.py index a0c519096e1a..fbbca2af6494 100644 --- a/openedx/features/offline_mode/html_manipulator.py +++ b/openedx/features/offline_mode/html_manipulator.py @@ -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() @@ -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.