summaryrefslogtreecommitdiff
path: root/cli/app/controllers/upload_controller.py
diff options
context:
space:
mode:
Diffstat (limited to 'cli/app/controllers/upload_controller.py')
-rw-r--r--cli/app/controllers/upload_controller.py75
1 files changed, 44 insertions, 31 deletions
diff --git a/cli/app/controllers/upload_controller.py b/cli/app/controllers/upload_controller.py
index 86f9f29..94a7fd1 100644
--- a/cli/app/controllers/upload_controller.py
+++ b/cli/app/controllers/upload_controller.py
@@ -15,18 +15,22 @@ 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)
+ List all uploads
"""
session = Session()
- uploads = session.query(Upload).all()
- response = {
+ query = session.query(Upload)
+ graph_id = args.get('graph_id', default=None)
+ if graph_id is not None:
+ query = query.filter(Upload.graph_id == int(graph_id))
+
+ items = query.all()
+
+ res = {
'status': 'ok',
- 'res': [ upload.toJSON() for upload in uploads ],
+ 'res': [ item.toJSON() for item in items ],
}
session.close()
- return jsonify(response)
+ return jsonify(res)
def get(self, id):
"""
@@ -50,14 +54,31 @@ class UploadView(FlaskView):
try:
username = request.form.get('username')
+ # print(username)
except:
raise APIError('No username specified')
- param_name = 'image'
- if param_name not in request.files:
- raise APIError('No file uploaded')
+ try:
+ tag = request.form.get('tag')
+ # print(tag)
+ except:
+ raise APIError('No tag specified')
- file = request.files[param_name]
+ try:
+ graph_id = request.form.get('graph_id')
+ # print(graph_id)
+ except:
+ raise APIError('No graph_id specified')
+
+ if 'image' in request.files:
+ file = request.files['image']
+ # print(fn)
+ elif 'file' in request.files:
+ file = request.files['file']
+ # print(request.form.get('__image_filename'))
+ # print(fn)
+ else:
+ raise APIError('No file uploaded')
# get sha256
sha256 = sha256_stream(file)
@@ -65,42 +86,34 @@ class UploadView(FlaskView):
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' })
+ ext = ext[1:]
- # 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' })
+ uploaded_im_fn = secure_filename(file.filename)
+ uploaded_im_abspath = os.path.join(app_cfg.DIR_UPLOADS, str(graph_id), tag)
+ uploaded_im_fullpath = os.path.join(uploaded_im_abspath, uploaded_im_fn)
session = Session()
upload = session.query(Upload).filter_by(sha256=sha256).first()
if upload is not None:
- print("Already uploaded image")
+ print("Already uploaded file")
+ if not os.path.exists(uploaded_im_fullpath):
+ # if we got in some weird state where the record wasnt deleted....
+ os.makedirs(uploaded_im_abspath, exist_ok=True)
+ file.save(uploaded_im_fullpath)
response = {
'status': 'ok',
- 'notes': 'Image already uploaded',
+ 'notes': 'File 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)
+ file.save(uploaded_im_fullpath)
- upload = Upload(username=username, sha256=sha256, ext=ext)
+ upload = Upload(username=username, tag=tag, fn=uploaded_im_fn, sha256=sha256, ext=ext, graph_id=graph_id)
session.add(upload)
session.commit()
response = {