Exemplo n.º 1
0
 def post(self):
     """
     Note: BlobstoreUploadHandler POSTs must return a redirect
     """
     logging.info("starting post")
     upload_files = self.get_uploads()
     handle = self.request.get("handle")
     signature = self.request.get("signature")
     logging.info("Handling post for handle=%s signature=%s" % (handle, signature))
     try:
         if len(upload_files) > 0:
             blob_info = upload_files[0]
             data = blobstore.fetch_data(blob_info.key(), 0, blob_info.size) 
             imgtemp = images.Image(image_data=data)
             width = imgtemp.width
             height = imgtemp.height
             logging.info("Got upload %s => handle=%s size=%dx%d" % (blob_info.__dict__, handle, width, height))
             
             if is_alphanumeric(handle) and \
                 is_alphanumeric(signature) and \
                 is_signature_valid(handle, signature):
                 image = find_image_by_handle(handle)
                 api_response = "/image/info?handle=%s&signature=%s" % (handle, signature)
                 if image is not None:
                     # TODO: add an option to clobber an existing image
                     response = { 'handle': image.handle, 'created_at': image.created_at.isoformat(), 'status': image.status }
                     logging.info("Image already exists: %s" % response)
                     self.redirect(api_response)
                     return
                 image = ImageAsset(handle=handle, status="done", width=width, height=height, blob_key=blob_info.key())
                 image.put()
                 if (self.request.get("return")):
                     # this was submitted by the Gallery form
                     self.redirect(self.request.get("return"))
                 else:
                     # this was submitted by an API client
                     logging.info("Created image: %s" % image)
                     # TODO: callback to the BC API to update that the image submission completed
                     self.redirect(api_response)
                 return
             else:
                 logging.error("Invalid params: handle=%s signature=%s" % (handle, signature))
         else:
             media = self.request.get("media")
             logging.error("Nothing uploaded? %s" % upload_files)
             logging.error("media = %s" % media)
             logging.error("Request data: %s" % self.request.__dict__)
     #except blobstore.BlobFetchSizeTooLargeError, e:
     #    self.redirect("/image/error?handle=%s&signature=%s&error=%s" % (handle, signature, urllib.quote(str(e)))
     #    return
     #except DeadlineExceededError, e:
     #    self.redirect("/image/error?handle=%s&signature=%s&error=%s" % (handle, signature, urllib.quote(str(e)))
     #    return
     except:
         logging.error("Request data: %s" % self.request.__dict__)
         logging.error("Image upload failed: ", exc_info=1)
     # TODO (maybe): redirect to an error handler
     self.error(500)