Download utils/file_handler.py from yekkala/SkillSync: direct link, hf CLI and curl.
- Browser
- Download file 2.19 kB
-
https://huggingface.co/spaces/yekkala/SkillSync/resolve/main/utils/file_handler.py
- Command line
-
hf download hf://spaces/yekkala/SkillSync/utils/file_handler.py
-
curl -L -o file_handler.py https://huggingface.co/spaces/yekkala/SkillSync/resolve/main/utils/file_handler.py
2.19 kB
| """ | |
| File handling utilities for SkillSync | |
| """ | |
| import os | |
| import shutil | |
| from datetime import datetime | |
| class FileHandler: | |
| """Handles file operations for submissions""" | |
| def __init__(self, upload_dir="data/user_data/submissions", max_size_mb=100): | |
| self.upload_dir = upload_dir | |
| self.max_size = max_size_mb * 1024 * 1024 | |
| self.allowed_extensions = [".zip", ".pdf", ".ipynb", ".py", ".txt", ".md"] | |
| def ensure_upload_dir(self): | |
| """Ensure upload directory exists""" | |
| os.makedirs(self.upload_dir, exist_ok=True) | |
| def validate_file(self, file): | |
| """Validate uploaded file""" | |
| if file is None: | |
| return False, "No file provided" | |
| file_name = getattr(file, 'name', 'unknown') | |
| file_ext = os.path.splitext(file_name)[1].lower() | |
| if file_ext not in self.allowed_extensions: | |
| return False, f"File type {file_ext} not allowed" | |
| return True, "File validated successfully" | |
| def save_file(self, file, user_id, assignment_type): | |
| """Save uploaded file""" | |
| try: | |
| self.ensure_upload_dir() | |
| original_name = getattr(file, 'name', 'uploaded_file') | |
| original_name = os.path.basename(original_name) | |
| timestamp = datetime.now().strftime("%Y%m%d_%H%M%S") | |
| new_name = f"{user_id}_{assignment_type}_{timestamp}_{original_name}" | |
| destination = os.path.join(self.upload_dir, new_name) | |
| if hasattr(file, 'name'): | |
| shutil.copy2(file.name, destination) | |
| return True, destination | |
| except Exception as e: | |
| return False, str(e) | |
| def get_file_info(self, filepath): | |
| """Get information about a file""" | |
| if not os.path.exists(filepath): | |
| return None | |
| stat = os.stat(filepath) | |
| return { | |
| "path": filepath, | |
| "size": stat.st_size, | |
| "created": datetime.fromtimestamp(stat.st_ctime).strftime("%Y-%m-%d %H:%M:%S"), | |
| "modified": datetime.fromtimestamp(stat.st_mtime).strftime("%Y-%m-%d %H:%M:%S") | |
| } | |