diff options
| -rw-r--r-- | main.py | 54 | ||||
| -rw-r--r-- | static/style.css | 21 | ||||
| -rw-r--r-- | templates/index.html | 20 |
3 files changed, 95 insertions, 0 deletions
| @@ -0,0 +1,54 @@ | |||
| 1 | import os | ||
| 2 | |||
| 3 | import libmat2 | ||
| 4 | from libmat2 import parser_factory | ||
| 5 | |||
| 6 | from flask import Flask, flash, request, redirect, url_for, render_template | ||
| 7 | from flask import send_from_directory, after_this_request | ||
| 8 | |||
| 9 | from werkzeug.utils import secure_filename | ||
| 10 | |||
| 11 | UPLOAD_FOLDER = './' | ||
| 12 | ALLOWED_EXTENSIONS = set(['txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif']) | ||
| 13 | |||
| 14 | app = Flask(__name__) | ||
| 15 | app.config['SECRET_KEY'] = '1337' | ||
| 16 | app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER | ||
| 17 | app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024 # 16MB | ||
| 18 | |||
| 19 | |||
| 20 | @app.route('/', methods=['GET', 'POST']) | ||
| 21 | def upload_file(): | ||
| 22 | if request.method == 'POST': | ||
| 23 | if 'file' not in request.files: # check if the post request has the file part | ||
| 24 | flash('No file part') | ||
| 25 | return redirect(request.url) | ||
| 26 | uploaded_file = request.files['file'] | ||
| 27 | if uploaded_file.filename == '': | ||
| 28 | flash('No selected file') | ||
| 29 | return redirect(request.url) | ||
| 30 | filename = secure_filename(uploaded_file.filename) | ||
| 31 | filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename) | ||
| 32 | uploaded_file.save(os.path.join(filepath)) | ||
| 33 | |||
| 34 | parser, mime = parser_factory.get_parser(filepath) | ||
| 35 | if parser is None: | ||
| 36 | flash('The type %s is not supported' % mime) | ||
| 37 | return redirect(url_for('upload_file')) | ||
| 38 | elif parser.remove_all() is not True: | ||
| 39 | flash('Unable to clean ' % mime) | ||
| 40 | return redirect(url_for('upload_file')) | ||
| 41 | os.remove(filename) | ||
| 42 | |||
| 43 | @after_this_request | ||
| 44 | def remove_file(response): | ||
| 45 | os.remove(parser.output_filename) | ||
| 46 | return response | ||
| 47 | |||
| 48 | return send_from_directory(app.config['UPLOAD_FOLDER'], parser.output_filename) | ||
| 49 | |||
| 50 | mimetypes = 'image/jpeg, image/png' | ||
| 51 | return render_template('index.html', mimetypes=mimetypes) | ||
| 52 | |||
| 53 | |||
| 54 | app.run() | ||
diff --git a/static/style.css b/static/style.css new file mode 100644 index 0000000..ed92664 --- /dev/null +++ b/static/style.css | |||
| @@ -0,0 +1,21 @@ | |||
| 1 | header { | ||
| 2 | border-bottom: 1px solid black; | ||
| 3 | width: 100%; | ||
| 4 | } | ||
| 5 | |||
| 6 | header h1 { | ||
| 7 | text-align: center; | ||
| 8 | } | ||
| 9 | header a { | ||
| 10 | text-align:right; | ||
| 11 | } | ||
| 12 | |||
| 13 | section { | ||
| 14 | border-radius: 1em; | ||
| 15 | padding: 1em; | ||
| 16 | position: absolute; | ||
| 17 | top: 50%; | ||
| 18 | left: 50%; | ||
| 19 | margin-right: -50%; | ||
| 20 | transform: translate(-50%, -50%); | ||
| 21 | } | ||
diff --git a/templates/index.html b/templates/index.html new file mode 100644 index 0000000..0496c2d --- /dev/null +++ b/templates/index.html | |||
| @@ -0,0 +1,20 @@ | |||
| 1 | <!doctype html> | ||
| 2 | <html> | ||
| 3 | <head> | ||
| 4 | <title>mat2 - online edition</title> | ||
| 5 | <link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}" type="text/css" /> | ||
| 6 | </head> | ||
| 7 | <body> | ||
| 8 | <header> | ||
| 9 | <h1>mat2 - online edition</h1> | ||
| 10 | </header> | ||
| 11 | <section> | ||
| 12 | <h1>Remove metadata from your images</h1> | ||
| 13 | <p>Drag and drop anywhere you want and get your files cleaned</p> | ||
| 14 | <br> | ||
| 15 | <form method=post enctype=multipart/form-data> | ||
| 16 | <input type=file name=file id="upload_file" accept="{{ mimetypes }}"> | ||
| 17 | <input type=submit value=Upload> | ||
| 18 | </form> | ||
| 19 | </section> | ||
| 20 | </body> | ||
