You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

68 lines
2.2 KiB
Python

# This is a flask application that overlaps uploaded images and text on top of images laid out in a grid on the browser.
from flask import Flask
from flask import request, redirect
from flask import render_template
import glob
import os
UPLOAD_FOLDER = './static/img/'
ALLOWED_EXTENSIONS = {'txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif'}
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
# Kamo's prefix to add /soupboat/padliography to all the routes
class PrefixMiddleware(object):
def __init__(self, app, prefix=""):
self.app = app
self.prefix = prefix
def __call__(self, environ, start_response):
if environ["PATH_INFO"].startswith(self.prefix):
environ["PATH_INFO"] = environ["PATH_INFO"][len(self.prefix):]
environ["SCRIPT_NAME"] = self.prefix
return self.app(environ, start_response)
else:
start_response("404", [("Content-Type", "text/plain")])
return ["This url does not belong to the app.".encode()]
# create flask application
app = Flask(__name__)
# Get the URL prefix for the soupboat
# register the middleware to use our base_url as prefix on all the requests
base_url = os.environ.get('BASE_URL', '')
app.wsgi_app = PrefixMiddleware(app.wsgi_app, prefix=base_url)
# configure the upload folder
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
@app.route("/")
def overlap():
images = glob.glob('./static/img/*.png')
print(images)
return render_template("mosaic.html", images=images)
from flask import request
@app.route('/upload', methods=['POST'])
def upload_file():
if request.method == 'POST':
file = request.files['tile']
# print(request.files["zine test.png"])
if file and allowed_file(file.filename):
filename = file.filename
print("zine test.png",filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
overwrite = request.form["overwrite"]
os.system(f"cp ./static/img/{ filename } ./static/img/{ overwrite }")
os.system(f"rm ./static/img/{ filename }")
return redirect("/")
# f = request.files['the_file']
# f.save('/var/www/uploads/uploaded_file.txt')