Spaces:
Running
Running
| import shutil | |
| from pathlib import Path | |
| from typing import List, Tuple, Dict | |
| import mimetypes | |
| def get_file_info(file_path: str) -> Dict: | |
| """Get file information""" | |
| path = Path(file_path) | |
| if not path.exists(): | |
| return {'error': 'File not found'} | |
| stat = path.stat() | |
| mime_type, _ = mimetypes.guess_type(str(path)) | |
| return { | |
| 'name': path.name, | |
| 'size': stat.st_size, | |
| 'size_mb': round(stat.st_size / (1024 * 1024), 2), | |
| 'extension': path.suffix, | |
| 'mime_type': mime_type, | |
| 'created': stat.st_ctime, | |
| 'modified': stat.st_mtime | |
| } | |
| def ensure_directory(directory: str): | |
| """Ensure directory exists""" | |
| Path(directory).mkdir(parents=True, exist_ok=True) | |
| def copy_file(src: str, dst: str) -> bool: | |
| """Copy file safely""" | |
| try: | |
| ensure_directory(str(Path(dst).parent)) | |
| shutil.copy2(src, dst) | |
| return True | |
| except Exception as e: | |
| print(f"Copy error: {e}") | |
| return False | |
| def move_file(src: str, dst: str) -> bool: | |
| """Move file safely""" | |
| try: | |
| ensure_directory(str(Path(dst).parent)) | |
| shutil.move(src, dst) | |
| return True | |
| except Exception as e: | |
| print(f"Move error: {e}") | |
| return False | |
| def list_files(directory: str, extension: str = None) -> List[str]: | |
| """List files in directory""" | |
| path = Path(directory) | |
| if not path.exists(): | |
| return [] | |
| if extension: | |
| return [str(f) for f in path.glob(f"*{extension}")] | |
| else: | |
| return [str(f) for f in path.glob("*") if f.is_file()] |