GAE + NDB + Blobstore + Google高性能图片服务

3
我正在开发一个应用程序来上传文本和图片。我已经阅读了很多关于Blobstore和Google高性能图像服务的资料,最终找到了一种将它们整合在一起实现的方法。
我想知道是否所有的工作都做得很好,或者是否有更好的方式来实现,以及是将serving_url保存在模型中还是每次打印页面中的图片时都要重新计算。
这里只有用户和图片两个模型。
以下是代码(概述,忘记我的custom.PageHandler,它只有一些易于渲染页面的函数以及检查表单值等内容)。
class User(ndb.Model):
    """ A User """
    username = ndb.StringProperty(required=True)
    password = ndb.StringProperty(required=True)
    email = ndb.StringProperty(required=True)

class Picture(ndb.Model):
    """ All pictures that a User has uploaded """
    title = ndb.StringProperty(required=True)
    description = ndb.StringProperty(required=True)
    blobKey = ndb.BlobKeyProperty(required=True)
    servingUrl = ndb.StringProperty()
    created = ndb.DateTimeProperty(auto_now_add=True)
    user = ndb.KeyProperty(kind=User)

# This class shows the user pics
class List(custom.PageHandler):
    def get(self):
        # Get the actual user pics
        pics = Picture.by_user(self.user.key)
        for pic in pics:
            pic.servingUrl = images.get_serving_url(pic.blobKey, size=90, crop=True)
        self.render_page("myPictures.htm", data=pics)

# Get and post for the send page
class Send(custom.PageHandler, blobstore_handlers.BlobstoreUploadHandler):
    def get(self):
        uploadUrl = blobstore.create_upload_url('/addPic')
        self.render_page("addPicture.htm", form_action=uploadUrl)

    def post(self):
        # Create a dictionary with the values, we will need in case of error
        templateValues = self.template_from_request()
        # Test if all data form is valid
        testErrors = check_fields(self)

        if testErrors[0]:
            # No errors, save the object
            try:
                # Get the file and upload it
                uploadFiles = self.get_uploads('picture')
                # Get the key returned from blobstore, for the first element
                blobInfo = uploadFiles[0]
                # Add the key to the template
                templateValues['blobKey'] = blobInfo.key()

                # Save all
                pic = Picture.save(self.user.key, **templateValues)
                if pic is None:
                    logging.error('Picture save error.')

                self.redirect("/myPics")

            except:
                self.render_page("customMessage.htm", custom_msg=_("Problems while uploading the picture."))
        else:
            # Errors, render the page again, with the values, and showing the errors
            templateValues = custom.prepare_errors(templateValues, testErrors[1])
            # The session for upload a file must be new every reload page
            templateValues['form_action'] = blobstore.create_upload_url('/addPic')

            self.render_page("addPicture.htm", **templateValues)

基本上,我列出所有的图片,并在jinja2模板中使用以下代码显示图片:

{% for line in data %}
  <tr>
    <td class="col-center-data"><img src="{{ line.servingUrl }}"></td>

在List类中,我计算了每个服务url并且将其暂时添加到Model。我不确定直接保存到Model中是否好,因为我不知道url是否会随着时间改变。那么图片的url是永久的吗?如果是真的,我可以直接保存它,而不是计算它,对吗?
Send类仅显示上传图片的表单并将数据保存到Model中。每当需要重新渲染页面时,我都会生成新的form_action链接,因为文档也这样要求。这样做正确吗?
代码可以运行,但我想知道哪种方法更好,从性能和资源节省的角度来看。
1个回答

4

你是正确的。你确实想要保存get_serving_url()而不是重复调用它。它保持不变。

请注意,当您完成使用该URL时(例如删除blob时),存在delete_serving_url()函数可以将其删除。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接