summaryrefslogtreecommitdiffstats
path: root/clover/controller/control/api/file_upload.py
blob: a532bc8be3e62142967de6a20f5ec2917570d820 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# Copyright (c) Authors of Clover
#
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Apache License, Version 2.0
# which accompanies this distribution, and is available at
# http://www.apache.org/licenses/LICENSE-2.0

from flask import Blueprint, request, Response, jsonify
import redis
import logging

file_upload = Blueprint('file_upload', __name__)

HOST_IP = 'redis.default'


@file_upload.route("/upload", methods=['GET', 'POST'])
def set_upload_metadata():
    try:
        response = "Uploaded file(s) successfully"
        content = request.form
        logging.debug(content)
        r = redis.StrictRedis(host=HOST_IP, port=6379, db=0)
        # Try various variable names
        upload_var_names = ['upload', 'file1', 'file2', 'file3',
                            'file4', 'file5', 'file6']
        for n in upload_var_names:
            try:
                param_name = n + '.name'
                meta_name = content.get(param_name)
                if meta_name:
                    param_path = n + '.path'
                    param_server = n + '.server'
                    meta_path = content.get(param_path)
                    meta_server = content.get(param_server)
                    entry = meta_name + ':' + meta_path + ':' + meta_server
                    r.sadd('upload_metadata', entry)
            except Exception as e:
                print("no metadata")
    except Exception as e:
        logging.debug(e)
        return Response('Unable to write file metadata to redis', status=400)
    return response


@file_upload.route("/upload/get", methods=['GET', 'POST'])
def get_upload_metadata():
    try:
        r = redis.StrictRedis(host=HOST_IP, port=6379, db=0)
        response = jsonify(list(r.smembers('upload_metadata')))
    except Exception as e:
        logging.debug(e)
        return Response('Unable to retrieve upload metadata', status=400)
    return response