import os from flask import request, jsonify from functools import wraps ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg', 'webp', 'bmp'} def allowed_file(filename): return '.' in filename and \ filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS def validate_detection_request(f): @wraps(f) def decorated(*args, **kwargs): if 'image' not in request.files: return jsonify({'error': 'No image provided. Form parameter "image" must contain an image file.'}), 400 file = request.files['image'] if not file or not file.filename: return jsonify({'error': 'No image selected. Please upload a valid image file.'}), 400 if not allowed_file(file.filename): return jsonify({'error': f"Invalid file type. Allowed extensions: {', '.join(ALLOWED_EXTENSIONS)}"}), 400 # Validate mode if present if 'mode' in request.form: mode = request.form.get('mode').lower() if mode not in ('fast', 'deep_forensics', 'auto'): return jsonify({'error': "Invalid mode. Allowed values: 'fast', 'deep_forensics', 'auto'."}), 400 # Validate detection_type if present if 'detection_type' in request.form: d_type = request.form.get('detection_type').lower() if d_type not in ('morph', 'deepfake', 'both'): return jsonify({'error': "Invalid detection_type. Allowed values: 'morph', 'deepfake', 'both'."}), 400 return f(*args, **kwargs) return decorated def validate_demorph_request(f): @wraps(f) def decorated(*args, **kwargs): if 'image' not in request.files: return jsonify({'error': 'No image provided. Form parameter "image" must contain an image file.'}), 400 file = request.files['image'] if not file or not file.filename: return jsonify({'error': 'No image selected. Please upload a valid image file.'}), 400 if not allowed_file(file.filename): return jsonify({'error': f"Invalid file type. Allowed extensions: {', '.join(ALLOWED_EXTENSIONS)}"}), 400 # Validate reference if present if 'reference' in request.files: ref_file = request.files['reference'] if ref_file and ref_file.filename and not allowed_file(ref_file.filename): return jsonify({'error': f"Invalid reference file type. Allowed extensions: {', '.join(ALLOWED_EXTENSIONS)}"}), 400 # Validate method if present if 'method' in request.form: method = request.form.get('method').lower() if method not in ('transformer', 'diffusion', 'gan'): return jsonify({'error': "Invalid demorphing method. Allowed values: 'transformer', 'diffusion', 'gan'."}), 400 return f(*args, **kwargs) return decorated def validate_batch_detection_request(f): @wraps(f) def decorated(*args, **kwargs): if 'images' not in request.files: return jsonify({'error': 'No images provided. Form parameter "images" must contain at least one image file.'}), 400 files = request.files.getlist('images') if not files or len(files) == 0 or (len(files) == 1 and files[0].filename == ''): return jsonify({'error': 'No images selected. Please upload at least one valid image file.'}), 400 for file in files: if not file or not file.filename or not allowed_file(file.filename): return jsonify({'error': f"Invalid file or file type. Allowed extensions: {', '.join(ALLOWED_EXTENSIONS)}"}), 400 # Validate mode if present if 'mode' in request.form: mode = request.form.get('mode').lower() if mode not in ('fast', 'deep_forensics', 'auto'): return jsonify({'error': "Invalid mode. Allowed values: 'fast', 'deep_forensics', 'auto'."}), 400 # Validate detection_type if present if 'detection_type' in request.form: d_type = request.form.get('detection_type').lower() if d_type not in ('morph', 'deepfake', 'both'): return jsonify({'error': "Invalid detection_type. Allowed values: 'morph', 'deepfake', 'both'."}), 400 return f(*args, **kwargs) return decorated