greut / up (http://yoan.dosimple.ch/blog/2009/05/10/)

A simple picture uploader that does asynchronous resizing using RabbitMQ.

Clone this repository (size: 41.4 KB): HTTPS / SSH
$ hg clone http://bitbucket.org/greut/up/
commit 2: 4ad1e78055c6
parent 1: 9cfc6b7f3dd4
branch: default
Creating specific thumbnails respecting original (PNG or JPEG) format.
Yoan Blanc
11 months ago

Changed (Δ329 bytes):

raw changeset »

consumer.py (14 lines added, 4 lines removed)

wsgi.py (8 lines added, 2 lines removed)

Up to file-list consumer.py:

@@ -4,7 +4,12 @@ import shutil
4
4
import images
5
5
import os.path
6
6
7
from twisted.internet.defer import inlineCallbacks
7
try:
8
    import cPickle as pickle
9
except ImportError, e:
10
    import pickle
11
12
from twisted.internet.defer import inlineCallbacks, returnValue
8
13
from twisted.internet import reactor
9
14
from twisted.internet.protocol import ClientCreator
10
15
@@ -23,6 +28,10 @@ SIZES = {"thumb": (100, 100),
23
28
        }
24
29
25
30
@inlineCallbacks
31
def errConnection(error):
32
    print error
33
34
@inlineCallbacks
26
35
def gotConnection(conn, authentication):
27
36
    yield conn.start(authentication)
28
37
    chan = yield conn.channel(1)
@@ -42,20 +51,20 @@ def gotConnection(conn, authentication):
42
51
                             consumer_tag="resize_tag")
43
52
44
53
    def recv_callback(msg, chan, queue):
45
        filename = msg.content.body
54
        filename, type = pickle.loads(msg.content.body)
46
55
        tmp = os.path.join(PATHS["temp"], filename)
47
56
        if os.path.exists(tmp):
48
57
            # square
49
58
            thumb = os.path.join(PATHS["thumb"], filename)
50
59
            if os.path.exists(thumb):
51
60
                os.unlink(thumb)
52
            images.fixed_thumbnail(SIZES["thumb"], tmp, thumb)
61
            images.fixed_thumbnail(SIZES["thumb"], tmp, thumb, type)
53
62
            
54
63
            # max size
55
64
            medium = os.path.join(PATHS["medium"], filename)
56
65
            if os.path.exists(medium):
57
66
                os.unlink(medium)
58
            images.thumbnail(SIZES["medium"], tmp, medium)
67
            images.thumbnail(SIZES["medium"], tmp, medium, type)
59
68
            
60
69
            # origin
61
70
            original = os.path.join(PATHS["original"], filename)
@@ -87,6 +96,7 @@ def main():
87
96
                          ).connectTCP("localhost", 5672)
88
97
89
98
    client.addCallback(gotConnection, authentication)
99
    client.addErrback(errConnection)
90
100
    try:
91
101
        print "Consumer running, press Ctrl+C to quit"
92
102
        reactor.run()

Up to file-list wsgi.py:

1
1
#!/usr/bin/env python
2
2
# -*- coding: utf-8 -*-
3
3
4
import webob
4
5
import os.path
5
6
7
try:
8
    import cPickle as pickle
9
except ImportError, e:
10
    import pickle
11
6
12
from paste.cascade import Cascade
7
13
from paste.urlparser import StaticURLParser
8
14
@@ -56,7 +62,7 @@ def sendMessage(exchange, message, confi
56
62
57
63
    chan = conn.channel()
58
64
59
    msg = amqp.Message(message)
65
    msg = amqp.Message(pickle.dumps(message))
60
66
    msg.properties["delivery_mode"] = 2 # persistant
61
67
62
68
    chan.basic_publish(msg, exchange=exchange)
@@ -88,7 +94,7 @@ def application(environ, start_response)
88
94
                picture.make_file()
89
95
                fp.write(picture.file.read())
90
96
                fp.close()
91
                sendMessage("resize", filename)
97
                sendMessage("resize", (filename, picture.type[6:]))
92
98
                body = TEMPLATES["success"] % filename
93
99
            else:
94
100
                body = TEMPLATES["failure"] % filename