| import os
|
| from werkzeug.utils import secure_filename
|
| from functools import wraps
|
| from flask import current_app, request, redirect, url_for, session, send_file
|
| import markdown
|
| from markupsafe import Markup
|
| from app.extensions import db
|
| from app.models import Image
|
| import io
|
|
|
| def markdown_to_html(text):
|
| return Markup(markdown.markdown(text, extensions=['fenced_code', 'tables']))
|
|
|
| def allowed_file(filename):
|
| """Check if uploaded file has allowed extension"""
|
| return '.' in filename and \
|
| filename.rsplit('.', 1)[1].lower() in current_app.config['ALLOWED_EXTENSIONS']
|
|
|
| def handle_image_upload(file):
|
| """Handle image upload and store in database"""
|
| if file and allowed_file(file.filename):
|
| filename = secure_filename(file.filename)
|
| image = Image(
|
| filename=filename,
|
| data=file.read(),
|
| mime_type=file.content_type
|
| )
|
| db.session.add(image)
|
| db.session.commit()
|
| return f'/api/images/{image.id}'
|
| return None
|
|
|
| def login_required(f):
|
| """Decorator to require login for admin routes"""
|
| @wraps(f)
|
| def decorated_function(*args, **kwargs):
|
| if not session.get('logged_in'):
|
| return redirect(url_for('admin.login'))
|
| return f(*args, **kwargs)
|
| return decorated_function
|
|
|
| def check_auth(username, password):
|
| """Check if username and password match environment variables"""
|
| return (username == current_app.config['ADMIN_USERNAME'] and
|
| password == current_app.config['ADMIN_PASSWORD']) |