forked from vas3k/i.vas3k.ru
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
213 lines (166 loc) · 7.56 KB
/
app.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
import io
import sys
import json
import logging
from mimetypes import guess_type
from PIL import Image
from flask import Flask, redirect, request, render_template, Response
import psycopg2
import psycopg2.extras
from helpers import *
from settings import *
app = Flask(__name__)
app.debug = True
log = logging.getLogger(__name__)
logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)
def x_accel_response(filepath):
# nginx'овый internal redirect
# очень магическая фигня, которая отдает статику nginx'ом, а не python'ом
# описание тут: http://kovyrin.net/2006/11/01/nginx-x-accel-redirect-php-rails/
redirect_response = Response(mimetype=guess_type(filepath)[0])
redirect_response.headers["X-Accel-Redirect"] = filepath
return redirect_response
@app.route("/")
def index():
return render_template("index.html")
@app.route("/upload/", methods=["POST"])
def upload():
files = request.files.getlist("media") or request.files.getlist("image")
data = request.form.get("media") or request.form.get("image")
images = []
if files:
for image_file in files:
image_extension = image_file.filename[image_file.filename.rfind(".") + 1:].lower()
if image_extension not in ALLOWED_EXTENSIONS:
return "{} is not allowed".format(image_extension)
data = image_file.read()
images.append((data, image_extension))
elif data:
data, image_extension = convert_param_to_data(data)
if image_extension not in ALLOWED_EXTENSIONS:
return "{} is not allowed".format(image_extension)
images.append((data, image_extension))
db = psycopg2.connect(PSYCOPG_CONNECTION_STRING)
cursor = db.cursor(cursor_factory=psycopg2.extras.DictCursor)
uploaded_image_names = []
for image in images:
data, image_extension = image
cursor.execute("insert into images values (default, null, now()) returning id")
file_id = cursor.fetchone()[0]
image_code = base36_encode(file_id)
image_path = os.path.join(FULL_IMAGE_FILE_PATH, file_path("{}.{}".format(image_code, image_extension)))
image_dir = image_path[:image_path.rfind("/") + 1]
if not os.path.exists(image_dir):
os.makedirs(image_dir, mode=0o777)
try:
image_file = io.BytesIO(data)
image = Image.open(image_file)
except IOError as ex:
cursor.execute("delete from images where id = %s", [file_id])
db.commit()
cursor.close()
return "Image upload error (maybe not image?): {}".format(ex)
image_width = float(image.size[0])
image_height = float(image.size[1])
orig_save_size = get_fit_image_size(image_width, image_height, ORIGINAL_MAX_IMAGE_LENGTH)
image.thumbnail(orig_save_size, Image.ANTIALIAS)
try:
image = apply_rotation_by_exif(image)
except (IOError, KeyError, AttributeError) as ex:
log.error("Auto-rotation error: {}".format(ex))
image.save(image_path, quality=IMAGE_QUALITY)
image_name = "{}.{}".format(image_code, image_extension)
cursor.execute("update images set image = %s, file = %s where id = %s", [image_name, image_path, file_id])
db.commit()
uploaded_image_names.append(image_name)
cursor.close()
db.close()
nojson = request.form.get("nojson")
if nojson:
return redirect("{}/meta/{}".format(BASE_URI, "+".join(uploaded_image_names)))
else:
return json.dumps({
"uploaded": [
"{}/{}".format(BASE_URI, image_name) for image_name in uploaded_image_names
]
})
@app.route("/<filename>", methods=["GET"])
def common_image(filename):
return fit_image(COMMON_IMAGE_LENGTH, filename)
@app.route("/full/<filename>", methods=["GET"])
def full_image(filename):
return x_accel_response("/images/max/{}".format(file_path(filename)))
@app.route("/meta/<filenames>", methods=["GET"])
def meta_image(filenames):
filenames = filenames.split("+")
return render_template(
"meta.html",
images=[
(
"{}/{}".format(BASE_URI, filename),
"{}/full/{}".format(BASE_URI, filename)
) for filename in filenames
]
)
@app.route("/<int(min=50,max=2000):max_length>/<filename>", methods=["GET"])
def fit_image(max_length, filename):
ok_filepath = file_path(filename)
filepath = os.path.join(IMAGES_FILE_PATH, "resize/{}/{}".format(max_length, ok_filepath))
if not os.path.exists(filepath):
try:
image = Image.open(os.path.join(FULL_IMAGE_FILE_PATH, ok_filepath))
except IOError:
return "Not found", 404
image_dir = filepath[:filepath.rfind("/") + 1]
if not os.path.exists(image_dir):
os.makedirs(image_dir, mode=0o777)
image_width = float(image.size[0])
image_height = float(image.size[1])
if image_width > max_length or image_height > max_length:
new_width, new_height = get_fit_image_size(image_width, image_height, max_length)
image.thumbnail((new_width, new_height), Image.ANTIALIAS)
image.save(filepath, quality=IMAGE_QUALITY)
return x_accel_response("/images/resize/{}/{}".format(max_length, ok_filepath))
@app.route("/square/<int(min=50,max=2000):max_length>/<filename>", methods=["GET"])
def square_image(max_length, filename):
ok_filepath = file_path(filename)
filepath = os.path.join(IMAGES_FILE_PATH, "square/{}/{}".format(max_length, ok_filepath))
if not os.path.exists(filepath):
try:
image = Image.open(os.path.join(FULL_IMAGE_FILE_PATH, ok_filepath))
except IOError:
return "Not found", 404
image_dir = filepath[:filepath.rfind("/") + 1]
if not os.path.exists(image_dir):
os.makedirs(image_dir, mode=0o777)
image_width = float(image.size[0])
image_height = float(image.size[1])
image_square = int(min(image_width, image_height))
crop_coordinates_x = int(image_width / 2 - image_square / 2)
crop_coordinates_y = int(image_height / 2 - image_square / 2)
image = image.crop((crop_coordinates_x, crop_coordinates_y, crop_coordinates_x + image_square,
crop_coordinates_y + image_square))
image.thumbnail((max_length, max_length), Image.ANTIALIAS)
image.save(filepath, quality=IMAGE_QUALITY)
return x_accel_response("/images/square/{}/{}".format(max_length, ok_filepath))
@app.route("/width/<int(min=50,max=2000):max_length>/<filename>", methods=["GET"])
def width_image(max_length, filename):
ok_filepath = file_path(filename)
filepath = os.path.join(IMAGES_FILE_PATH, "width/{}/{}".format(max_length, ok_filepath))
if not os.path.exists(filepath):
try:
image = Image.open(os.path.join(FULL_IMAGE_FILE_PATH, ok_filepath))
except IOError:
return "Not found", 404
image_dir = filepath[:filepath.rfind("/") + 1]
if not os.path.exists(image_dir):
os.makedirs(image_dir, mode=0o777)
image_width = float(image.size[0])
image_height = float(image.size[1])
new_width = int(max_length)
new_height = int(new_width / image_width * image_height)
image.thumbnail((new_width, new_height), Image.ANTIALIAS)
image.save(filepath, quality=IMAGE_QUALITY)
return x_accel_response("/images/width/{}/{}".format(max_length, ok_filepath))
if __name__ == '__main__':
app.run()