forked from Seagate/cortx-images
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.py
77 lines (60 loc) · 2.12 KB
/
api.py
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import os
import settings
import flask
from flask import send_file, request, abort, render_template
from functools import wraps
import bucket
import image
app = flask.Flask(__name__)
app.config["DEBUG"] = True
def require_api_key(view_function):
@wraps(view_function)
def decorated_function(*args, **kwargs):
api_key = os.environ.get('api_key')
if request.headers.get('x-api-key') and request.headers.get('x-api-key') == api_key:
return view_function(*args, **kwargs)
elif request.args.get('key') and request.args.get('key') == api_key:
return view_function(*args, **kwargs)
else:
abort(401)
return decorated_function
@app.route('/images/<size>/<filename>', methods=['GET'])
def get_resized_image(size, filename):
w, h = size.split("x")
_, ext = filename.split(".")
filename_with_size = "{}_{}x{}.jpg".format(filename, w, h)
file_path = bucket.download(filename_with_size)
if file_path:
return send_file(file_path, mimetype='image/{}'.format(ext))
file_path = bucket.download(filename)
if not file_path:
return abort(404)
resized_file_path = image.resize(file_path, (int(w), int(h)))
bucket.upload(resized_file_path, filename)
return send_file(resized_file_path, mimetype='image/{}'.format(ext))
@app.route('/images/<filename>', methods=['GET'])
def get_image(filename):
_, ext = filename.split(".")
file_path = bucket.download(filename)
if not file_path:
return abort(404)
return send_file(file_path, mimetype='image/{}'.format(ext))
@app.route('/images', methods=['post'])
@require_api_key
def upload():
if request.files['image'].filename != '':
image = request.files['image']
file_path = './.tmp/{}'.format(image.filename)
image.save(file_path)
uploaded = bucket.upload(file_path, image.filename)
if uploaded:
return "uploaded"
return abort(400)
return abort(500)
@app.route('/')
def home():
return render_template('index.html')
@app.route('/api/docs')
def docs():
return render_template('docs.html')
app.run()