Beispiel #1
0
        def resCb(payload):
            id = num_encode(int(payload))

            params = {'param': param}
            if attached:
                params['attached'] = simplejson.loads(attached)

            jsonParams = simplejson.dumps(params)
            safeBody = simplejson.dumps({'body': body})[len("{'body': \""):-2]

            def headCb(payload):
                req.result = id

                if uid:
                    log.push(
                        "%s/head" % uid,
                        simplejson.dumps({
                            'type': type,
                            'params': params,
                            'body': safeBody
                        }))

                res.callback(None)

            d2 = data.putHead(id, title, type, uid, jsonParams, safeBody, time,
                              user)
            d2.addCallback(headCb)
Beispiel #2
0
        def nextCb(payload):
            id = num_encode(int(payload))
            req.result = id

            params = {'param': param}
            if attached:
                params['attached'] = simplejson.loads(attached)

            jsonParams = simplejson.dumps(params)
            safeBody = simplejson.dumps({'body': body})[len("{'body': \""):-2]

            def tailCb(payload):
                if payload:
                    payload = simplejson.loads(payload)

                broadcast = {
                    'tail' : [{
                        'id' : id,
                        'parent' : pid,
                        'uid' : uid,
                        'lid' : lid,
                        'user' : user,
                        'type' : type,
                        'params' : params,
                        'body' : safeBody,
                        'time' : time
                    }],

                    'users' : [payload]
                }
                
                def broadcastCb(payload):
                    if puid:
                        def uidCb(payload):
                            res.callback(None)

                        if puid:
                            log.push("%s/reply" % uid, simplejson.dumps({'reply': broadcast['tail'][0]}))
                        
                        d4 = comm.broadcast('/user/' + puid, simplejson.dumps({'reply': broadcast}))
                        d4.addCallback(uidCb)
                                    
                    else:
                        res.callback(None)

                if uid:
                    log.push("%s/tail" % uid, simplejson.dumps({'tail': broadcast['tail'][0]}))
                        
                d3 = comm.broadcast('/chan/tail_' + lid, simplejson.dumps({'broadcast': broadcast}))
                d3.addCallback(broadcastCb)
            
            d2 = data.putTail(lid, id, pid, uid, user, type, jsonParams, safeBody, time)
            d2.addCallback(tailCb)
Beispiel #3
0
        def resCb(payload):
            id = num_encode(int(payload))

            params = {"param": param}
            if attached:
                params["attached"] = simplejson.loads(attached)

            jsonParams = simplejson.dumps(params)
            safeBody = simplejson.dumps({"body": body})[len("{'body': \"") : -2]

            def headCb(payload):
                req.result = id

                if uid:
                    log.push("%s/head" % uid, simplejson.dumps({"type": type, "params": params, "body": safeBody}))

                res.callback(None)

            d2 = data.putHead(id, title, type, uid, jsonParams, safeBody, time, user)
            d2.addCallback(headCb)
Beispiel #4
0
        def nextCb(payload):
            id = num_encode(int(payload))
            req.result = id

            params = {'param': param}
            if attached:
                params['attached'] = simplejson.loads(attached)

            jsonParams = simplejson.dumps(params)
            safeBody = simplejson.dumps({'body': body})[len("{'body': \""):-2]

            def tailCb(payload):
                if payload:
                    payload = simplejson.loads(payload)

                broadcast = {
                    'tail': [{
                        'id': id,
                        'parent': pid,
                        'uid': uid,
                        'lid': lid,
                        'user': user,
                        'type': type,
                        'params': params,
                        'body': safeBody,
                        'time': time
                    }],
                    'users': [payload]
                }

                def broadcastCb(payload):
                    if puid:

                        def uidCb(payload):
                            res.callback(None)

                        if puid:
                            log.push(
                                "%s/reply" % uid,
                                simplejson.dumps(
                                    {'reply': broadcast['tail'][0]}))

                        d4 = comm.broadcast(
                            '/user/' + puid,
                            simplejson.dumps({'reply': broadcast}))
                        d4.addCallback(uidCb)

                    else:
                        res.callback(None)

                if uid:
                    log.push("%s/tail" % uid,
                             simplejson.dumps({'tail': broadcast['tail'][0]}))

                d3 = comm.broadcast('/chan/tail_' + lid,
                                    simplejson.dumps({'broadcast': broadcast}))
                d3.addCallback(broadcastCb)

            d2 = data.putTail(lid, id, pid, uid, user, type, jsonParams,
                              safeBody, time)
            d2.addCallback(tailCb)
Beispiel #5
0
def http_POST(req):
    attachment = req.files.get('attachment', [None])[0]
    id = num_encode(uuid.uuid4().int)
    
    atype = None
    aname = None
    if attachment:
        atype = TYPES.get(attachment[0][-4:].lower(), None)
        if atype:
            res = DeferredWrapper() 
                        
            aname = "attch_%s.%s" % (id, atype['extension'])
            store = s3.SimpleStorageService(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY)
            imgdata = attachment[2].read()

            def putCb(payload):
                res.callback(None)
            
            d1 = store.putObjectData(AWS_ATTACHMENT_BUCKET, aname, imgdata, contentType=atype['contentType'], public=True)
            d1.addCallback(putCb)
            
            # scaled image
            
            imgbuffer = cStringIO.StringIO(imgdata)
            scaledata = Image.open(imgbuffer)
            
            (w, h) = scaledata.size
            (x, y) = (0, 0)
            xscale = float(900.0 / float(w))
            yscale = float(400.0 / float(h))
            if xscale > yscale:
                scale = yscale
            else:
                scale = xscale
            if scale > 1.0:
                scale = 1.0
            w = int(w * scale)
            h = int(h * scale)
            
            scaledata = scaledata.resize((w, h), Image.ANTIALIAS)
            imgbuffer.close()
            imgbuffer = cStringIO.StringIO()
            scaledata.save(imgbuffer, atype['encoder'])
            scaledata = imgbuffer.getvalue()
            imgbuffer.close()

            d2 = store.putObjectData(AWS_ATTACHMENT_BUCKET, "scaled_%s" % aname, scaledata, contentType=atype['contentType'], public=True)
            d2.addCallback(putCb)

            # thumbnail image

            imgbuffer = cStringIO.StringIO(imgdata)
            thumbdata = Image.open(imgbuffer)
            
            (w, h) = thumbdata.size
            (x, y) = (0, 0)
            scale = float(50.0 / float(w))
            w = int(w * scale)
            h = int(h * scale)
            
            thumbdata = thumbdata.resize((w, h), Image.ANTIALIAS)
            thumbdata = ImageEnhance.Brightness(ImageOps.grayscale(thumbdata)).enhance(0.50)
            imgbuffer.close()
            imgbuffer = cStringIO.StringIO()
            thumbdata.save(imgbuffer, atype['encoder'])
            thumbdata = imgbuffer.getvalue()
            imgbuffer.close()
            
            d3 = store.putObjectData(AWS_ATTACHMENT_BUCKET, "thumb_%s" % aname, thumbdata, contentType=atype['contentType'], public=True)
            d3.addCallback(putCb)
            
            req.result = {'name': aname, 'type': atype}
            return None
        else:
            req.result = None
            return None