Spaces:
				
			
			
	
			
			
		Running
		
			on 
			
			Zero
	
	
	
			
			
	
	
	
	
		
		
		Running
		
			on 
			
			Zero
	fix some broken file types
Browse files- utils/file_utils.py +33 -1
- utils/image_utils.py +4 -5
    	
        utils/file_utils.py
    CHANGED
    
    | @@ -1,10 +1,42 @@ | |
| 1 | 
             
            # file_utils
         | 
| 2 | 
             
            import os
         | 
| 3 | 
             
            import utils.constants as constants
         | 
|  | |
|  | |
| 4 |  | 
| 5 | 
             
            def cleanup_temp_files():
         | 
| 6 | 
             
                for file_path in constants.temp_files:
         | 
| 7 | 
             
                    try:
         | 
| 8 | 
             
                        os.remove(file_path)
         | 
| 9 | 
             
                    except Exception as e:
         | 
| 10 | 
            -
                        print(f"Failed to delete temp file {file_path}: {e}")
         | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | 
|  | |
| 1 | 
             
            # file_utils
         | 
| 2 | 
             
            import os
         | 
| 3 | 
             
            import utils.constants as constants
         | 
| 4 | 
            +
            import shutil
         | 
| 5 | 
            +
            from pathlib import Path
         | 
| 6 |  | 
| 7 | 
             
            def cleanup_temp_files():
         | 
| 8 | 
             
                for file_path in constants.temp_files:
         | 
| 9 | 
             
                    try:
         | 
| 10 | 
             
                        os.remove(file_path)
         | 
| 11 | 
             
                    except Exception as e:
         | 
| 12 | 
            +
                        print(f"Failed to delete temp file {file_path}: {e}")
         | 
| 13 | 
            +
             | 
| 14 | 
            +
            def rename_file_to_lowercase_extension(image_path: str) -> str:
         | 
| 15 | 
            +
                """
         | 
| 16 | 
            +
                Renames only the file extension to lowercase by copying it to the temporary folder.
         | 
| 17 | 
            +
             | 
| 18 | 
            +
                Parameters:
         | 
| 19 | 
            +
                    image_path (str): The original file path.
         | 
| 20 | 
            +
             | 
| 21 | 
            +
                Returns:
         | 
| 22 | 
            +
                    str: The new file path in the temporary folder with the lowercase extension.
         | 
| 23 | 
            +
             | 
| 24 | 
            +
                Raises:
         | 
| 25 | 
            +
                    Exception: If there is an error copying the file.
         | 
| 26 | 
            +
                """
         | 
| 27 | 
            +
                path = Path(image_path)
         | 
| 28 | 
            +
                new_suffix = path.suffix.lower()
         | 
| 29 | 
            +
                new_path = path.with_suffix(new_suffix)
         | 
| 30 | 
            +
             | 
| 31 | 
            +
                # Make a copy in the temporary folder with the lowercase extension
         | 
| 32 | 
            +
                if path.suffix != new_suffix:
         | 
| 33 | 
            +
                    try:
         | 
| 34 | 
            +
                        shutil.copy(path, new_path)
         | 
| 35 | 
            +
                        constants.temp_files.append(str(new_path))
         | 
| 36 | 
            +
                        return str(new_path)
         | 
| 37 | 
            +
                    except FileExistsError:
         | 
| 38 | 
            +
                        raise Exception(f"Cannot copy {path} to {new_path}: target file already exists.")
         | 
| 39 | 
            +
                    except Exception as e:
         | 
| 40 | 
            +
                        raise Exception(f"Error copying file: {e}")
         | 
| 41 | 
            +
                else:
         | 
| 42 | 
            +
                    return str(path)
         | 
    	
        utils/image_utils.py
    CHANGED
    
    | @@ -15,6 +15,8 @@ from utils.color_utils import ( | |
| 15 | 
             
                detect_color_format,
         | 
| 16 | 
             
                update_color_opacity
         | 
| 17 | 
             
            )
         | 
|  | |
|  | |
| 18 | 
             
            def get_image_from_dict(image_path):
         | 
| 19 | 
             
                if isinstance(image_path, dict) :
         | 
| 20 | 
             
                    if 'image' in image_path:
         | 
| @@ -44,11 +46,8 @@ def open_image(image_path): | |
| 44 | 
             
                """
         | 
| 45 | 
             
                if isinstance(image_path, Image.Image):
         | 
| 46 | 
             
                    return image_path
         | 
| 47 | 
            -
                else: | 
| 48 | 
            -
                     | 
| 49 | 
            -
                    new_suffix = path.suffix.lower()
         | 
| 50 | 
            -
                    new_path = path.with_suffix(new_suffix)
         | 
| 51 | 
            -
                    image_path = str(new_path)
         | 
| 52 |  | 
| 53 | 
             
                import requests
         | 
| 54 | 
             
                try:
         | 
|  | |
| 15 | 
             
                detect_color_format,
         | 
| 16 | 
             
                update_color_opacity
         | 
| 17 | 
             
            )
         | 
| 18 | 
            +
            from utils.file_utils import rename_file_to_lowercase_extension
         | 
| 19 | 
            +
             | 
| 20 | 
             
            def get_image_from_dict(image_path):
         | 
| 21 | 
             
                if isinstance(image_path, dict) :
         | 
| 22 | 
             
                    if 'image' in image_path:
         | 
|  | |
| 46 | 
             
                """
         | 
| 47 | 
             
                if isinstance(image_path, Image.Image):
         | 
| 48 | 
             
                    return image_path
         | 
| 49 | 
            +
                else:        
         | 
| 50 | 
            +
                    image_path = rename_file_to_lowercase_extension(image_path)
         | 
|  | |
|  | |
|  | |
| 51 |  | 
| 52 | 
             
                import requests
         | 
| 53 | 
             
                try:
         | 
