Spaces:
Sleeping
Sleeping
Update api.py
Browse files
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 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 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 |
|