|
1
|
|
|
#!/usr/bin/env python3.5 |
|
2
|
|
|
# -*- coding: utf-8 -*- |
|
3
|
|
|
|
|
4
|
|
|
|
|
5
|
|
|
import os |
|
6
|
|
|
import ujson as json |
|
7
|
|
|
import tornado.httpserver |
|
8
|
|
|
import tornado.ioloop |
|
9
|
|
|
import tornado.options |
|
10
|
|
|
import tornado.web |
|
11
|
|
|
from tornado.options import define, options |
|
12
|
|
|
from PIL import Image |
|
13
|
|
|
from io import BytesIO |
|
14
|
|
|
define("port", default=8888, help="run on the given port", type=int) |
|
15
|
|
|
define("originals_path", default='', help="full path to original images folder", type=str) |
|
16
|
|
|
define("buckets_path", default='', help="full path to buckets folder", type=str) |
|
17
|
|
|
|
|
18
|
|
|
|
|
19
|
|
|
class BaseHandler(tornado.web.RequestHandler): |
|
20
|
|
|
def set_default_headers(self): |
|
21
|
|
|
self.set_header('Access-Control-Allow-Origin', '*') |
|
22
|
|
|
self.set_header('Access-Control-Allow-Methods', 'GET') |
|
23
|
|
|
self.set_header('Access-Control-Max-Age', 1000) |
|
24
|
|
|
self.set_header('Access-Control-Allow-Headers', '*') |
|
25
|
|
|
self.set_header('Content-type', 'application/json') |
|
26
|
|
|
self.set_header('Server', 'reformo/rcdn') |
|
27
|
|
|
self.set_status(200, 'Success') |
|
28
|
|
|
|
|
29
|
|
|
|
|
30
|
|
|
class ReturnImageHandler(tornado.web.RequestHandler): |
|
31
|
|
|
def set_default_headers(self): |
|
32
|
|
|
self.set_header('Access-Control-Allow-Origin', '*') |
|
33
|
|
|
self.set_header('Access-Control-Allow-Methods', 'GET') |
|
34
|
|
|
self.set_header('Access-Control-Max-Age', 1000) |
|
35
|
|
|
self.set_header('Access-Control-Allow-Headers', '*') |
|
36
|
|
|
self.set_header('Server', 'reformo/rcdn') |
|
37
|
|
|
self.set_status(200, 'Success') |
|
38
|
|
|
|
|
39
|
|
|
|
|
40
|
|
|
class MainHandler(BaseHandler): |
|
41
|
|
|
def get(self): |
|
42
|
|
|
data = {"status": 200, "message": "OK"} |
|
43
|
|
|
output = json.dumps(data, sort_keys=True, indent=4) |
|
44
|
|
|
self.write(output) |
|
45
|
|
|
|
|
46
|
|
|
|
|
47
|
|
|
class BucketHandler(ReturnImageHandler): |
|
48
|
|
|
def get(self, slug): |
|
49
|
|
|
slug_dir = os.path.dirname(slug) |
|
50
|
|
|
output_options_string = os.path.basename(slug_dir) |
|
51
|
|
|
output_options = output_options_string.split(",") |
|
52
|
|
|
original_slug = slug.replace(output_options_string, '') |
|
53
|
|
|
original_file = options.originals_path + "/" + original_slug |
|
54
|
|
|
output_file = options.buckets_path + "/" + slug |
|
55
|
|
|
output_dir = os.path.dirname(output_file) |
|
56
|
|
|
output = None |
|
57
|
|
|
size = [] |
|
58
|
|
|
new_width = 50 |
|
59
|
|
|
new_height = 50 |
|
60
|
|
|
process_type = 't' |
|
61
|
|
|
new_adjust = 'w' |
|
62
|
|
|
if os.path.isfile(original_file): |
|
63
|
|
|
if not os.path.exists(output_dir): |
|
64
|
|
|
os.makedirs(output_dir) |
|
65
|
|
|
image = Image.open(original_file) |
|
66
|
|
|
image.load() |
|
67
|
|
|
original_width, original_height = image.size |
|
68
|
|
|
o = BytesIO() |
|
69
|
|
|
for item in output_options: |
|
70
|
|
|
if item.startswith('w_'): |
|
71
|
|
|
new_width = int(item.replace('w_', '')) |
|
72
|
|
|
if item.startswith('h_'): |
|
73
|
|
|
new_height = int(item.replace('h_', '')) |
|
74
|
|
|
if item.startswith('crop'): |
|
75
|
|
|
process_type = 'c' |
|
76
|
|
|
if item.startswith('a_'): |
|
77
|
|
|
new_adjust = item.replace('a_', '') |
|
78
|
|
|
if process_type == 't': |
|
79
|
|
|
if new_adjust == 'w': |
|
80
|
|
|
new_height = new_width*original_height/original_width |
|
81
|
|
|
elif new_adjust == 'h': |
|
82
|
|
|
new_width = new_height*original_width/original_height |
|
83
|
|
|
size.append(new_width) |
|
84
|
|
|
size.append(new_height) |
|
85
|
|
|
image.thumbnail(size, Image.ANTIALIAS) |
|
86
|
|
|
image.save(output_file, image.format, quality=90) |
|
87
|
|
|
image.save(o, image.format, quality=90) |
|
88
|
|
|
output = o.getvalue() |
|
89
|
|
|
elif process_type == 'c': |
|
90
|
|
|
box = self.innerDetermineBox() |
|
91
|
|
|
new_image = image.crop(box) |
|
92
|
|
|
new_image.load() |
|
93
|
|
|
new_image.save(output_file, image.format, quality=90) |
|
94
|
|
|
new_image.save(o, image.format, quality=90) |
|
95
|
|
|
output = o.getvalue() |
|
96
|
|
|
self.set_header('Content-type', 'image/'+image.format) |
|
97
|
|
|
self.set_status(404, 'Not Found') |
|
98
|
|
|
output = "404/rcdn o: ("+options.originals_path+" --- "+original_slug+")" + original_file + " n:" + \ |
|
99
|
|
|
output_file + " op:" + " ".join(output_options) |
|
100
|
|
|
self.write(output) |
|
101
|
|
|
|
|
102
|
|
|
def innerDetermineBox(self, original_width, original_height, new_width, new_height, image): |
|
103
|
|
|
if original_width >= original_height: |
|
104
|
|
|
new_width_tmp = new_height * original_width / original_height |
|
105
|
|
|
image.thumbnail((new_width_tmp, new_height), Image.ANTIALIAS) |
|
106
|
|
|
left = int((new_width_tmp - new_width) / 2) |
|
107
|
|
|
right = int((new_width_tmp + new_width) / 2) |
|
108
|
|
|
return (left, 0, right, new_height) |
|
109
|
|
|
|
|
110
|
|
|
new_height_tmp = new_width * original_height / original_width |
|
111
|
|
|
image.thumbnail((new_width, new_height_tmp), Image.ANTIALIAS) |
|
112
|
|
|
top = int((new_height_tmp - new_height) / 2) |
|
113
|
|
|
bottom = int((new_height_tmp + new_height) / 2) |
|
114
|
|
|
return (0, top, new_width, bottom) |
|
115
|
|
|
|
|
116
|
|
|
def main(): |
|
117
|
|
|
tornado.options.parse_command_line() |
|
118
|
|
|
application = tornado.web.Application([ |
|
119
|
|
|
(r"/", MainHandler), |
|
120
|
|
|
(r"/bucket/(.*)", BucketHandler) |
|
121
|
|
|
]) |
|
122
|
|
|
http_server = tornado.httpserver.HTTPServer(application) |
|
123
|
|
|
http_server.listen(options.port) |
|
124
|
|
|
tornado.ioloop.IOLoop.current().start() |
|
125
|
|
|
|
|
126
|
|
|
if __name__ == "__main__": |
|
127
|
|
|
main() |
|
128
|
|
|
|