Exemplo n.º 1
0
    def test_convert_1(self):
        
        f_id = create_fig("testfigs/test1.pdf", self.db)

        reqdoc = {
            'outputformat' : 'png',
            'max' : {'height' : 600,
                     'width' : 300}}

        x = figure.convert_request(self.db.fs, f_id, reqdoc, self.tempdir)

        # this should be the first of these requests, so
        assert_equal(x['state'], 'pending')
        assert_equal(x['source'], f_id)
        assert_equal(x['type'], 'conversion')
        assert_equal(x['outputformat'], 'png')
        
        # try again, should trigger a check
        while True:
            x = figure.convert_request(self.db.fs, f_id, reqdoc, self.tempdir)

            time.sleep(1)
            if x['state'] == 'done' :
                break

        assert_equal(x['error'], None)
        # get the id of the new doc
        g = gridfs.GridFS(self.db)
        f = g.get(x['output'])
        file("/tmp/test2.png", 'w').write(f.read())
Exemplo n.º 2
0
def files(notebook, fileid):
    if "." in fileid:
        fileid, ext = fileid.split(".")
    else:
        ext = None
        
    nbdb = g.dbconn[get_nb_dbname(notebook)]
    gfs = gridfs.GridFS(nbdb)
    fid = gfs.get(bson.objectid.ObjectId(fileid))
    # we do this lookup to make sure that the FID is valid
    
    content_type = fid.content_type
    # construct convert request
    conv_req = None
    if len(request.args) > 0:
        conv_req = {}
        conv_req['outputformat'] = ext
        if "max_height" or "max_width" in request.args:
            conv_req["max"] = {}
            if "max_height" in request.args:
                conv_req['max']['height'] = int(request.args['max_height'])
            if "max_width" in request.args:
                conv_req['max']['width'] = int(request.args['max_width'])
    else:
            
        if ext == None or \
        (content_type == "image/jpeg" and ext == "jpeg") or \
        (content_type == "image/jpeg" and ext == "jpg") or \
        (content_type == "image/png" and ext == "png") or \
        (content_type == "application/pdf" and ext == "pdf"):
               r =  make_response(fid.read())
               r.mimetype = content_type
               r.content_type = content_type
               return r


        conv_req = {'outputformat' :  ext}
    


    ext_to_mime = {'jpeg' : "image/jpeg",
                   'jpg' : "image/jpeg",
                   'png' : "image/png",
                   'pdf' : "application/pdf"}
    
    
    conv_res  = figure.convert_request(nbdb.fs, fid._id, conv_req,
                                       app.config["FIGURE_TEMP_DIR"])
    if conv_res['state'] == "pending":
        return "PENDING", 202

    if conv_res['state'] == "done":
        # fixme include error handling
        of = gfs.get(conv_res['output'])
        r = make_response(of.read())
        
        r.mimetype = ext_to_mime[ext]
        r.content_type = ext_to_mime[ext]
        m = hashlib.md5()
        m.update(json.dumps(conv_req))

        r.headers['ETag'] = of.md5 + m.hexdigest()
        r.headers['Cache-Control'] = "max-age=3218319841"
        r.headers['Expires'] = "Expires: 19 Jun 2012 11:30:24 GMT"

        return r