From d76f7ca169954f276f42e94085baaec7abf8a4da Mon Sep 17 00:00:00 2001 From: Yannik Schmidt Date: Thu, 22 Jul 2021 16:50:11 +0200 Subject: [PATCH] add simple upload --- README.md | 13 ++++++++----- server.py | 21 +++++++++++++++++++++ templates/upload.html | 9 +++++++++ 3 files changed, 38 insertions(+), 5 deletions(-) create mode 100644 templates/upload.html diff --git a/README.md b/README.md index 7306b66..86e1914 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # python-flask picture factory This server provides and caches images based on url-arguments. -# Ussage +# Usage Place your images in the "/pictures/"-subdirectory. ../pictures @@ -17,19 +17,22 @@ or: /usr/bin/waitress-serve --host 127.0.0.1 --port 5002 --call 'app:createApp'* -Retrive the images with these URLs: +Retrieve the images with these URLs: http://server:port/media/picture1.png?x=100&y=200&encoding=webp http://server:port/media/picture2.jpg?x=100 http://server:port/media/picture3.jpg?y=200&encoding=png -You may omitt any of the parameters. Not giving any parameters will return the original image. You must always name the original picture in your URL, even if you want a different encoding. +You may omit any of the parameters. Not giving any parameters will return the original image. You must always name the original picture in your URL, even if you want a different encoding. # Other possible URL-parameters: In addition to *encoding*, *scaleX* and *scaleY* you may also use the following parameters: force=True/False -> apply the x/y-values as given, cut off image when necessary - cacheTimeout=SECONDS -> add a cache timeout header witht the given value to the response + cacheTimeout=SECONDS -> add a cache timeout header with the given value to the response -# Futher explanation +# Uploading +This tool is not intended for uploading or large amounts of files, use SFTP/FTPS or whatever your server provides for. For pure convenience usage there is a */upload*/-location. It must be enabled by creating a file called *upload.enable* in the project root before the server is started. + +# Further explanation I wrote a small article with some more example on how to best use this to optimize your website -if you want to use it for that: [Medium](https://medium.com/anti-clickbait-coalition/responsive-image-factory-f1ed6e61d13c) diff --git a/server.py b/server.py index c0778db..95d25a3 100755 --- a/server.py +++ b/server.py @@ -4,6 +4,7 @@ import flask import argparse import sys import PIL.Image +import werkzeug.utils app = flask.Flask("Picture factory app", static_folder=None) PICTURE_DIR = "pictures/" @@ -129,6 +130,26 @@ def list(): return flask.render_template("index.html", paths=retStringArr) +@app.route("/upload", methods = ['GET', 'POST']) +def upload(): + if not app.config['UPLOAD_ENABLED']: + return ("Upload Disabled", 403) + if flask.request.method == 'POST': + f = flask.request.files['file'] + sfName = os.path.join(PICTURE_DIR, werkzeug.utils.secure_filename(f.filename)) + if not os.path.isfile(sfName): + f.save(sfName) + return ('Success', 204) + else: + return ('Conflicting File', 409) + else: + return flask.render_template("upload.html") + +@app.before_first_request +def init(): + app.config['MAX_CONTENT_PATH'] = 32+1000*1000 + app.config['UPLOAD_FOLDER'] = PICTURE_DIR + app.config['UPLOAD_ENABLED'] = os.path.isfile("./upload.enable") if __name__ == "__main__": diff --git a/templates/upload.html b/templates/upload.html new file mode 100644 index 0000000..6e9cc33 --- /dev/null +++ b/templates/upload.html @@ -0,0 +1,9 @@ + + +
+ + +
+ +