|
11 | 11 | from mealie.services.parser_services.parser_utils.string_utils import extract_quantity_from_string |
12 | 12 |
|
13 | 13 | from ._migration_base import BaseMigrator |
14 | | -from .utils.migration_helpers import format_time |
| 14 | +from .utils.migration_helpers import format_time, safe_local_path |
15 | 15 |
|
16 | 16 |
|
17 | 17 | class DSVParser: |
@@ -157,15 +157,21 @@ def _parse_media(self, _cookbook_id: str, _chapter_id: str, _recipe_id: str, db: |
157 | 157 | if _media_type != "": |
158 | 158 | # Determine file extension based on media type |
159 | 159 | _extension = _media_type.split("/")[-1] |
160 | | - _old_image_path = os.path.join(db.directory, str(_media_id)) |
161 | | - new_image_path = f"{_old_image_path}.{_extension}" |
| 160 | + _old_image_path = Path(db.directory) / str(_media_id) |
| 161 | + new_image_path = _old_image_path.with_suffix(f".{_extension}") |
| 162 | + if safe_local_path(_old_image_path, db.directory) is None: |
| 163 | + return None |
| 164 | + if safe_local_path(new_image_path, db.directory) is None: |
| 165 | + return None |
162 | 166 | # Rename the file if it exists and has no extension |
163 | | - if os.path.exists(_old_image_path) and not os.path.exists(new_image_path): |
| 167 | + if _old_image_path.exists() and not new_image_path.exists(): |
164 | 168 | os.rename(_old_image_path, new_image_path) |
165 | | - if Path(new_image_path).exists(): |
166 | | - return new_image_path |
| 169 | + if new_image_path.exists(): |
| 170 | + return str(new_image_path) |
167 | 171 | else: |
168 | | - return os.path.join(db.directory, str(_media_id)) |
| 172 | + candidate = Path(db.directory) / str(_media_id) |
| 173 | + if safe_local_path(candidate, db.directory) is not None: |
| 174 | + return str(candidate) |
169 | 175 | return None |
170 | 176 |
|
171 | 177 | def _parse_ingredients(self, _recipe_id: str, db: DSVParser) -> list[RecipeIngredient]: |
@@ -388,14 +394,14 @@ def _process_cookbook(self, path: Path) -> None: |
388 | 394 | recipe = recipe_lookup.get(slug) |
389 | 395 | if recipe: |
390 | 396 | if recipe.image: |
391 | | - self.import_image(slug, recipe.image, recipe_id) |
| 397 | + self.import_image(slug, recipe.image, recipe_id, extraction_root=db.directory) |
392 | 398 | else: |
393 | 399 | index_len = len(slug.split("-")[-1]) |
394 | 400 | recipe = recipe_lookup.get(slug[: -(index_len + 1)]) |
395 | 401 | if recipe: |
396 | 402 | self.logger.warning("Duplicate recipe (%s) found! Saved as copy...", recipe.name) |
397 | 403 | if recipe.image: |
398 | | - self.import_image(slug, recipe.image, recipe_id) |
| 404 | + self.import_image(slug, recipe.image, recipe_id, extraction_root=db.directory) |
399 | 405 | else: |
400 | 406 | self.logger.warning("Failed to lookup recipe! (%s)", slug) |
401 | 407 |
|
|
0 commit comments