arrafaqat commited on
Commit
f9670de
·
verified ·
1 Parent(s): 7547fac

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +74 -0
app.py ADDED
@@ -0,0 +1,74 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # app.py
2
+ from flask import Flask, send_file, request, Response, jsonify
3
+ import os
4
+ import base64
5
+
6
+ app = Flask(__name__)
7
+
8
+ @app.route('/')
9
+ def home():
10
+ return "Image Proxy Service for Ghibli Generator"
11
+
12
+ @app.route('/proxy-image')
13
+ def proxy_image():
14
+ """
15
+ Proxies an image from a local file path
16
+ Example usage: /proxy-image?path=/tmp/gradio/4f461b7833afb048edcda4fd8968db08aeb352e871a5445721d63b11f9f489c7/image.webp
17
+ """
18
+ file_path = request.args.get('path')
19
+
20
+ if not file_path:
21
+ return "No path specified", 400
22
+
23
+ # Security check to prevent directory traversal attacks
24
+ if '..' in file_path:
25
+ return "Invalid path", 400
26
+
27
+ # Ensure path starts with a slash
28
+ if not file_path.startswith('/'):
29
+ file_path = '/' + file_path
30
+
31
+ try:
32
+ # Read the file
33
+ with open(file_path, 'rb') as f:
34
+ file_data = f.read()
35
+
36
+ # Return the file
37
+ return Response(file_data, mimetype='image/webp')
38
+ except Exception as e:
39
+ return f"Error accessing file: {str(e)}", 404
40
+
41
+ @app.route('/proxy-image-base64')
42
+ def proxy_image_base64():
43
+ """
44
+ Proxies an image from a local file path and returns it as base64
45
+ Example usage: /proxy-image-base64?path=/tmp/gradio/4f461b7833afb048edcda4fd8968db08aeb352e871a5445721d63b11f9f489c7/image.webp
46
+ """
47
+ file_path = request.args.get('path')
48
+
49
+ if not file_path:
50
+ return jsonify({"error": "No path specified"}), 400
51
+
52
+ # Security check to prevent directory traversal attacks
53
+ if '..' in file_path:
54
+ return jsonify({"error": "Invalid path"}), 400
55
+
56
+ # Ensure path starts with a slash
57
+ if not file_path.startswith('/'):
58
+ file_path = '/' + file_path
59
+
60
+ try:
61
+ # Read the file
62
+ with open(file_path, 'rb') as f:
63
+ file_data = f.read()
64
+
65
+ # Convert to base64
66
+ base64_data = base64.b64encode(file_data).decode('utf-8')
67
+
68
+ # Return the base64 data
69
+ return jsonify({"base64": base64_data, "mime_type": "image/webp"})
70
+ except Exception as e:
71
+ return jsonify({"error": str(e)}), 404
72
+
73
+ if __name__ == '__main__':
74
+ app.run(host='0.0.0.0', port=7860)