diff options
| author | Jules Laplace <julescarbon@gmail.com> | 2020-06-23 23:18:07 +0200 |
|---|---|---|
| committer | Jules Laplace <julescarbon@gmail.com> | 2020-06-23 23:18:07 +0200 |
| commit | 3cf70771cb45cc16ec33ffe44e7a1a4799d8f395 (patch) | |
| tree | 55f0edb53141d5f043b486d722f507bfd94abdea /animism-align/cli/app/controllers/upload_controller.py | |
| parent | 014816dc724c1be60b7dd28d4e608c89b4ed451c (diff) | |
adding web app base
Diffstat (limited to 'animism-align/cli/app/controllers/upload_controller.py')
| -rw-r--r-- | animism-align/cli/app/controllers/upload_controller.py | 142 |
1 files changed, 142 insertions, 0 deletions
diff --git a/animism-align/cli/app/controllers/upload_controller.py b/animism-align/cli/app/controllers/upload_controller.py new file mode 100644 index 0000000..86f9f29 --- /dev/null +++ b/animism-align/cli/app/controllers/upload_controller.py @@ -0,0 +1,142 @@ +from flask import request, jsonify +from flask_classful import FlaskView, route +from werkzeug.datastructures import MultiDict +from werkzeug.utils import secure_filename +import os +import numpy as np +from PIL import Image + +from app.settings import app_cfg +from app.sql.common import db, Session +from app.sql.models.upload import Upload +from app.utils.file_utils import sha256_stream, sha256_tree, VALID_IMAGE_EXTS +from app.server.decorators import APIError + +class UploadView(FlaskView): + def index(self): + """ + List all uploaded files. + + * Query string params: offset, limit, sort (id, date), order (asc, desc) + """ + session = Session() + uploads = session.query(Upload).all() + response = { + 'status': 'ok', + 'res': [ upload.toJSON() for upload in uploads ], + } + session.close() + return jsonify(response) + + def get(self, id): + """ + Fetch a single upload. + """ + session = Session() + upload = session.query(Upload).get(id) + response = { + 'status': 'ok', + 'res': upload.toJSON(), + } + session.close() + return jsonify(response) + + def post(self): + """ + Upload a new file. + + * JSON params: username + """ + + try: + username = request.form.get('username') + except: + raise APIError('No username specified') + + param_name = 'image' + if param_name not in request.files: + raise APIError('No file uploaded') + + file = request.files[param_name] + + # get sha256 + sha256 = sha256_stream(file) + _, ext = os.path.splitext(file.filename) + if ext == '.jpeg': + ext = '.jpg' + + # TODO: here check sha256 + # upload = Upload.query.get(id) + + if ext[1:] not in VALID_IMAGE_EXTS: + return jsonify({ 'status': 'error', 'error': 'Not a valid image' }) + + # convert string of image data to uint8 + file.seek(0) + nparr = np.fromstring(file.read(), np.uint8) + + # decode image + try: + im = Image.fromarray(nparr) + except: + return jsonify({ 'status': 'error', 'error': 'Image parse error' }) + + session = Session() + upload = session.query(Upload).filter_by(sha256=sha256).first() + if upload is not None: + print("Already uploaded image") + response = { + 'status': 'ok', + 'notes': 'Image already uploaded', + 'res': upload.toJSON(), + } + session.close() + return jsonify(response) + + uploaded_im_fn = secure_filename(sha256 + ext) + uploaded_im_abspath = os.path.join(app_cfg.DIR_UPLOADS, sha256_tree(sha256)) + uploaded_im_fullpath = os.path.join(uploaded_im_abspath, uploaded_im_fn) + + os.makedirs(uploaded_im_abspath, exist_ok=True) + nparr.tofile(uploaded_im_fullpath) + + upload = Upload(username=username, sha256=sha256, ext=ext) + session.add(upload) + session.commit() + response = { + 'status': 'ok', + 'res': upload.toJSON(), + } + session.close() + return jsonify(response) + + def delete(self, id): + """ + Delete an uploaded file. + """ + session = Session() + upload = session.query(Upload).get(id) + if not upload: + session.close() + return jsonify({ + 'status': 'error', + 'error': 'not found', + }) + + sha256 = upload.sha256 + + uploaded_im_fn = secure_filename(sha256 + upload.ext) + uploaded_im_abspath = os.path.join(app_cfg.DIR_UPLOADS, sha256_tree(sha256)) + uploaded_im_fullpath = os.path.join(uploaded_im_abspath, uploaded_im_fn) + if os.path.exists(uploaded_im_fullpath): + print("Removing " + uploaded_im_fullpath) + os.remove(uploaded_im_fullpath) + + session.delete(upload) + session.commit() + response = { + 'status': 'ok', + 'id': id, + } + session.close() + return jsonify(response) |
