-
Notifications
You must be signed in to change notification settings - Fork 2
/
handlers.py
275 lines (233 loc) · 9.12 KB
/
handlers.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
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# -*- coding:utf-8 -*-
'url handlers'
# import time
import uuid
import os
import magic
import json
import io
import logging
import re
from bottle import template, request, response, HTTPResponse, abort
from PIL import Image, ImageFilter
import redis
from img_process import *
UPLOAD_FOLDER = '/Users/liyouzhi/dev/python/zimg-python/image_storage'
ALLOWED_EXTENSIONS = set(['png', 'jpg', 'jpeg', 'gif', 'webp'])
EXPIRE_TIME = 60 * 15
SETTING_OPTIONS = set(['fill', 'fit', 'stretch'])
POSITIONS = set(['center', 'left_top', 'left_bottom', 'right_top', 'right_bottom'])
WATER_MARK_DEFAULT = '/Users/liyouzhi/dev/python/zimg-python/static/watermark.png'
cache = redis.StrictRedis(host='127.0.0.1', port=6379, db=1)
def next_id():
# return '%016d%s%s' % (int(time.time() * 10000), uuid.uuid4().hex, '0'*16)
return uuid.uuid4().hex
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
def id2address(id):
addrs = [str(int(id[i:i + 7], 16) % 1024) for i in range(0, 17, 8)]
path = UPLOAD_FOLDER
# path = app.config['UPLOAD_FOLDER']
# for addr in addrs:
# path = os.path.join(path, addr)
path = os.path.join(path, '/'.join(addrs))
return path
def gen_cache_key(id, w=0, h=0, format='', s='', a=0, filter='', ratio='', quality='', watermark=0, position='', watermark_text=''):
return 'ick:v0:' + id + ':' + str(w) + ':' + str(
h) + ':' + format + ':' + s + ':' + str(a) + ':' + filter + ':' + str(ratio) + ':' + quality + ':' + str(watermark) + ':' + position + ':' + watermark_text
# def get_cache(cache_id):
# parts = cache_id.split(':')
# data = cache.get(cache_id)
# if data
def get_image_size(path):
img = Image.open(path)
return img.size
def home():
return template('index.html')
def post_image():
data = request.body.read()
if not data:
abort(400, 'upload file not found!')
mtype = magic.from_buffer(data, mime=True)
im_type = mtype.split('/')
if len(im_type) < 2:
abort(400, 'illegal mimetype!')
if im_type[1] not in ALLOWED_EXTENSIONS:
abort(400, 'invalid image formait!')
id = next_id()
addr = id2address(id)
# create_dir(addr)
os.makedirs(addr)
path = os.path.join(addr, id)
with open(path, 'wb') as f:
f.write(data)
logging.info('upload image "%s" to "%s"', id, addr)
size = get_image_size(path)
ret = {'key': id, 'size': size}
return json.dumps(ret)
def multipart_post_image():
file = request.files.get('file')
if not file:
abort(400, 'upload file not found!')
if not allowed_file(file.filename):
abort(400, 'upload file illegal!')
id = next_id()
addr = id2address(id)
os.makedirs(addr)
# create_dir(addr)
path = os.path.join(addr, id)
file.save(path)
logging.info('upload image "%s" to "%s"', id, addr)
size = get_image_size(path)
ret = {'key': id, 'size': size}
return json.dumps(ret)
RE_ID = r'^[0-9a-f]{32}$'
RE_ID_EXT = r'^([0-9a-f]{32})\.([a-z]+)$'
def get_image(image_id):
# parts = image_id.split('.')
# if len(parts) != 1 and len(parts) != 2:
# abort(400, 'invalid request!')
# if len(parts[0]) != 32:
# abort(400, 'invalid ID!')
#
# image_id = parts[0]
#
# ext = ''
# if len(parts) > 1:
# ext = parts[1].lower()
# if ext not in ALLOWED_EXTENSIONS:
# abort(400, 'invalid format!')
# if ext == 'jpg': ext = 'jpeg'
image_id = image_id.lower()
re_id = re.match(RE_ID, image_id)
re_id_ext = re.match(RE_ID_EXT, image_id)
if re_id:
ext = ''
elif re_id_ext:
image_id = re_id_ext.group(1)
ext = re_id_ext.group(2)
if ext not in ALLOWED_EXTENSIONS:
abort(400, 'invalid format!')
else:
abort(400, 'invalid request!')
if ext == 'jpg': ext = 'jpeg'
addr = id2address(image_id)
path = os.path.join(addr, image_id)
h = int(request.query.h or 0) #参数大小的限制?
w = int(request.query.w or 0)
s = request.query.s
a = int(request.query.a or 0)
filter = request.query.f
ratio = request.query.r
quality = request.query.q
watermark = int(request.query.watermark or 0)
position = request.query.p
watermark_text = request.query.text
if s and s not in SETTING_OPTIONS:
abort(400, 'invalid options!')
if a >= 360:
abort(400, 'invalid angle!')
if ratio and (int(ratio) > 100 or int(ratio) < 0):
abort(400, 'invalid ratio!')
if quality and (int(quality) > 100 or int(quality) < 0):
abort(400, 'invalid quality!')
if watermark:
if not position:
position = 'right_bottom'
elif position not in POSITIONS:
abort(400, 'invalid position! valid positions:[centre, left_top, left_bottom, right_top, right_bottom]')
cache_id = gen_cache_key(image_id, w, h, ext, s, a, filter, ratio, quality, watermark, position, watermark_text)
data = cache.get(cache_id)
# data = None
if data is None:
data = cache.get(gen_cache_key(image_id))
if data is None:
with open(path, 'rb') as f:
data = f.read()
logging.info('open image in file: %s', image_id)
else:
logging.info('get origin cache: %s',gen_cache_key(image_id))
need_resize, need_save, need_rotate, need_filter, need_reduce, need_watermark = False, False, False, False, False, False
if h or w or ratio or a or filter or ext or quality or watermark:
imgfile = io.BytesIO(data)
imgfile.seek(0)
im = Image.open(imgfile)
fmt = im.format
if a: need_rotate = True
if filter: need_filter = True
if h or w: need_resize = True
if ratio:
ratio = int(ratio)
need_reduce = True
if watermark: need_watermark=True
if need_resize or need_rotate or need_filter or ext != fmt or quality or need_watermark: need_save = True
if need_resize and need_reduce:
abort(400, "invalid request! can't reduce and resize a image in the same time!")
if need_resize:
if s == 'fill':
if not h or not w:
abort(400, 'fill option needs 2 arguments!')
im = fill(im, w, h)
logging.info('[fill] (%s) width: %s height: %s', image_id,
w, h)
elif s == 'fit':
if not h or not w:
abort(400, 'fit option needs 2 arguments!')
im = fit(im, w, h)
logging.info('[fit] (%s) width: %s height: %s', image_id,
w, h)
else:
im = resize_image(im, w, h)
logging.info('resize (%s) width: %s height: %s', image_id,
w, h)
if need_reduce:
im = reduce_image(im, ratio)
logging.info('reduce (%s) in %d%% ', image_id, ratio)
if need_rotate:
im = im.rotate(a)
logging.info('rotate (%s) angle: %s', image_id, a)
if need_filter:
if filter == 'b':
im = im.convert('1')
logging.info('covert (%s) to black', image_id)
elif filter == 'bl':
im = im.filter(ImageFilter.BLUR)
logging.info('filter (%s) with BLUR ', image_id)
else:
abort(400, 'invalid filter!')
if need_watermark:
if watermark_text:
print('watermark_text:',watermark_text)
# watermark_im = text2watermark(watermark_text)
watermark_im = text2watermark2(watermark_text)
logging.info('defined watermark (%s)', image_id)
else:
watermark_im = Image.open(WATER_MARK_DEFAULT)
logging.info('default watermark (%s)', image_id)
im = watermark_image(im, watermark_im, position)
if need_save:
if ext == '': ext = fmt
buf = io.BytesIO()
if quality:
im.save(buf,ext,quality=int(quality))
logging.info('save (%s) as %s, quality:%s', image_id, ext, quality)
else:
im.save(buf, ext)
logging.info('save (%s) as %s', image_id, ext)
data = buf.getvalue()
mtype = 'image/' + ext
else:
mtype = magic.from_buffer(data, mime=True)
logging.info('get origin image (%s)', image_id)
cache.set(cache_id, data)
cache.expire(cache_id, EXPIRE_TIME)
d = cache.get(cache_id)
if d != None:
logging.info('set cache: %s', cache_id)
else:
logging.info('get cache: %s', cache_id)
mtype = magic.from_buffer(data, mime=True)
res = HTTPResponse(body=data)
res.set_header('Content-Type', mtype)
return res