开发者

How to use get_serving_url in appengine?

The following is my main.py so far.

import cgi
import datetime
import logging

from google.appengine.ext import db
from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.api import images

logging.getLogger().setLevel(logging.DEBUG)


class Greeting(db.Model):
    author = db.UserProperty()
    content = db.StringProperty(multiline=True)
    imageblob = blobstore.BlobReferebceProperty()
    date = db.DateTimeProperty(auto_now_add=True)

class MainPage(webapp.RequestHandler):
    def get(self):
        self.response.out.write('<html><body>')
        query_str = "SELECT * FROM Greeting ORDER BY date DESC LIMIT 10"
        greetings = db.GqlQuery (query_str)

        for greeting in greetings:
            if greeting.author:
                self.response.out.write('<b>%s</b> wrote:' % greeting.author.nickname())
            else:
                self.response.out.write('An anonymous person wrote:')
            self.response.out.write("<div><img src='img?img_id=%s'></img>" %
                                greeting.key())
            self.response.out.write(' %s</div>' %
                              cgi.escape(greeting.content))

        self.response.out.write("""
          <form action="/sign" enctype="multipart/form-data" method="post">
            <div><label>Message:</label></div>
            <div><textarea name="content开发者_如何学Python" rows="3" cols="60"></textarea></div>
            <div><label>Avatar:</label></div>
            <div><input type="file" name="img"/></div>
            <div><input type="submit" value="Sign Guestbook"></div>
          </form>
        </body>
        </html>""")

class Image (webapp.RequestHandler):
    def get(self):
        greeting = db.get(self.request.get("img_id"))
        if greeting.avatar:
            self.response.headers['Content-Type'] = "image/png"
            self.response.out.write(greeting.avatar)
        else:
            self.response.out.write("No image")

class Guestbook(webapp.RequestHandler):
    def post(self):
        greeting = Greeting()
        if users.get_current_user():
            greeting.author = users.get_current_user()
        greeting.content = self.request.get("content")
        avatar = get_serving_url(self.request.get("img"), size=None, crop=False)
        #avatar = images.crop(self.request.get("img"), 0.0, 0.0,1.0,0.5)
        greeting.avatar = db.Blob(avatar)
        greeting.put()
        self.redirect('/')


application = webapp.WSGIApplication([
    ('/', MainPage),
    ('/img', Image),
    ('/sign', Guestbook)
], debug=True)


def main():
    run_wsgi_app(application)


if __name__ == '__main__':
    main()


Like it says in the docs:

Returns a URL that serves the image. This URL format allows dynamic resizing and cropping, so you don't need to store different image sizes on the server. Images are served with low latency from a highly optimized, cookieless infrastructure.

In your code you seem to be using get_serving_url() to construct a Blob. This will not work, since get_serving_url() returns a URL, not the image data.

You also seem to be confusing a blobstore.BlobReferenceProperty and a db.BlobProperty -- a BlobReferenceProperty references an object uploaded and stored in the blobstore while a BlobProperty stores blob data directly in the datastore.

A better solution to your problem would be to store the image you get in the request (as a BlobProperty) in your model, then serve it at different sizes using get_serving_url().

Alternatively, if you expect to be storing large images, store the images in the blobstore and use the BlobReferenceProperty -- either way, you should only be using get_serving_url() to serve the image, not in storing it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜