Spaces:
Sleeping
Sleeping
File size: 3,747 Bytes
b949a69 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
import os
import tempfile
import shutil
from pathlib import Path
from typing import List
from fastapi import UploadFile
async def save_upload_file(upload_file: UploadFile) -> str:
"""
Save an uploaded file to a temporary location
Args:
upload_file: FastAPI UploadFile object
Returns:
Path to the saved temporary file
"""
try:
# Create temporary file with appropriate extension
suffix = Path(upload_file.filename).suffix if upload_file.filename else ""
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=suffix)
# Write uploaded content to temporary file
content = await upload_file.read()
temp_file.write(content)
temp_file.close()
return temp_file.name
except Exception as e:
print(f"Error saving uploaded file: {e}")
raise
def cleanup_temp_files(file_paths: List[str]) -> None:
"""
Clean up temporary files
Args:
file_paths: List of file paths to delete
"""
for file_path in file_paths:
try:
if os.path.exists(file_path):
os.unlink(file_path)
print(f"Cleaned up temporary file: {file_path}")
except Exception as e:
print(f"Error cleaning up file {file_path}: {e}")
def cleanup_temp_directories(dir_paths: List[str]) -> None:
"""
Clean up temporary directories
Args:
dir_paths: List of directory paths to delete
"""
for dir_path in dir_paths:
try:
if os.path.exists(dir_path):
shutil.rmtree(dir_path)
print(f"Cleaned up temporary directory: {dir_path}")
except Exception as e:
print(f"Error cleaning up directory {dir_path}: {e}")
def get_file_extension(filename: str) -> str:
"""
Get file extension from filename
Args:
filename: Name of the file
Returns:
File extension (including the dot)
"""
return Path(filename).suffix.lower()
def is_valid_image_file(filename: str) -> bool:
"""
Check if filename represents a valid image file
Args:
filename: Name of the file
Returns:
True if valid image file
"""
valid_extensions = {'.jpg', '.jpeg', '.png', '.gif', '.bmp', '.tiff', '.webp'}
return get_file_extension(filename) in valid_extensions
def is_valid_video_file(filename: str) -> bool:
"""
Check if filename represents a valid video file
Args:
filename: Name of the file
Returns:
True if valid video file
"""
valid_extensions = {'.mp4', '.avi', '.mov', '.wmv', '.flv', '.webm', '.mkv', '.m4v'}
return get_file_extension(filename) in valid_extensions
def create_temp_directory() -> str:
"""
Create a temporary directory
Returns:
Path to the created temporary directory
"""
return tempfile.mkdtemp()
def get_file_size(file_path: str) -> int:
"""
Get file size in bytes
Args:
file_path: Path to the file
Returns:
File size in bytes
"""
try:
return os.path.getsize(file_path)
except OSError:
return 0
def format_file_size(size_bytes: int) -> str:
"""
Format file size in human-readable format
Args:
size_bytes: File size in bytes
Returns:
Formatted file size string
"""
if size_bytes == 0:
return "0B"
size_names = ["B", "KB", "MB", "GB", "TB"]
i = 0
while size_bytes >= 1024 and i < len(size_names) - 1:
size_bytes /= 1024.0
i += 1
return f"{size_bytes:.1f}{size_names[i]}"
|