1
|
1 |
|
import logging |
2
|
|
|
|
3
|
1 |
|
from flask import Blueprint, current_app, request, redirect |
4
|
1 |
|
from webargs import fields, flaskparser |
5
|
|
|
|
6
|
1 |
|
from .. import domain |
7
|
|
|
|
8
|
1 |
|
from ._cache import Cache |
9
|
1 |
|
from ._utils import route, track, display |
10
|
|
|
|
11
|
|
|
|
12
|
1 |
|
blueprint = Blueprint('image', __name__) |
13
|
1 |
|
log = logging.getLogger(__name__) |
14
|
1 |
|
cache_filtered = Cache() |
15
|
1 |
|
cache_unfiltered = Cache(filtered=False) |
16
|
|
|
|
17
|
1 |
|
OPTIONS = { |
18
|
|
|
'alt': fields.Str(missing=None), |
19
|
|
|
'font': fields.Str(missing=None), |
20
|
|
|
'preview': fields.Bool(missing=False), |
21
|
|
|
'share': fields.Bool(missing=False), |
22
|
|
|
'width': fields.Int(missing=None), |
23
|
|
|
'height': fields.Int(missing=None), |
24
|
|
|
'watermark': fields.Str(missing=None), |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
|
28
|
1 |
|
@blueprint.route("/latest.jpg") |
29
|
1 |
|
@blueprint.route("/latest<int:index>.jpg") |
30
|
1 |
|
@flaskparser.use_kwargs({'filtered': fields.Bool(missing=True)}) |
31
|
1 |
|
def get_latest(index=1, filtered=True): |
32
|
1 |
|
cache = cache_filtered if filtered else cache_unfiltered |
33
|
1 |
|
kwargs = cache.get(index - 1) |
34
|
|
|
|
35
|
1 |
|
if kwargs: |
36
|
1 |
|
kwargs['preview'] = True |
37
|
|
|
else: |
38
|
1 |
|
kwargs['key'] = 'custom' |
39
|
1 |
|
kwargs['path'] = "your_meme/goes_here" |
40
|
1 |
|
kwargs['alt'] = "https://raw.githubusercontent.com/jacebrowning/memegen/master/memegen/static/images/missing.png" |
41
|
|
|
|
42
|
1 |
|
return redirect(route('.get', _external=True, **kwargs)) |
43
|
|
|
|
44
|
|
|
|
45
|
1 |
|
@blueprint.route("/<key>.jpg") |
46
|
1 |
|
@flaskparser.use_kwargs(OPTIONS) |
47
|
|
|
def get_without_text(key, **options): |
48
|
1 |
|
options.pop('preview') |
49
|
1 |
|
options.pop('share') |
50
|
|
|
|
51
|
1 |
|
template = current_app.template_service.find(key) |
52
|
1 |
|
text = domain.Text(template.default_path) |
53
|
|
|
|
54
|
1 |
|
return redirect(route('.get', key=key, path=text.path, **options)) |
55
|
|
|
|
56
|
|
|
|
57
|
1 |
|
@blueprint.route("/<key>.jpeg") |
58
|
|
|
def get_without_text_jpeg(key): |
59
|
1 |
|
return redirect(route('.get_without_text', key=key)) |
60
|
|
|
|
61
|
|
|
|
62
|
1 |
|
@blueprint.route("/<key>/<path:path>.jpg", endpoint='get') |
63
|
1 |
|
@flaskparser.use_kwargs(OPTIONS) |
64
|
|
|
def get_with_text(key, path, alt, font, watermark, preview, share, **size): |
65
|
1 |
|
assert len(size) == 2 |
66
|
|
|
options = dict(key=key, path=path, |
67
|
1 |
|
alt=alt, font=font, watermark=watermark, **size) |
68
|
1 |
|
if preview: |
69
|
1 |
|
options['preview'] = True |
70
|
|
|
if share: |
71
|
|
|
options['share'] = True |
72
|
1 |
|
|
73
|
1 |
|
text = domain.Text(path) |
74
|
|
|
fontfile = current_app.font_service.find(font) |
75
|
1 |
|
|
76
|
1 |
|
template = current_app.template_service.find(key, allow_missing=True) |
77
|
1 |
|
if template.key != key: |
78
|
1 |
|
options['key'] = template.key |
79
|
|
|
return redirect(route('.get', **options)) |
80
|
1 |
|
|
81
|
1 |
|
if alt and template.path == template.get_path(alt): |
82
|
1 |
|
options.pop('alt') |
83
|
|
|
return redirect(route('.get', **options)) |
84
|
1 |
|
|
85
|
1 |
|
if path != text.path: |
86
|
1 |
|
options['path'] = text.path |
87
|
|
|
return redirect(route('.get', **options)) |
88
|
1 |
|
|
89
|
1 |
|
if font and not fontfile: |
90
|
1 |
|
options.pop('font') |
91
|
|
|
return redirect(route('.get', **options)) |
92
|
1 |
|
|
93
|
1 |
|
watermark, valid = _get_watermark(request, watermark) |
94
|
1 |
|
if not valid: |
95
|
1 |
|
options.pop('watermark') |
96
|
|
|
return redirect(route('.get', **options)) |
97
|
1 |
|
|
98
|
|
|
image = current_app.image_service.create( |
99
|
|
|
template, text, |
100
|
|
|
style=alt, font=fontfile, size=size, watermark=watermark, |
101
|
|
|
) |
102
|
1 |
|
|
103
|
1 |
|
if not preview: |
104
|
1 |
|
cache_filtered.add(key=key, path=path, alt=alt, font=font) |
105
|
1 |
|
cache_unfiltered.add(key=key, path=path, alt=alt, font=font) |
106
|
|
|
track(image.text) |
107
|
1 |
|
|
108
|
|
|
return display(image.text, image.path, share=share) |
109
|
|
|
|
110
|
1 |
|
|
111
|
|
|
@blueprint.route("/<key>/<path:path>.jpeg") |
112
|
1 |
|
def get_with_text_jpeg(key, path): |
113
|
|
|
return redirect(route('.get', key=key, path=path)) |
114
|
|
|
|
115
|
1 |
|
|
116
|
|
|
@blueprint.route("/_<code>.jpg") |
117
|
|
|
@flaskparser.use_kwargs(OPTIONS) |
118
|
1 |
|
def get_encoded(code, alt, font, watermark, preview, share, **size): |
119
|
1 |
|
assert len(size) == 2 |
120
|
1 |
|
options = dict(code=code, font=font, watermark=watermark, **size) |
121
|
1 |
|
if share: |
122
|
|
|
options['share'] = True |
123
|
1 |
|
|
124
|
|
|
if alt or preview: |
125
|
1 |
|
return redirect(route('.get_encoded', **options)) |
126
|
|
|
|
127
|
|
|
key, path = current_app.link_service.decode(code) |
128
|
1 |
|
template = current_app.template_service.find(key) |
129
|
1 |
|
text = domain.Text(path) |
130
|
1 |
|
fontfile = current_app.font_service.find(font) |
131
|
1 |
|
|
132
|
|
|
if font and not fontfile: |
133
|
1 |
|
options.pop('font') |
134
|
1 |
|
return redirect(route('.get_encoded', **options)) |
135
|
1 |
|
|
136
|
1 |
|
watermark, valid = _get_watermark(request, watermark) |
137
|
1 |
|
if not valid: |
138
|
1 |
|
options.pop('watermark') |
139
|
|
|
return redirect(route('.get_encoded', **options)) |
140
|
|
|
|
141
|
|
|
image = current_app.image_service.create( |
142
|
1 |
|
template, text, font=fontfile, size=size, watermark=watermark, |
143
|
1 |
|
) |
144
|
1 |
|
|
145
|
|
|
track(image.text) |
146
|
1 |
|
|
147
|
1 |
|
return display(image.text, image.path, share=share) |
148
|
1 |
|
|
149
|
|
|
|
150
|
1 |
|
def _get_watermark(_request, watermark): |
151
|
1 |
|
referrer = _request.environ.get('HTTP_REFERER', "").lower() |
152
|
1 |
|
agent = _request.environ.get('HTTP_USER_AGENT', "").lower() |
153
|
|
|
log.debug("Referrer: %r, Agent: %r", referrer, agent) |
154
|
|
|
|
155
|
|
|
if watermark == 'none': |
156
|
|
|
for option in current_app.config['WATERMARK_OPTIONS']: |
157
|
|
|
for identity in (referrer, agent): |
158
|
|
|
if option in identity: |
159
|
|
|
log.debug("Watermark disabled (%r in %r)", option, identity) |
160
|
|
|
return None, True |
161
|
|
|
log.warning("Request does not support unmarked images") |
162
|
|
|
return None, False |
163
|
|
|
|
164
|
|
|
if watermark and watermark not in current_app.config['WATERMARK_OPTIONS']: |
165
|
|
|
log.warning("Unsupported custom watermark: %r", watermark) |
166
|
|
|
return watermark, False |
167
|
|
|
|
168
|
|
|
if watermark: |
169
|
|
|
log.debug("Using custom watermark: %r", watermark) |
170
|
|
|
return watermark, True |
171
|
|
|
|
172
|
|
|
default = current_app.config['WATERMARK_OPTIONS'][0] |
173
|
|
|
log.debug("Using default watermark: %r", default) |
174
|
|
|
return default, True |
175
|
|
|
|