add simple upload

This commit is contained in:
Yannik Schmidt
2021-07-22 16:50:11 +02:00
parent 3fe17b4743
commit d76f7ca169
3 changed files with 38 additions and 5 deletions

View File

@@ -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)

View File

@@ -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__":

9
templates/upload.html Normal file
View File

@@ -0,0 +1,9 @@
<html>
<body>
<form action = "/upload" method = "POST"
enctype = "multipart/form-data">
<input type = "file" name = "file" />
<input type = "submit"/>
</form>
</body>
</html>