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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import shutil
|
| 2 |
+
import os
|
| 3 |
+
from pathlib import Path
|
| 4 |
+
from typing import List
|
| 5 |
+
import mimetypes
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
async def organize_files(directory: str, strategy: str = 'by_type') -> dict:
|
| 9 |
+
"""
|
| 10 |
+
Organize files in a directory
|
| 11 |
+
|
| 12 |
+
Args:
|
| 13 |
+
directory: Directory to organize
|
| 14 |
+
strategy: Organization strategy (by_type, by_date, by_size)
|
| 15 |
+
|
| 16 |
+
Returns:
|
| 17 |
+
Dict with organization results
|
| 18 |
+
"""
|
| 19 |
+
try:
|
| 20 |
+
dir_path = Path(directory)
|
| 21 |
+
if not dir_path.exists():
|
| 22 |
+
return {'error': 'Directory not found', 'success': False}
|
| 23 |
+
|
| 24 |
+
files = [f for f in dir_path.iterdir() if f.is_file()]
|
| 25 |
+
|
| 26 |
+
if strategy == 'by_type':
|
| 27 |
+
return await organize_by_type(files, dir_path)
|
| 28 |
+
elif strategy == 'by_date':
|
| 29 |
+
return await organize_by_date(files, dir_path)
|
| 30 |
+
elif strategy == 'by_size':
|
| 31 |
+
return await organize_by_size(files, dir_path)
|
| 32 |
+
else:
|
| 33 |
+
return {'error': 'Unknown strategy', 'success': False}
|
| 34 |
+
|
| 35 |
+
except Exception as e:
|
| 36 |
+
return {'error': str(e), 'success': False}
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
async def organize_by_type(files: List[Path], base_dir: Path) -> dict:
|
| 40 |
+
"""Organize files by file type"""
|
| 41 |
+
type_map = {
|
| 42 |
+
'documents': ['.pdf', '.doc', '.docx', '.txt', '.odt'],
|
| 43 |
+
'images': ['.jpg', '.jpeg', '.png', '.gif', '.bmp', '.svg'],
|
| 44 |
+
'spreadsheets': ['.xlsx', '.xls', '.csv'],
|
| 45 |
+
'archives': ['.zip', '.rar', '.7z', '.tar', '.gz'],
|
| 46 |
+
'other': []
|
| 47 |
+
}
|
| 48 |
+
|
| 49 |
+
moved_files = {}
|
| 50 |
+
|
| 51 |
+
for file in files:
|
| 52 |
+
ext = file.suffix.lower()
|
| 53 |
+
|
| 54 |
+
# Find category
|
| 55 |
+
category = 'other'
|
| 56 |
+
for cat, extensions in type_map.items():
|
| 57 |
+
if ext in extensions:
|
| 58 |
+
category = cat
|
| 59 |
+
break
|
| 60 |
+
|
| 61 |
+
# Create category folder
|
| 62 |
+
cat_dir = base_dir / category
|
| 63 |
+
cat_dir.mkdir(exist_ok=True)
|
| 64 |
+
|
| 65 |
+
# Move file
|
| 66 |
+
dest = cat_dir / file.name
|
| 67 |
+
shutil.move(str(file), str(dest))
|
| 68 |
+
|
| 69 |
+
if category not in moved_files:
|
| 70 |
+
moved_files[category] = []
|
| 71 |
+
moved_files[category].append(file.name)
|
| 72 |
+
|
| 73 |
+
return {
|
| 74 |
+
'success': True,
|
| 75 |
+
'strategy': 'by_type',
|
| 76 |
+
'files_moved': sum(len(v) for v in moved_files.values()),
|
| 77 |
+
'categories': moved_files
|
| 78 |
+
}
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
async def organize_by_date(files: List[Path], base_dir: Path) -> dict:
|
| 82 |
+
"""Organize files by modification date"""
|
| 83 |
+
moved_files = {}
|
| 84 |
+
|
| 85 |
+
for file in files:
|
| 86 |
+
# Get modification time
|
| 87 |
+
mtime = datetime.fromtimestamp(file.stat().st_mtime)
|
| 88 |
+
date_folder = mtime.strftime('%Y-%m')
|
| 89 |
+
|
| 90 |
+
# Create date folder
|
| 91 |
+
date_dir = base_dir / date_folder
|
| 92 |
+
date_dir.mkdir(exist_ok=True)
|
| 93 |
+
|
| 94 |
+
# Move file
|
| 95 |
+
dest = date_dir / file.name
|
| 96 |
+
shutil.move(str(file), str(dest))
|
| 97 |
+
|
| 98 |
+
if date_folder not in moved_files:
|
| 99 |
+
moved_files[date_folder] = []
|
| 100 |
+
moved_files[date_folder].append(file.name)
|
| 101 |
+
|
| 102 |
+
return {
|
| 103 |
+
'success': True,
|
| 104 |
+
'strategy': 'by_date',
|
| 105 |
+
'files_moved': sum(len(v) for v in moved_files.values()),
|
| 106 |
+
'folders': moved_files
|
| 107 |
+
}
|
| 108 |
+
|
| 109 |
+
|
| 110 |
+
async def organize_by_size(files: List[Path], base_dir: Path) -> dict:
|
| 111 |
+
"""Organize files by size"""
|
| 112 |
+
size_categories = {
|
| 113 |
+
'small': (0, 1024 * 1024), # < 1MB
|
| 114 |
+
'medium': (1024 * 1024, 10 * 1024 * 1024), # 1-10MB
|
| 115 |
+
'large': (10 * 1024 * 1024, float('inf')) # > 10MB
|
| 116 |
+
}
|
| 117 |
+
|
| 118 |
+
moved_files = {}
|
| 119 |
+
|
| 120 |
+
for file in files:
|
| 121 |
+
size = file.stat().st_size
|
| 122 |
+
|
| 123 |
+
# Determine category
|
| 124 |
+
category = 'small'
|
| 125 |
+
for cat, (min_size, max_size) in size_categories.items():
|
| 126 |
+
if min_size <= size < max_size:
|
| 127 |
+
category = cat
|
| 128 |
+
break
|
| 129 |
+
|
| 130 |
+
# Create category folder
|
| 131 |
+
cat_dir = base_dir / category
|
| 132 |
+
cat_dir.mkdir(exist_ok=True)
|
| 133 |
+
|
| 134 |
+
# Move file
|
| 135 |
+
dest = cat_dir / file.name
|
| 136 |
+
shutil.move(str(file), str(dest))
|
| 137 |
+
|
| 138 |
+
if category not in moved_files:
|
| 139 |
+
moved_files[category] = []
|
| 140 |
+
moved_files[category].append(file.name)
|
| 141 |
+
|
| 142 |
+
return {
|
| 143 |
+
'success': True,
|
| 144 |
+
'strategy': 'by_size',
|
| 145 |
+
'files_moved': sum(len(v) for v in moved_files.values()),
|
| 146 |
+
'categories': moved_files
|
| 147 |
+
}
|
| 148 |
+
|
| 149 |
+
|
| 150 |
+
async def rename_file(file_path: str, new_name: str) -> dict:
|
| 151 |
+
"""Rename file with semantic name"""
|
| 152 |
+
try:
|
| 153 |
+
old_path = Path(file_path)
|
| 154 |
+
if not old_path.exists():
|
| 155 |
+
return {'error': 'File not found', 'success': False}
|
| 156 |
+
|
| 157 |
+
# Keep extension
|
| 158 |
+
ext = old_path.suffix
|
| 159 |
+
if not new_name.endswith(ext):
|
| 160 |
+
new_name += ext
|
| 161 |
+
|
| 162 |
+
new_path = old_path.parent / new_name
|
| 163 |
+
old_path.rename(new_path)
|
| 164 |
+
|
| 165 |
+
return {
|
| 166 |
+
'success': True,
|
| 167 |
+
'old_name': old_path.name,
|
| 168 |
+
'new_name': new_name,
|
| 169 |
+
'new_path': str(new_path)
|
| 170 |
+
}
|
| 171 |
+
|
| 172 |
+
except Exception as e:
|
| 173 |
+
return {'error': str(e), 'success': False}
|