Spaces:
Running
on
Zero
Running
on
Zero
| # file_utils | |
| import os | |
| import utils.constants as constants | |
| import shutil | |
| from pathlib import Path | |
| def cleanup_temp_files(): | |
| for file_path in constants.temp_files: | |
| try: | |
| os.remove(file_path) | |
| except Exception as e: | |
| print(f"Failed to delete temp file {file_path}: {e}") | |
| def rename_file_to_lowercase_extension(image_path: str) -> str: | |
| """ | |
| Renames only the file extension to lowercase by copying it to the temporary folder. | |
| Parameters: | |
| image_path (str): The original file path. | |
| Returns: | |
| str: The new file path in the temporary folder with the lowercase extension. | |
| Raises: | |
| Exception: If there is an error copying the file. | |
| """ | |
| path = Path(image_path) | |
| new_suffix = path.suffix.lower() | |
| new_path = path.with_suffix(new_suffix) | |
| # Make a copy in the temporary folder with the lowercase extension | |
| if path.suffix != new_suffix: | |
| try: | |
| shutil.copy(path, new_path) | |
| constants.temp_files.append(str(new_path)) | |
| return str(new_path) | |
| except FileExistsError: | |
| raise Exception(f"Cannot copy {path} to {new_path}: target file already exists.") | |
| except Exception as e: | |
| raise Exception(f"Error copying file: {e}") | |
| else: | |
| return str(path) |