Spaces:
Runtime error
Runtime error
from flask import Flask, url_for, redirect | |
from flask import request as req | |
from flask_cors import CORS | |
app = Flask(__name__) | |
CORS(app) | |
from werkzeug.utils import secure_filename | |
import os | |
from PIL import Image | |
app.config['UPLOAD_FOLDER'] = "static" | |
from pyngrok import ngrok | |
# Open a HTTP tunnel on the default port 80 | |
# <NgrokTunnel: "https://<public_sub>.ngrok.io" -> "http://localhost:80"> | |
http_tunnel = ngrok.connect("1337", "http") | |
print(http_tunnel) | |
def index(): | |
# If a post method then handle file upload | |
if req.method == 'POST': | |
if 'file' not in req.files: | |
return redirect('/') | |
file = req.files['file'] | |
if file : | |
filename = secure_filename(file.filename) | |
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename)) | |
if ("camera" in file.filename or "capture" in file.filename or "IMG" in file.filename or "Screenshot" in file.filename) : | |
img=Image.open(f"static/{filename}") | |
img.thumbnail((512, 512),Image.Resampling.LANCZOS) | |
img.save(f"static/{filename}") | |
return filename | |
# Get Files in the directory and create list items to be displayed to the user | |
file_list = '' | |
for f in os.listdir(app.config['UPLOAD_FOLDER']): | |
# Create link html | |
link = url_for("static", filename=f) | |
file_list = file_list + '<li><a href="%s">%s</a></li>' % (link, f) | |
# Format return HTML - allow file upload and list all available files | |
return_html = ''' | |
<!doctype html> | |
<title>Upload File</title> | |
<h1>Upload File</h1> | |
<form method=post enctype=multipart/form-data> | |
<input type=file name=file><br> | |
<input type=submit value=Upload> | |
</form> | |
<hr> | |
<h1>Files</h1> | |
<ol>%s</ol> | |
''' % file_list | |
return return_html | |
if __name__ == '__main__': | |
config = { | |
'host': 'localhost', | |
'port': 1337, | |
'debug': True, | |
} | |
app.run(**config) |