summaryrefslogtreecommitdiff
path: root/fbin/api.py
blob: e6520193c295e6bac2cf90967716ff020e856fd7 (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
55
56
57
58
59
60
61
62
import functools

from flask import Blueprint, current_app, request, jsonify
from flask.views import MethodView
from flask_login import current_user

from . import db
# FIXME
from .fbin import get_file

app = Blueprint('api', __name__)

# TODO: Implement this stuff.

def makejson(f):
    @functools.wraps(f)
    def wrapper(*args, **kwargs):
        r = f(*args, **kwargs)
        if isinstance(r, dict):
            r = jsonify(r)
        return r
    return wrapper

def api_login_required(f):
    def wrapper(*args, **kwargs):
        if not current_user.is_authenticated:
            return {
                'status': False,
                'message': 'Not authenticated'
            }
        return f(*args, **kwargs)
    return wrapper

class FileAPI(MethodView):
    decorators = [api_login_required, makejson]

    def put(self, hash):
        f = get_file(hash, user_id = current_user.get_id())
        if not f:
            return {
                'status': False,
                'message': 'File not found'
            }
        filename = request.form.get('filename')
        if not filename:
            return {
                'status': False,
                'message': 'Empty or missing filename',
            }
        with db.session_scope() as sess:
            f.filename = filename
            sess.add(f)
        return {
            'status': True,
        }

    def delete(self, hash):
        pass

file_api_view = FileAPI.as_view('file_api')
app.add_url_rule('/file/<hash>', view_func = file_api_view, methods = ['PUT', 'DELETE'])