This repository was archived by the owner on Jul 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrcdn.py
More file actions
145 lines (128 loc) · 6.05 KB
/
Copy pathrcdn.py
File metadata and controls
145 lines (128 loc) · 6.05 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
#!/usr/bin/env python3.5
# -*- coding: utf-8 -*-
import os
import ujson as json
import tornado.httpserver
import tornado.ioloop
import tornado.options
import tornado.web
from tornado.options import define, options
from PIL import Image
from io import BytesIO
define("port", default=8888, help="run on the given port", type=int)
define("originals_path", default='', help="full path to original images folder", type=str)
define("buckets_path", default='', help="full path to buckets folder", type=str)
class BaseHandler(tornado.web.RequestHandler):
def set_default_headers(self):
self.set_header('Access-Control-Allow-Origin', '*')
self.set_header('Access-Control-Allow-Methods', 'GET')
self.set_header('Access-Control-Max-Age', 1000)
self.set_header('Access-Control-Allow-Headers', '*')
self.set_header('Content-type', 'application/json')
self.set_header('Server', 'reformo/rcdn')
self.set_status(200, 'Success')
class ReturnImageHandler(tornado.web.RequestHandler):
def set_default_headers(self):
self.set_header('Access-Control-Allow-Origin', '*')
self.set_header('Access-Control-Allow-Methods', 'GET')
self.set_header('Access-Control-Max-Age', 1000)
self.set_header('Access-Control-Allow-Headers', '*')
self.set_header('Server', 'reformo/rcdn')
self.set_status(200, 'Success')
class MainHandler(BaseHandler):
def get(self):
data = {"status": 200, "message": "OK"}
output = json.dumps(data, sort_keys=True, indent=4)
self.write(output)
class BucketHandler(ReturnImageHandler):
def get(self, slug):
slug_dir = os.path.dirname(slug)
output_options_string = os.path.basename(slug_dir)
output_options = output_options_string.split(",")
original_slug = slug.replace(output_options_string, '')
original_file = options.originals_path + "/" + original_slug
output_file = options.buckets_path + "/" + slug
output_dir = os.path.dirname(output_file)
output = None
size = []
new_width = 50
new_height = 50
process_type = 't'
new_adjust = 'w'
if os.path.isfile(original_file):
if not os.path.exists(output_dir):
os.makedirs(output_dir)
image = Image.open(original_file)
image.load()
original_width, original_height = image.size
new_width, new_height, new_adjust, process_type = self.determine_new_options(output_options, new_width,
new_height, process_type,
new_adjust)
if process_type == 't':
output = self.return_thumbnail(new_adjust, new_width, new_height, original_width, original_height, size,
image, output_file)
elif process_type == 'c':
output = self.return_crop(image, output_file, original_width, original_height, new_width, new_height)
self.set_header('Content-type', 'image/'+image.format)
else:
self.set_status(404, 'Not Found')
output = "404/rcdn o: ("+options.originals_path+" --- "+original_slug+")" + original_file + " n:" + \
output_file + " op:" + " ".join(output_options)
self.write(output)
@staticmethod
def determine_new_options(output_options, new_width, new_height, process_type, new_adjust):
for item in output_options:
if item.startswith('w_'):
new_width = int(item.replace('w_', ''))
if item.startswith('h_'):
new_height = int(item.replace('h_', ''))
if item.startswith('crop'):
process_type = 'c'
if item.startswith('a_'):
new_adjust = item.replace('a_', '')
return new_width, new_height, new_adjust, process_type
@staticmethod
def determine_box(original_width, original_height, new_width, new_height, image):
if original_width >= original_height:
new_width_tmp = new_height * original_width / original_height
image.thumbnail((new_width_tmp, new_height), Image.ANTIALIAS)
left = int((new_width_tmp - new_width) / 2)
right = int((new_width_tmp + new_width) / 2)
return left, 0, right, new_height
new_height_tmp = new_width * original_height / original_width
image.thumbnail((new_width, new_height_tmp), Image.ANTIALIAS)
top = int((new_height_tmp - new_height) / 2)
bottom = int((new_height_tmp + new_height) / 2)
return 0, top, new_width, bottom
@staticmethod
def return_thumbnail(new_adjust, new_width, new_height, original_width, original_height, size, image, output_file):
o = BytesIO()
if new_adjust == 'w':
new_height = new_width * original_height / original_width
elif new_adjust == 'h':
new_width = new_height * original_width / original_height
size.append(new_width)
size.append(new_height)
image.thumbnail(size, Image.ANTIALIAS)
image.save(output_file, image.format, quality=90)
image.save(o, image.format, quality=90)
return o.getvalue()
def return_crop(self, image, output_file, original_width, original_height, new_width, new_height):
o = BytesIO()
box = self.determine_box(original_width, original_height, new_width, new_height, image)
new_image = image.crop(box)
new_image.load()
new_image.save(output_file, image.format, quality=90)
new_image.save(o, image.format, quality=90)
return o.getvalue()
def main():
tornado.options.parse_command_line()
application = tornado.web.Application([
(r"/", MainHandler),
(r"/bucket/(.*)", BucketHandler)
])
http_server = tornado.httpserver.HTTPServer(application)
http_server.listen(options.port)
tornado.ioloop.IOLoop.current().start()
if __name__ == "__main__":
main()