File size: 4,505 Bytes
2978bba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
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