89 lines
No EOL
3.2 KiB
Python
89 lines
No EOL
3.2 KiB
Python
from flask import Blueprint, render_template
|
|
from flask_login import login_required, current_user
|
|
from ..models import File, Folder
|
|
from ..utils.file_helpers import format_file_size
|
|
|
|
# Create blueprint
|
|
bp = Blueprint('dashboard', __name__)
|
|
|
|
@bp.route('/')
|
|
@bp.route('/index')
|
|
@login_required
|
|
def index():
|
|
"""Dashboard index page"""
|
|
# Count user's files and folders
|
|
file_count = File.query.filter_by(user_id=current_user.id).count()
|
|
folder_count = Folder.query.filter_by(user_id=current_user.id).count()
|
|
|
|
# Get storage usage
|
|
storage_used = sum(file.size for file in File.query.filter_by(user_id=current_user.id).all())
|
|
|
|
# Get recent files
|
|
recent_files = File.query.filter_by(user_id=current_user.id).order_by(File.created_at.desc()).limit(5).all()
|
|
|
|
# Add icon_class to each file
|
|
for file in recent_files:
|
|
file.icon_class = file.get_icon_class()
|
|
|
|
return render_template('dashboard/index.html',
|
|
file_count=file_count,
|
|
folder_count=folder_count,
|
|
storage_used=format_file_size(storage_used),
|
|
recent_files=recent_files)
|
|
|
|
def get_file_icon(mime_type, filename):
|
|
"""Return Font Awesome icon class based on file type"""
|
|
if mime_type:
|
|
if mime_type.startswith('image/'):
|
|
return 'fa-file-image'
|
|
elif mime_type.startswith('video/'):
|
|
return 'fa-file-video'
|
|
elif mime_type.startswith('audio/'):
|
|
return 'fa-file-audio'
|
|
elif mime_type.startswith('text/'):
|
|
return 'fa-file-alt'
|
|
elif mime_type.startswith('application/pdf'):
|
|
return 'fa-file-pdf'
|
|
elif 'spreadsheet' in mime_type or 'excel' in mime_type:
|
|
return 'fa-file-excel'
|
|
elif 'presentation' in mime_type or 'powerpoint' in mime_type:
|
|
return 'fa-file-powerpoint'
|
|
elif 'document' in mime_type or 'word' in mime_type:
|
|
return 'fa-file-word'
|
|
|
|
# Check by extension
|
|
ext = os.path.splitext(filename)[1].lower()[1:]
|
|
if ext in ['jpg', 'jpeg', 'png', 'gif', 'bmp', 'svg', 'webp']:
|
|
return 'fa-file-image'
|
|
elif ext in ['mp4', 'avi', 'mov', 'wmv', 'flv', 'mkv']:
|
|
return 'fa-file-video'
|
|
elif ext in ['mp3', 'wav', 'ogg', 'flac', 'm4a']:
|
|
return 'fa-file-audio'
|
|
elif ext in ['doc', 'docx', 'odt']:
|
|
return 'fa-file-word'
|
|
elif ext in ['xls', 'xlsx', 'ods', 'csv']:
|
|
return 'fa-file-excel'
|
|
elif ext in ['ppt', 'pptx', 'odp']:
|
|
return 'fa-file-powerpoint'
|
|
elif ext == 'pdf':
|
|
return 'fa-file-pdf'
|
|
elif ext in ['zip', 'rar', '7z', 'tar', 'gz']:
|
|
return 'fa-file-archive'
|
|
elif ext in ['txt', 'rtf', 'md']:
|
|
return 'fa-file-alt'
|
|
elif ext in ['html', 'css', 'js', 'py', 'java', 'php', 'c', 'cpp', 'json', 'xml']:
|
|
return 'fa-file-code'
|
|
|
|
return 'fa-file'
|
|
|
|
def format_file_size(size):
|
|
"""Format file size in bytes to human-readable format"""
|
|
if not size:
|
|
return "0 B"
|
|
|
|
size_names = ("B", "KB", "MB", "GB", "TB")
|
|
i = 0
|
|
while size >= 1024 and i < len(size_names) - 1:
|
|
size /= 1024
|
|
i += 1
|
|
return f"{size:.1f} {size_names[i]}" |