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
|
|
|
new_width, new_height, new_adjust, process_type = self.determine_new_options(output_options, new_width, |
69
|
|
|
new_height, process_type, |
70
|
|
|
new_adjust) |
71
|
|
|
if process_type == 't': |
72
|
|
|
output = self.return_thumbnail(new_adjust, new_width, new_height, original_width, original_height, size, |
73
|
|
|
image, output_file) |
74
|
|
|
elif process_type == 'c': |
75
|
|
|
output = self.return_crop(image, output_file, original_width, original_height, new_width, new_height) |
76
|
|
|
self.set_header('Content-type', 'image/'+image.format) |
77
|
|
|
else: |
78
|
|
|
self.set_status(404, 'Not Found') |
79
|
|
|
output = "404/rcdn o: ("+options.originals_path+" --- "+original_slug+")" + original_file + " n:" + \ |
80
|
|
|
output_file + " op:" + " ".join(output_options) |
81
|
|
|
self.write(output) |
82
|
|
|
|
83
|
|
|
@staticmethod |
84
|
|
|
def determine_new_options(output_options, new_width, new_height, process_type, new_adjust): |
85
|
|
|
for item in output_options: |
86
|
|
|
if item.startswith('w_'): |
87
|
|
|
new_width = int(item.replace('w_', '')) |
88
|
|
|
if item.startswith('h_'): |
89
|
|
|
new_height = int(item.replace('h_', '')) |
90
|
|
|
if item.startswith('crop'): |
91
|
|
|
process_type = 'c' |
92
|
|
|
if item.startswith('a_'): |
93
|
|
|
new_adjust = item.replace('a_', '') |
94
|
|
|
return new_width, new_height, new_adjust, process_type |
95
|
|
|
|
96
|
|
|
@staticmethod |
97
|
|
|
def determine_box(original_width, original_height, new_width, new_height, image): |
98
|
|
|
if original_width >= original_height: |
99
|
|
|
new_width_tmp = new_height * original_width / original_height |
100
|
|
|
image.thumbnail((new_width_tmp, new_height), Image.ANTIALIAS) |
101
|
|
|
left = int((new_width_tmp - new_width) / 2) |
102
|
|
|
right = int((new_width_tmp + new_width) / 2) |
103
|
|
|
return left, 0, right, new_height |
104
|
|
|
new_height_tmp = new_width * original_height / original_width |
105
|
|
|
image.thumbnail((new_width, new_height_tmp), Image.ANTIALIAS) |
106
|
|
|
top = int((new_height_tmp - new_height) / 2) |
107
|
|
|
bottom = int((new_height_tmp + new_height) / 2) |
108
|
|
|
return 0, top, new_width, bottom |
109
|
|
|
|
110
|
|
|
@staticmethod |
111
|
|
|
def return_thumbnail(new_adjust, new_width, new_height, original_width, original_height, size, image, output_file): |
112
|
|
|
o = BytesIO() |
113
|
|
|
if new_adjust == 'w': |
114
|
|
|
new_height = new_width * original_height / original_width |
115
|
|
|
elif new_adjust == 'h': |
116
|
|
|
new_width = new_height * original_width / original_height |
117
|
|
|
size.append(new_width) |
118
|
|
|
size.append(new_height) |
119
|
|
|
image.thumbnail(size, Image.ANTIALIAS) |
120
|
|
|
image.save(output_file, image.format, quality=90) |
121
|
|
|
image.save(o, image.format, quality=90) |
122
|
|
|
return o.getvalue() |
123
|
|
|
|
124
|
|
|
def return_crop(self, image, output_file, original_width, original_height, new_width, new_height): |
125
|
|
|
o = BytesIO() |
126
|
|
|
box = self.determine_box(original_width, original_height, new_width, new_height, image) |
127
|
|
|
new_image = image.crop(box) |
128
|
|
|
new_image.load() |
129
|
|
|
new_image.save(output_file, image.format, quality=90) |
130
|
|
|
new_image.save(o, image.format, quality=90) |
131
|
|
|
return o.getvalue() |
132
|
|
|
|
133
|
|
|
|
134
|
|
|
def main(): |
135
|
|
|
tornado.options.parse_command_line() |
136
|
|
|
application = tornado.web.Application([ |
137
|
|
|
(r"/", MainHandler), |
138
|
|
|
(r"/bucket/(.*)", BucketHandler) |
139
|
|
|
]) |
140
|
|
|
http_server = tornado.httpserver.HTTPServer(application) |
141
|
|
|
http_server.listen(options.port) |
142
|
|
|
tornado.ioloop.IOLoop.current().start() |
143
|
|
|
|
144
|
|
|
if __name__ == "__main__": |
145
|
|
|
main() |
146
|
|
|
|