Skip to content

Commit

Permalink
feat: [AXM-755] save extrnal files and fonts to offline block content
Browse files Browse the repository at this point in the history
  • Loading branch information
NiedielnitsevIvan committed Jun 22, 2024
1 parent c2a7bee commit e170103
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
15 changes: 15 additions & 0 deletions openedx/features/offline_mode/assets_management.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,3 +161,18 @@ def save_mathjax_to_xblock_assets(temp_dir):
file.write(response.content)

log.info(f"Successfully saved MathJax to {file_path}")


def save_external_file(temp_dir, link, filename):
"""
Save external file to the local directory.
"""
file_path = os.path.join(temp_dir, filename)
try:
response = requests.get(link)
except requests.exceptions.RequestException as e:
log.error(f"Failed to download {link}: {e}")
else:
create_subdirectories_for_asset(file_path)
with open(file_path, 'wb') as file:
file.write(response.content)
36 changes: 35 additions & 1 deletion openedx/features/offline_mode/html_manipulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
"""
import os
import re
import uuid

from bs4 import BeautifulSoup

from django.conf import settings

from .assets_management import save_asset_file, save_mathjax_to_xblock_assets
from .assets_management import save_asset_file, save_external_file, save_mathjax_to_xblock_assets
from .constants import MATHJAX_CDN_URL, MATHJAX_STATIC_PATH


Expand All @@ -33,6 +34,8 @@ def process_html(self):
self._replace_asset_links()
self._replace_static_links()
self._replace_mathjax_link()
self._replace_external_links()
self._copy_platform_fonts()

soup = BeautifulSoup(self.html_data, 'html.parser')
self._replace_iframe(soup)
Expand Down Expand Up @@ -79,6 +82,37 @@ def _replace_link(self, match):
save_asset_file(self.temp_dir, self.xblock, link, filename)
return f'assets/{filename}'

def _replace_external_links(self):
"""
Replace external links to images and js files with local links.
"""
pattern = re.compile(r'https:\/\/[^"\s]+?\.(js|jpe?g|png|gif|bmp|svg)')
self.html_data = pattern.sub(self._replace_external_link, self.html_data)

def _replace_external_link(self, match):
"""
Returns the local path of the external file.
"""
link = match.group()
file_extension = match.group(1)
unique_id = uuid.uuid4()
filename = f"assets/eternal/{unique_id}.{file_extension}"
save_external_file(self.temp_dir, link, filename)

return filename

def _copy_platform_fonts(self):
"""
Copy platform fonts to the block temp directory.
"""
platform_fonts_dir = "xmodule/js/common_static/fonts/vendor/"
block_fonts_path = os.path.join(self.temp_dir, "assets", "fonts", "vendor")
os.makedirs(block_fonts_path)
for font in os.listdir(platform_fonts_dir):
font_path = os.path.join(platform_fonts_dir, font)
if os.path.isfile(font_path):
os.system(f'cp {font_path} {block_fonts_path}')

@staticmethod
def _replace_iframe(soup):
"""
Expand Down

0 comments on commit e170103

Please sign in to comment.