forked from bboe/flask-image-uploader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
285 lines (248 loc) · 8.57 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
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
275
276
277
278
279
280
281
282
283
284
285
"""An example flask application demonstrating server-sent events."""
from hashlib import sha1
from shutil import rmtree
from stat import S_ISREG, ST_CTIME, ST_MODE
import json
import os
import time
from PIL import Image, ImageFile
from gevent.event import AsyncResult
from gevent.queue import Empty, Queue
from gevent.timeout import Timeout
import flask
DATA_DIR = 'data'
KEEP_ALIVE_DELAY = 25
MAX_IMAGE_SIZE = 800, 600
MAX_IMAGES = 10
MAX_DURATION = 300
APP = flask.Flask(__name__, static_folder=DATA_DIR)
BROADCAST_QUEUE = Queue()
try: # Reset saved files on each start
rmtree(DATA_DIR, True)
os.mkdir(DATA_DIR)
except OSError:
pass
def broadcast(message):
"""Notify all waiting waiting gthreads of message."""
waiting = []
try:
while True:
waiting.append(BROADCAST_QUEUE.get(block=False))
except Empty:
pass
print('Broadcasting {} messages'.format(len(waiting)))
for item in waiting:
item.set(message)
def receive():
"""Generator that yields a message at least every KEEP_ALIVE_DELAY seconds.
yields messages sent by `broadcast`.
"""
now = time.time()
end = now + MAX_DURATION
tmp = None
# Heroku doesn't notify when clients disconnect so we have to impose a
# maximum connection duration.
while now < end:
if not tmp:
tmp = AsyncResult()
BROADCAST_QUEUE.put(tmp)
try:
yield tmp.get(timeout=KEEP_ALIVE_DELAY)
tmp = None
except Timeout:
yield ''
now = time.time()
def safe_addr(ip_addr):
"""Strip off the trailing two octets of the IP address."""
return '.'.join(ip_addr.split('.')[:2] + ['xxx', 'xxx'])
def save_normalized_image(path, data):
"""Generate an RGB thumbnail of the provided image."""
image_parser = ImageFile.Parser()
try:
image_parser.feed(data)
image = image_parser.close()
except IOError:
return False
image.thumbnail(MAX_IMAGE_SIZE, Image.ANTIALIAS)
if image.mode != 'RGB':
image = image.convert('RGB')
image.save(path)
return True
def event_stream(client):
"""Yield messages as they come in."""
force_disconnect = False
try:
for message in receive():
yield 'data: {}\n\n'.format(message)
print('{} force closing stream'.format(client))
force_disconnect = True
finally:
if not force_disconnect:
print('{} disconnected from stream'.format(client))
@APP.route('/post', methods=['POST'])
def post():
"""Handle image uploads."""
sha1sum = sha1(flask.request.data).hexdigest()
target = os.path.join(DATA_DIR, '{}.jpg'.format(sha1sum))
message = json.dumps({'src': target,
'ip_addr': safe_addr(flask.request.access_route[0])})
try:
if save_normalized_image(target, flask.request.data):
broadcast(message) # Notify subscribers of completion
except Exception as exception: # Output errors
return '{}'.format(exception)
return 'success'
@APP.route('/stream')
def stream():
"""Handle long-lived SSE streams."""
return flask.Response(event_stream(flask.request.access_route[0]),
mimetype='text/event-stream')
@APP.route('/')
def home():
"""Provide the primary view along with its javascript."""
# Code adapted from: http://stackoverflow.com/questions/168409/
image_infos = []
for filename in os.listdir(DATA_DIR):
filepath = os.path.join(DATA_DIR, filename)
file_stat = os.stat(filepath)
if S_ISREG(file_stat[ST_MODE]):
image_infos.append((file_stat[ST_CTIME], filepath))
images = []
for i, (_, path) in enumerate(sorted(image_infos, reverse=True)):
if i >= MAX_IMAGES:
os.unlink(path)
continue
images.append('<div><img alt="User uploaded image" src="{}" /></div>'
.format(path))
return """
<!doctype html>
<title>Image Uploader</title>
<meta charset="utf-8" />
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.1/jquery-ui.min.js"></script>
<link rel="stylesheet" href="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.1/themes/vader/jquery-ui.css" />
<style>
body {
max-width: 800px;
margin: auto;
padding: 1em;
background: black;
color: #fff;
font: 16px/1.6 menlo, monospace;
text-align:center;
}
a {
color: #fff;
}
.notice {
font-size: 80%%;
}
#drop {
font-weight: bold;
text-align: center;
padding: 1em 0;
margin: 1em 0;
color: #555;
border: 2px dashed #555;
border-radius: 7px;
cursor: default;
}
#drop.hover {
color: #f00;
border-color: #f00;
border-style: solid;
box-shadow: inset 0 3px 4px #888;
}
</style>
<h3>Image Uploader</h3>
<p>Upload an image for everyone to see. Valid images are pushed to everyone
currently connected, and only the most recent %s images are saved.</p>
<p>The complete source for this Flask web service can be found at:
<a href="https://github.com/bboe/flask-image-uploader">https://github.com/bboe/flask-image-uploader</a></p>
<p class="notice">Disclaimer: The author of this application accepts no responsibility for the
images uploaded to this web service. To discourage the submission of obscene images, IP
addresses with the last two octets hidden will be visibly associated with uploaded images.</p>
<noscript>Note: You must have javascript enabled in order to upload and
dynamically view new images.</noscript>
<fieldset>
<p id="status">Select an image</p>
<div id="progressbar"></div>
<input id="file" type="file" />
<div id="drop">or drop image here</div>
</fieldset>
<h3>Uploaded Images (updated in real-time)</h3>
<div id="images">%s</div>
<script>
function sse() {
var source = new EventSource('/stream');
source.onmessage = function(e) {
if (e.data == '')
return;
var data = $.parseJSON(e.data);
var upload_message = 'Image uploaded by ' + data['ip_addr'];
var image = $('<img>', {alt: upload_message, src: data['src']});
var container = $('<div>').hide();
container.append($('<div>', {text: upload_message}));
container.append(image);
$('#images').prepend(container);
image.load(function(){
container.show('blind', {}, 1000);
});
};
}
function file_select_handler(to_upload) {
var progressbar = $('#progressbar');
var status = $('#status');
var xhr = new XMLHttpRequest();
xhr.upload.addEventListener('loadstart', function(e1){
status.text('uploading image');
progressbar.progressbar({max: e1.total});
});
xhr.upload.addEventListener('progress', function(e1){
if (progressbar.progressbar('option', 'max') == 0)
progressbar.progressbar('option', 'max', e1.total);
progressbar.progressbar('value', e1.loaded);
});
xhr.onreadystatechange = function(e1) {
if (this.readyState == 4) {
if (this.status == 200)
var text = 'upload complete: ' + this.responseText;
else
var text = 'upload failed: code ' + this.status;
status.html(text + '<br/>Select an image');
progressbar.progressbar('destroy');
}
};
xhr.open('POST', '/post', true);
xhr.send(to_upload);
};
function handle_hover(e) {
e.originalEvent.stopPropagation();
e.originalEvent.preventDefault();
e.target.className = (e.type == 'dragleave' || e.type == 'drop') ? '' : 'hover';
}
$('#drop').bind('drop', function(e) {
handle_hover(e);
if (e.originalEvent.dataTransfer.files.length < 1) {
return;
}
file_select_handler(e.originalEvent.dataTransfer.files[0]);
}).bind('dragenter dragleave dragover', handle_hover);
$('#file').change(function(e){
file_select_handler(e.target.files[0]);
e.target.value = '';
});
sse();
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-510348-17']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
""" % (MAX_IMAGES, '\n'.join(images)) # noqa
if __name__ == '__main__':
APP.debug = True
APP.run('0.0.0.0', threaded=True)