FlaskProxy / app.py
arrafaqat's picture
Update app.py
aeacb6f verified
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)