-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathstatic_file.py
274 lines (225 loc) · 8.02 KB
/
static_file.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
# -*- coding: utf-8 -*-
"""
static_file
Static File
:copyright: (c) 2013-2014 by Openlabs Technologies & Consulting (P) Limited
:license: BSD, see LICENSE for more details.
"""
import os
from io import BytesIO
from datetime import datetime
from urllib2 import unquote
from PIL import Image
import pytz
from nereid.helpers import send_file
from nereid import url_for, route, abort
from jinja2 import Markup
from werkzeug.utils import secure_filename
from trytond.pool import PoolMeta
from trytond.transaction import Transaction
__all__ = ['NereidStaticFile', 'TransformationCommand']
__metaclass__ = PoolMeta
FILTER_MAP = {
'': Image.NEAREST,
'n': Image.NEAREST,
'b': Image.BILINEAR,
'c': Image.BICUBIC,
'a': Image.ANTIALIAS,
}
class TransformationCommand(object):
"""
>>> c = TransformationCommand()
>>> c.thumbnail(128, 128)
TransformationCommand(['thumbnail,w_128,h_128,m_n'])
>>> c.resize(100, 100)
TransformationCommand(
['thumbnail,w_128,h_128,m_n', 'resize,w_100,h_100,m_n']
)
>>> c
TransformationCommand(
['thumbnail,w_128,h_128,m_n', 'resize,w_100,h_100,m_n']
)
>>> str(c)
'thumbnail,w_128,h_128,m_n/resize,w_100,h_100,m_n'
"""
def __init__(self, commands=None):
if commands is not None:
self.commands = commands[:]
else:
self.commands = []
def __repr__(self):
return u'TransformationCommand(%s)' % self.commands
def __str__(self):
return '/'.join(self.commands)
def __unicode__(self):
return u'/'.join(self.commands)
def thumbnail(self, width, height, mode='n'):
"""
Returns a resize command. To understand more about the arguments see
`pil documentation
<http://www.pythonware.com/library/pil/handbook/image.htm>`_
:param width: Width of the image
:param height: Height og the image
:param mode: Filter to use
* n - NEAREST
* l - BILINEAR
* c - BICUBIC
* a - ANTIALIAS (best quality)
"""
arguments = ['thumbnail']
arguments.append('w_%s' % width)
arguments.append('h_%s' % height)
if mode:
arguments.append('m_%s' % mode)
self.commands.append(','.join(arguments))
return self
def resize(self, width, height, mode='n'):
"""
Returns a resize command. To understand more about the arguments see
`pil documentation
<http://www.pythonware.com/library/pil/handbook/image.htm>`_
:param width: Width of the image
:param height: Height og the image
:param mode: Filter to use
* n - NEAREST
* l - BILINEAR
* c - BICUBIC
* a - ANTIALIAS (best quality)
"""
arguments = ['resize']
arguments.append('w_%s' % width)
arguments.append('h_%s' % height)
if mode:
arguments.append('m_%s' % mode)
self.commands.append(','.join(arguments))
return self
@staticmethod
def parse_command(command):
"""
Parse the given commands to a dictionary of command parameters
:param command: A special command to be parsed
:return: A tuple of the operation to be done and parameters for it
"""
command = unquote(unicode(command))
try:
operation, params = command.split(',', 1)
except ValueError:
abort(404)
return operation, dict(
map(lambda arg: arg.split('_'), params.split(','))
)
class StaticFileTransformationCommand(TransformationCommand):
"""
A helper class which can be chained to build resizable image
urls.
"""
def __init__(self, static_file, extension=None, commands=None):
"""
:param static_file: ID of static_file or Active Record
:param extension: File extension to use
:param commands: A list of commands (optional)
"""
self.static_file = static_file
self.extension = extension or \
os.path.splitext(static_file.name)[1][1:] or 'png'
super(StaticFileTransformationCommand, self).__init__(commands)
def __html__(self):
return Markup(self.url())
def url(self, **kwargs):
"""
Constructs a URL based on the static file and the commands
.. versionchanged::3.2.0.2
Supports keyword arguments passed to the url builder
"""
return url_for(
'nereid.static.file.transform_static_file',
active_id=int(self.static_file), commands=unicode(self),
extension=self.extension, **kwargs
)
class NereidStaticFile:
__name__ = "nereid.static.file"
allowed_operations = ['resize', 'thumbnail']
@staticmethod
def thumbnail(image, w=128, h=128, m='n'):
"""
:param image: Image instance
:param w: width
:param h: height
:param m: mode for the resize operation
* n - NEAREST
* l - BILINEAR
* c - BICUBIC
* a - ANTIALIAS (best quality)
"""
image.thumbnail((int(w), int(h)), FILTER_MAP[m])
return image
@staticmethod
def resize(image, w=128, h=128, m='n'):
"""
:param image: Image instance
:param w: width
:param h: height
:param m: mode for the resize operation
* n - NEAREST
* l - BILINEAR
* c - BICUBIC
* a - ANTIALIAS (best quality)
"""
return image.resize((int(w), int(h)), FILTER_MAP[m])
def _transform_static_file(self, commands, extension, filename):
"""
Transform the static file and send the transformed file
:param commands: A list of commands separated by /
:param extension: The image format to use
:param filename: The file to which the transformed image
needs to be written
"""
# Ugly hack '[:]' to fix issue in python 2.7.3
image_file = Image.open(BytesIO(self.file_binary[:]))
parse_command = TransformationCommand.parse_command
for command in commands.split('/'):
operation, params = parse_command(command)
if operation not in self.allowed_operations:
abort(404)
image_file = getattr(self, operation)(image_file, **params)
image_file.save(filename)
@route('/static-file-transform/<int:active_id>/<path:commands>.<extension>')
def transform_static_file(self, commands, extension):
"""
Transform the static file and send the transformed file
:param commands: A list of commands separated by /
:param extension: The image format to use
"""
tmp_folder = os.path.join(
'/tmp/nereid/', Transaction().cursor.dbname, str(self.id)
)
try:
os.makedirs(tmp_folder)
except OSError, err:
if err.errno == 17:
# directory exists
pass
else:
raise
filename = os.path.join(
tmp_folder, '%s.%s' % (secure_filename(commands), extension)
)
file_date = os.path.exists(filename) and datetime.fromtimestamp(
os.path.getmtime(filename), pytz.timezone('UTC')
)
if not file_date or (
self.write_date and file_date < pytz.UTC.localize(self.write_date)
):
self._transform_static_file(commands, extension, filename)
rv = send_file(filename)
rv.headers['Cache-Control'] = 'public, max-age=%d' % 86400
return rv
def transform_command(self):
"""
Returns a chainable StaticFileTransformationCommand object for this
static file
"""
return StaticFileTransformationCommand(self)
if __name__ == "__main__":
import doctest
doctest.testmod()