Spaces:
Sleeping
Sleeping
| import os | |
| import json | |
| import uuid | |
| from flask import Flask, render_template, request, jsonify, send_file, send_from_directory | |
| from werkzeug.utils import secure_filename | |
| app = Flask(__name__) | |
| app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024 # 16MB max limit | |
| app.config['UPLOAD_FOLDER'] = os.path.join(os.path.dirname(__file__), 'data', 'uploads') | |
| # Ensure data directories exist | |
| DATA_DIR = os.path.join(os.path.dirname(__file__), 'data') | |
| os.makedirs(DATA_DIR, exist_ok=True) | |
| os.makedirs(app.config['UPLOAD_FOLDER'], exist_ok=True) | |
| ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg', 'gif', 'json'} | |
| def allowed_file(filename): | |
| return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS | |
| def not_found(e): | |
| if request.path.startswith('/api/'): | |
| return jsonify({'error': 'Not found'}), 404 | |
| return render_template('index.html'), 404 | |
| def server_error(e): | |
| if request.path.startswith('/api/'): | |
| return jsonify({'error': 'Internal Server Error'}), 500 | |
| return "<h1>500 Internal Server Error</h1><p>Something went wrong.</p>", 500 | |
| def index(): | |
| return render_template('index.html') | |
| def uploaded_file(filename): | |
| return send_from_directory(app.config['UPLOAD_FOLDER'], filename) | |
| def upload_file(): | |
| if 'file' not in request.files: | |
| return jsonify({'error': 'No file part'}), 400 | |
| file = request.files['file'] | |
| if file.filename == '': | |
| return jsonify({'error': 'No selected file'}), 400 | |
| if file and allowed_file(file.filename): | |
| filename = secure_filename(file.filename) | |
| # Unique filename to avoid overwrites | |
| unique_filename = f"{uuid.uuid4()}_{filename}" | |
| file.save(os.path.join(app.config['UPLOAD_FOLDER'], unique_filename)) | |
| return jsonify({'success': True, 'url': f'/uploads/{unique_filename}', 'filename': unique_filename}) | |
| return jsonify({'error': 'File type not allowed'}), 400 | |
| def list_projects(): | |
| projects = [] | |
| for filename in os.listdir(DATA_DIR): | |
| if filename.endswith('.json'): | |
| try: | |
| with open(os.path.join(DATA_DIR, filename), 'r', encoding='utf-8') as f: | |
| data = json.load(f) | |
| projects.append({ | |
| 'id': filename.replace('.json', ''), | |
| 'name': data.get('name', 'Untitled'), | |
| 'updated_at': data.get('updated_at', '') | |
| }) | |
| except: | |
| pass | |
| return jsonify(projects) | |
| def save_project(): | |
| data = request.json | |
| project_id = data.get('id') | |
| if not project_id: | |
| project_id = str(uuid.uuid4()) | |
| data['id'] = project_id | |
| file_path = os.path.join(DATA_DIR, f"{project_id}.json") | |
| with open(file_path, 'w', encoding='utf-8') as f: | |
| json.dump(data, f, ensure_ascii=False, indent=2) | |
| return jsonify({'success': True, 'id': project_id}) | |
| def get_project(project_id): | |
| file_path = os.path.join(DATA_DIR, f"{project_id}.json") | |
| if os.path.exists(file_path): | |
| with open(file_path, 'r', encoding='utf-8') as f: | |
| return jsonify(json.load(f)) | |
| return jsonify({'error': 'Not found'}), 404 | |
| def delete_project(project_id): | |
| file_path = os.path.join(DATA_DIR, f"{project_id}.json") | |
| if os.path.exists(file_path): | |
| os.remove(file_path) | |
| return jsonify({'success': True}) | |
| return jsonify({'error': 'Not found'}), 404 | |
| if __name__ == '__main__': | |
| app.run(host='0.0.0.0', port=7860, debug=True) | |