Spaces:
Running
Running
File size: 5,051 Bytes
b9409d5 |
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 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
import shutil
import os
from pathlib import Path
from typing import List
import mimetypes
async def organize_files(directory: str, strategy: str = 'by_type') -> dict:
"""
Organize files in a directory
Args:
directory: Directory to organize
strategy: Organization strategy (by_type, by_date, by_size)
Returns:
Dict with organization results
"""
try:
dir_path = Path(directory)
if not dir_path.exists():
return {'error': 'Directory not found', 'success': False}
files = [f for f in dir_path.iterdir() if f.is_file()]
if strategy == 'by_type':
return await organize_by_type(files, dir_path)
elif strategy == 'by_date':
return await organize_by_date(files, dir_path)
elif strategy == 'by_size':
return await organize_by_size(files, dir_path)
else:
return {'error': 'Unknown strategy', 'success': False}
except Exception as e:
return {'error': str(e), 'success': False}
async def organize_by_type(files: List[Path], base_dir: Path) -> dict:
"""Organize files by file type"""
type_map = {
'documents': ['.pdf', '.doc', '.docx', '.txt', '.odt'],
'images': ['.jpg', '.jpeg', '.png', '.gif', '.bmp', '.svg'],
'spreadsheets': ['.xlsx', '.xls', '.csv'],
'archives': ['.zip', '.rar', '.7z', '.tar', '.gz'],
'other': []
}
moved_files = {}
for file in files:
ext = file.suffix.lower()
# Find category
category = 'other'
for cat, extensions in type_map.items():
if ext in extensions:
category = cat
break
# Create category folder
cat_dir = base_dir / category
cat_dir.mkdir(exist_ok=True)
# Move file
dest = cat_dir / file.name
shutil.move(str(file), str(dest))
if category not in moved_files:
moved_files[category] = []
moved_files[category].append(file.name)
return {
'success': True,
'strategy': 'by_type',
'files_moved': sum(len(v) for v in moved_files.values()),
'categories': moved_files
}
async def organize_by_date(files: List[Path], base_dir: Path) -> dict:
"""Organize files by modification date"""
moved_files = {}
for file in files:
# Get modification time
mtime = datetime.fromtimestamp(file.stat().st_mtime)
date_folder = mtime.strftime('%Y-%m')
# Create date folder
date_dir = base_dir / date_folder
date_dir.mkdir(exist_ok=True)
# Move file
dest = date_dir / file.name
shutil.move(str(file), str(dest))
if date_folder not in moved_files:
moved_files[date_folder] = []
moved_files[date_folder].append(file.name)
return {
'success': True,
'strategy': 'by_date',
'files_moved': sum(len(v) for v in moved_files.values()),
'folders': moved_files
}
async def organize_by_size(files: List[Path], base_dir: Path) -> dict:
"""Organize files by size"""
size_categories = {
'small': (0, 1024 * 1024), # < 1MB
'medium': (1024 * 1024, 10 * 1024 * 1024), # 1-10MB
'large': (10 * 1024 * 1024, float('inf')) # > 10MB
}
moved_files = {}
for file in files:
size = file.stat().st_size
# Determine category
category = 'small'
for cat, (min_size, max_size) in size_categories.items():
if min_size <= size < max_size:
category = cat
break
# Create category folder
cat_dir = base_dir / category
cat_dir.mkdir(exist_ok=True)
# Move file
dest = cat_dir / file.name
shutil.move(str(file), str(dest))
if category not in moved_files:
moved_files[category] = []
moved_files[category].append(file.name)
return {
'success': True,
'strategy': 'by_size',
'files_moved': sum(len(v) for v in moved_files.values()),
'categories': moved_files
}
async def rename_file(file_path: str, new_name: str) -> dict:
"""Rename file with semantic name"""
try:
old_path = Path(file_path)
if not old_path.exists():
return {'error': 'File not found', 'success': False}
# Keep extension
ext = old_path.suffix
if not new_name.endswith(ext):
new_name += ext
new_path = old_path.parent / new_name
old_path.rename(new_path)
return {
'success': True,
'old_name': old_path.name,
'new_name': new_name,
'new_path': str(new_path)
}
except Exception as e:
return {'error': str(e), 'success': False}
|