from flask import Flask, render_template, request, redirect, url_for, jsonify, send_from_directory import os from werkzeug.utils import secure_filename import json app = Flask(__name__) app.config['UPLOAD_FOLDER'] = 'uploads' app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024 # 16MB max file size app.config['ALLOWED_EXTENSIONS'] = {'png', 'jpg', 'jpeg', 'gif', 'webp'} app.config['ALLOWED_AUDIO'] = {'mp3', 'wav', 'ogg', 'mpeg'} # إنشاء مجلد uploads إذا لم يكن موجوداً os.makedirs(app.config['UPLOAD_FOLDER'], exist_ok=True) def allowed_file(filename): return '.' in filename and filename.rsplit('.', 1)[1].lower() in app.config['ALLOWED_EXTENSIONS'] def allowed_audio(filename): return '.' in filename and filename.rsplit('.', 1)[1].lower() in app.config['ALLOWED_AUDIO'] @app.route('/') def index(): """الصفحة الرئيسية - Valentine Question""" return render_template('valentine.html') @app.route('/admin') def admin(): """صفحة الأدمن لرفع الصور""" images = [] if os.path.exists(app.config['UPLOAD_FOLDER']): images = [f for f in os.listdir(app.config['UPLOAD_FOLDER']) if allowed_file(f)] return render_template('admin.html', images=images) @app.route('/upload', methods=['POST']) def upload_file(): """رفع الصور والملفات الصوتية""" if 'file' not in request.files: return jsonify({'error': 'لا يوجد ملف'}), 400 file = request.files['file'] image_type = request.form.get('image_type', 'other') if file.filename == '': return jsonify({'error': 'لم يتم اختيار ملف'}), 400 # التحقق من نوع الملف is_audio = image_type == 'music' if is_audio and allowed_audio(file.filename): ext = file.filename.rsplit('.', 1)[1].lower() filename = f"music.{ext}" filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename) file.save(filepath) return jsonify({ 'success': True, 'filename': filename, 'message': 'تم رفع الموسيقى بنجاح!' }) elif not is_audio and allowed_file(file.filename): # حفظ الملف باسم يعتمد على نوعه ext = file.filename.rsplit('.', 1)[1].lower() if image_type in ['stitch', 'angela', 'sticker1', 'sticker2', 'background']: filename = f"{image_type}.{ext}" else: filename = secure_filename(file.filename) filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename) file.save(filepath) return jsonify({ 'success': True, 'filename': filename, 'message': 'تم رفع الصورة بنجاح!' }) return jsonify({'error': 'نوع ملف غير مسموح'}), 400 @app.route('/uploads/') def uploaded_file(filename): """عرض الصور المرفوعة""" return send_from_directory(app.config['UPLOAD_FOLDER'], filename) @app.route('/delete/', methods=['POST']) def delete_file(filename): """حذف صورة""" try: filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename) if os.path.exists(filepath): os.remove(filepath) return jsonify({'success': True, 'message': 'تم الحذف بنجاح'}) return jsonify({'error': 'الملف غير موجود'}), 404 except Exception as e: return jsonify({'error': str(e)}), 500 @app.route('/get-images') def get_images(): """الحصول على قائمة الصور المتاحة""" images = {} if os.path.exists(app.config['UPLOAD_FOLDER']): for image_type in ['stitch', 'angela', 'sticker1', 'sticker2', 'background']: for ext in ['png', 'jpg', 'jpeg', 'gif', 'webp']: filename = f"{image_type}.{ext}" if os.path.exists(os.path.join(app.config['UPLOAD_FOLDER'], filename)): images[image_type] = filename break return jsonify(images) @app.route('/get-music') def get_music(): """الحصول على ملف الموسيقى المتاح""" music_data = {} if os.path.exists(app.config['UPLOAD_FOLDER']): for ext in ['mp3', 'wav', 'ogg']: filename = f"music.{ext}" if os.path.exists(os.path.join(app.config['UPLOAD_FOLDER'], filename)): music_data['music_file'] = filename break return jsonify(music_data) if __name__ == '__main__': app.run(debug=False, host='0.0.0.0', port=7860)