-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathimageproxy.py
40 lines (31 loc) · 1 KB
/
imageproxy.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
from sanic import Sanic
from sanic import response
from urllib.request import urlopen
from cooltools import memcached_bin
from wand.image import Image
proxy_app = Sanic()
def _get_image(res,url):
if res != '0x0':
w,h = map(lambda x: int(x), res.split('x'))
req = urlopen(url)
retval = None
fmt_str = 'jpeg'
try:
with Image(file=req) as img:
if res != '0x0':
if (w != img.width) or (h != img.height):
img.resize(w,h)
retval = img.make_blob()
fmt_str = img.format
finally:
req.close()
return retval,req.getheader('content-type','image/%s' % fmt_str)
get_image = memcached_bin(_get_image)
@proxy_app.middleware('request')
async def image(request):
res = request.path.split('/')[1]
url = request.path[len(res)+2:]
img_data,img_type = get_image(res,url)
return response.raw(img_data,content_type=img_type)
if __name__ == "__main__":
proxy_app.run(host="0.0.0.0", port=8020)