File size: 1,590 Bytes
44cae7c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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'])