File size: 2,203 Bytes
f9670de
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from flask import Flask, send_file, request, Response, jsonify
import os
import base64

app = Flask(__name__)

@app.route('/')
def home():
    return "Image Proxy Service for Ghibli Generator"

@app.route('/proxy-image')
def proxy_image():
    """
    Proxies an image from a local file path
    Example usage: /proxy-image?path=/tmp/gradio/4f461b7833afb048edcda4fd8968db08aeb352e871a5445721d63b11f9f489c7/image.webp
    """
    file_path = request.args.get('path')
    
    if not file_path:
        return "No path specified", 400
    
    # Security check to prevent directory traversal attacks
    if '..' in file_path:
        return "Invalid path", 400
    
    # Ensure path starts with a slash
    if not file_path.startswith('/'):
        file_path = '/' + file_path
    
    try:
        # Read the file
        with open(file_path, 'rb') as f:
            file_data = f.read()
        
        # Return the file
        return Response(file_data, mimetype='image/webp')
    except Exception as e:
        return f"Error accessing file: {str(e)}", 404

@app.route('/proxy-image-base64')
def proxy_image_base64():
    """
    Proxies an image from a local file path and returns it as base64
    Example usage: /proxy-image-base64?path=/tmp/gradio/4f461b7833afb048edcda4fd8968db08aeb352e871a5445721d63b11f9f489c7/image.webp
    """
    file_path = request.args.get('path')
    
    if not file_path:
        return jsonify({"error": "No path specified"}), 400
    
    # Security check to prevent directory traversal attacks
    if '..' in file_path:
        return jsonify({"error": "Invalid path"}), 400
    
    # Ensure path starts with a slash
    if not file_path.startswith('/'):
        file_path = '/' + file_path
    
    try:
        # Read the file
        with open(file_path, 'rb') as f:
            file_data = f.read()
        
        # Convert to base64
        base64_data = base64.b64encode(file_data).decode('utf-8')
        
        # Return the base64 data
        return jsonify({"base64": base64_data, "mime_type": "image/webp"})
    except Exception as e:
        return jsonify({"error": str(e)}), 404

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=7860)