-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
156 lines (142 loc) · 5.11 KB
/
app.py
File metadata and controls
156 lines (142 loc) · 5.11 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import os
from flask import Flask, request, send_file
from werkzeug.utils import secure_filename
import psycopg2
from psycopg2.extras import RealDictCursor
from PIL import Image
import logging
import io
import json
import boto3
logging.getLogger('boto').setLevel(logging.WARNING)
logging.basicConfig(level=logging.INFO)
ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg'}
ALLOWED_MIME = {'image/jpeg', 'image/png'}
RESULT_PATH = os.path.dirname(os.path.abspath(__file__)) + '/im/'
app = Flask(__name__)
app.config['MAX_CONTENT_LENGTH'] = 10 * 1024 * 1024
SEND_TO_QUEUE = True
def _get_postgres_conn():
conn_uri = os.environ['PG_CONN_IM']
return psycopg2.connect(conn_uri)
def allowed_ext(filename):
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
def allowed_mime(filemime):
return filemime in ALLOWED_MIME
@app.route('/images', methods=['POST'])
def index():
logging.info(request)
logging.info(request.form)
if 'file' not in request.files:
return 'No file part', 400
file = request.files['file']
if file.filename == '':
return 'No selected file', 400
if not file or not allowed_ext(file.filename) or not allowed_mime(file.mimetype):
return 'Not allowed', 400
if 'width' not in request.form or 'height' not in request.form:
return 'No size part', 400
filewidth = request.form['width']
fileheight = request.form['height']
if int(filewidth) not in range(1, 9999) or int(fileheight) not in range(1, 9999):
return 'Not allowed size', 400
logging.info(file)
logging.info(file.mimetype)
filename = secure_filename(file.filename)
f = open(filename,'rb')
filedata = f.read()
filemime = file.mimetype
conn = _get_postgres_conn()
cur = conn.cursor(cursor_factory=RealDictCursor)
cur.execute("""
INSERT INTO images (id, im_name, im_file, im_mime, im_width, im_height, status)
VALUES (DEFAULT, %s, %s, %s, %s, %s, 'not_ready') RETURNING id
""", (filename, filedata, filemime, filewidth, fileheight))
result = cur.fetchone()
conn.commit()
msg_body = json.dumps(dict(id=result['id']))
if SEND_TO_QUEUE is False:
return result
sqs = boto3.resource('sqs')
queue = sqs.get_queue_by_name(QueueName='images_queue.fifo')
response = queue.send_message(MessageBody=msg_body, MessageGroupId='resize-group')
if response['ResponseMetadata']['HTTPStatusCode'] != 200:
logging.error('Cannot send to queue: %s', response)
return json.dumps(dict(error="Cannot add to queue"))
return result
import time
def resize_img(event, context):
body = event['Records'][0]["body"]
time.sleep(15)
try:
json_data = json.loads(body)
except ValueError as e:
logging.error("Error decoding data: %s %s", body, e)
return False
im_id = json_data['id']
conn = _get_postgres_conn()
cur = conn.cursor(cursor_factory=RealDictCursor)
cur.execute("SELECT * FROM images WHERE id=%s", (im_id,))
result = cur.fetchone()
im_file = result['im_file']
im_name = result['im_name']
im_mime = result['im_mime']
im_width = result['im_width']
im_height = result['im_height']
im_b = io.BytesIO(im_file.tobytes())
im = Image.open(im_b)
res_im = im.resize((im_width, im_height))
buffer = io.BytesIO()
if im_mime == 'image/jpeg':
res_im.save(buffer, format='JPEG')
if im_mime == 'image/png':
res_im.save(buffer, format='PNG')
new_im_file = buffer.getvalue()
w = str(im_width)
h = str(im_height)
new_im_name = '_'.join([w, h, im_name])
conn = _get_postgres_conn()
cur = conn.cursor(cursor_factory=RealDictCursor)
cur.execute("""
UPDATE images SET im_name = %s, im_file = %s, status = 'ready'
WHERE id=%s
""", (new_im_name, new_im_file, im_id))
conn.commit()
@app.route('/newimages', methods=['GET', 'POST'])
def get_im():
data = request.get_json()
if 'id' not in data:
return "No id part", 400
im_id = data['id']
logging.info(im_id)
logging.info(not im_id)
if not im_id:
return 'Invalid request', 400
conn = _get_postgres_conn()
cur = conn.cursor(cursor_factory=RealDictCursor)
cur.execute("SELECT * FROM images WHERE id=%s", (im_id, ))
result = cur.fetchone()
if result is None:
return 'Not found', 404
img_status = result['status']
if img_status == 'not_ready':
return 'Repeat request later, image is not resized', 404
img_file = result['im_file']
img_mime = result['im_mime']
return send_file(io.BytesIO(img_file.tobytes()), mimetype=img_mime)
@app.route('/status', methods=['GET', 'POST'])
def get_status():
data = request.get_json()
if not data or 'id' not in data:
return "No id part", 400
im_id = data['id']
if not im_id:
return 'Invalid request', 400
conn = _get_postgres_conn()
cur = conn.cursor(cursor_factory=RealDictCursor)
cur.execute("SELECT status FROM images WHERE id=%s", (im_id, ))
result = cur.fetchone()
if result:
return dict(status=result['status'])
else:
return "Not found", 404