ABDALLALSWAITI commited on
Commit
0d059f2
·
verified ·
1 Parent(s): e61da5a

Update api.py

Browse files
Files changed (1) hide show
  1. api.py +39 -17
api.py CHANGED
@@ -44,24 +44,46 @@ def save_uploaded_images(images: List[UploadFile], temp_dir: str):
44
  return image_mapping
45
 
46
  def process_html_with_images(html_content: str, temp_dir: str, image_mapping: dict):
47
- """Process HTML to handle image references"""
48
- # Replace image references in HTML
49
- for original_name, new_path in image_mapping.items():
50
- # Handle various image reference patterns
51
- patterns = [
52
- f'src="{original_name}"',
53
- f"src='{original_name}'",
54
- f'src={original_name}',
55
- f'href="{original_name}"',
56
- f"href='{original_name}'"
57
- ]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58
 
59
- for pattern in patterns:
60
- if pattern in html_content:
61
- html_content = html_content.replace(
62
- pattern,
63
- pattern.replace(original_name, new_path)
64
- )
 
65
 
66
  return html_content
67
 
 
44
  return image_mapping
45
 
46
  def process_html_with_images(html_content: str, temp_dir: str, image_mapping: dict):
47
+ """Process HTML to handle image references with absolute file paths"""
48
+ import re
49
+
50
+ for original_name, relative_path in image_mapping.items():
51
+ # Get absolute path for the image
52
+ absolute_path = os.path.abspath(os.path.join(temp_dir, relative_path))
53
+ file_url = f"file://{absolute_path}"
54
+
55
+ # Replace various image reference patterns
56
+ # Pattern 1: src="filename"
57
+ html_content = re.sub(
58
+ f'src=["\'](?:\.\/)?{re.escape(original_name)}["\']',
59
+ f'src="{file_url}"',
60
+ html_content,
61
+ flags=re.IGNORECASE
62
+ )
63
+
64
+ # Pattern 2: src='filename'
65
+ html_content = re.sub(
66
+ f"src=['\"](?:\.\/)?{re.escape(original_name)}['\"]",
67
+ f'src="{file_url}"',
68
+ html_content,
69
+ flags=re.IGNORECASE
70
+ )
71
+
72
+ # Pattern 3: background-image: url(filename)
73
+ html_content = re.sub(
74
+ f'url\(["\']?(?:\.\/)?{re.escape(original_name)}["\']?\)',
75
+ f'url("{file_url}")',
76
+ html_content,
77
+ flags=re.IGNORECASE
78
+ )
79
 
80
+ # Pattern 4: href for links
81
+ html_content = re.sub(
82
+ f'href=["\'](?:\.\/)?{re.escape(original_name)}["\']',
83
+ f'href="{file_url}"',
84
+ html_content,
85
+ flags=re.IGNORECASE
86
+ )
87
 
88
  return html_content
89