#!/usr/bin/env python
import flask
import redis
import os
from hashlib import sha1
from stat import S_ISREG, ST_CTIME, ST_MODE


DATA_DIR = 'data'

app = flask.Flask(__name__, static_folder=DATA_DIR)
red = redis.from_url(os.getenv('REDISTOGO_URL', 'redis://localhost:6379'))


try:
    os.mkdir(DATA_DIR)
except OSError:
    pass


def event_stream():
    pubsub = red.pubsub()
    pubsub.subscribe('processed')
    for message in pubsub.listen():
        print message
        if message['type'] == 'message':
            yield 'data: {0}\n\n'.format(message['data'])


@app.route('/post', methods=['POST'])
def post():
    sha1sum = sha1(flask.request.data).hexdigest()
    target = os.path.join(DATA_DIR, '{0}.jpg'.format(sha1sum))
    if not os.path.isfile(target):  # Save the file to disk
        with open(target, 'wb') as fp:
            fp.write(flask.request.data)
    # Notify subscribers of completion
    red.publish('processed', target)
    return ''


@app.route('/stream')
def stream():
    return flask.Response(event_stream(),
                          mimetype="text/event-stream")


@app.route('/')
def home():
    # 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, filename[:-4]))
    images = '\n'.join('<div id="{0}"><img src="{1}" /></div>'.format(x[2], x[1])
                       for x in sorted(image_infos, reverse=True))
    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>
<style>
  body {
    max-width: 500px;
    margin: auto;
    padding: 1em;
    background: black;
    color: #fff;
    font: 16px/1.6 menlo, monospace;
  }
</style>
<h3>Image Uploader</h3>
<p>Image: <input id="file" type="file" /></p>
<h3>Uploaded Image</h3>
<div id="images">%s</div>
<script>
  function sse() {
      var source = new EventSource('/stream');
      source.onmessage = function(e) {
          console.log(e.data);
          var image = $('<img>', {src: e.data}).hide();
          $('#images').prepend(image);
          image.load(function(){
              image.show('blind', {}, 1000);
          });
      };
  }
  $('#file').change(function(e){
      var xhr = new XMLHttpRequest();
      xhr.open('POST', '/post', true);
      xhr.send(e.target.files[0]);
      e.target.value = '';
  });
  sse();
</script>
""" % images


if __name__ == '__main__':
    #app.debug = True
    app.run('0.0.0.0', threaded=True)

@@ -51,16 +51,42 @@
 port os%0A
+from shutil import rmtree%0A
 from has
@@ -102,16 +102,16 @@
 rt sha1%0A
-
 from sta
@@ -165,16 +165,32 @@
 = 'data'
+%0AMAX_IMAGES = 10
 %0A%0Aapp = 
@@ -317,16 +317,43 @@
 %0A%0A%0Atry:%0A
+    rmtree(DATA_DIR, True)%0A
     os.m
@@ -1519,26 +1519,12 @@
 path
-, filename%5B:-4%5D
 ))%0A
+%0A
     
@@ -1536,141 +1536,217 @@
 s = 
-'%5Cn'.join('%3Cdiv id=%22%7B0%7D%22%3E%3Cimg src=%22%7B1%7D%22 /%3E%3C/div%3E'.format(x%5B2%5D, x%5B1%5D)%0A                       for x in sorted(image_infos, reverse=True
+%5B%5D%0A    for i, (_, path) in enumerate(sorted(image_infos, reverse=True)):%0A        if i %3E= MAX_IMAGES:%0A            os.unlink(path)%0A            continue%0A        images.append('%3Cimg src=%22%7B0%7D%22 /%3E%3C/div%3E'.format(path
 ))%0A 
@@ -2835,22 +2835,33 @@
 %3E%0A%22%22%22 %25 
+'%5Cn'.join(
 images
+)
 %0A%0A%0Aif __
@@ -2882,21 +2882,20 @@
 ain__':%0A
+
     
-#
 app.debu

Clear images on restart and set image limit.