-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
124 lines (105 loc) · 3.91 KB
/
app.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import os
import uuid
import datetime
from flask import Flask, request, jsonify, send_from_directory
from werkzeug.utils import secure_filename
from wand.image import Image
from config import SETTINGS
from utils import is_allowed_file, get_cassandra_session
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = SETTINGS["UPLOAD_FOLDER"]
app.config['MAX_CONTENT_LENGTH'] = SETTINGS["MAX_CONTENT_LENGTH"]
session = get_cassandra_session()
# the root url containing app info
@app.route('/')
def info():
return jsonify(SETTINGS["INFO"])
# the file upload url
@app.route('/api/v1/image', methods=["GET", "POST"])
def upload_file():
if request.method == 'POST':
# check if the post request has the file part
if 'image' not in request.files:
return jsonify({"success": False})
file = request.files['image']
# if user does not select file, browser also
# submit an empty part without filename
if file.filename == '':
return jsonify({"success": False})
if file and is_allowed_file(file.filename):
filename = secure_filename(file.filename)
uuid_val = uuid.uuid1()
filename = "{}#{}".format(uuid_val, filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
# now insert into cassandra
# filename
session.execute(
"""
INSERT INTO images (id, path, uploaded)
VALUES (%s, %s, %s)
""",
(uuid_val, filename, datetime.datetime.now())
)
return jsonify({
"success": True,
"id": uuid_val
})
return '''
<!doctype html>
<title>Upload new File</title>
<h1>Upload new File</h1>
<form method=post enctype=multipart/form-data>
<input type=file name=image>
<input type=submit value=Upload>
</form>
'''
# read the file
@app.route('/api/v1/image/<image_id>', methods=["GET"])
def read_file(image_id):
try:
rows = session.execute('SELECT * FROM images where id={}'.format(image_id))
if rows[0]:
width = request.args.get('width')
height = request.args.get('height')
if not width or not height:
# if resize params are not passed, return the actual file
return send_from_directory(SETTINGS["UPLOAD_FOLDER"], rows[0].path)
else:
filename = "{}/{}x{}#{}".format(
SETTINGS["CACHE_FOLDER"],
width,
height,
rows[0].path
)
# if there is already a resized file
if os.path.isfile(filename):
# serve it
return send_from_directory(SETTINGS["CACHE_FOLDER"], "{}x{}#{}".format(
width,
height,
rows[0].path
))
# if there is no resized file, resize it and save
with Image(filename=os.path.join(app.config['UPLOAD_FOLDER'], rows[0].path)) as img:
with img.clone() as i:
i.resize(int(width), int(height))
i.save(filename="{}/{}x{}#{}".format(
SETTINGS["CACHE_FOLDER"],
width,
height,
rows[0].path
))
# serve it
return send_from_directory(SETTINGS["CACHE_FOLDER"], "{}x{}#{}".format(
width,
height,
rows[0].path
))
return jsonify({
"success": False
})
except Exception as e:
return jsonify({
"success": False,
"message": str(e)
})