|
| 1 | +# Copyright 2015 Google Inc. All rights reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +# [START all] |
| 16 | +from google.appengine.api import users |
| 17 | +from google.appengine.ext import blobstore |
| 18 | +from google.appengine.ext import ndb |
| 19 | +from google.appengine.ext.webapp import blobstore_handlers |
| 20 | +import webapp2 |
| 21 | + |
| 22 | + |
| 23 | +# This datastore model keeps track of which users uploaded which photos. |
| 24 | +class UserPhoto(ndb.Model): |
| 25 | + user = ndb.StringProperty() |
| 26 | + blob_key = ndb.BlobKeyProperty() |
| 27 | + |
| 28 | + |
| 29 | +class PhotoUploadFormHandler(webapp2.RequestHandler): |
| 30 | + def get(self): |
| 31 | + # [START upload_url] |
| 32 | + upload_url = blobstore.create_upload_url('/upload_photo') |
| 33 | + # [END upload_url] |
| 34 | + # [START upload_form] |
| 35 | + # To upload files to the blobstore, the request method must be "POST" |
| 36 | + # and enctype must be set to "multipart/form-data". |
| 37 | + self.response.out.write(""" |
| 38 | +<html><body> |
| 39 | +<form action="{0}" method="POST" enctype="multipart/form-data"> |
| 40 | + Upload File: <input type="file" name="file"><br> |
| 41 | + <input type="submit" name="submit" value="Submit"> |
| 42 | +</form> |
| 43 | +</body></html>""".format(upload_url)) |
| 44 | + # [END upload_form] |
| 45 | + |
| 46 | + |
| 47 | +# [START upload_handler] |
| 48 | +class PhotoUploadHandler(blobstore_handlers.BlobstoreUploadHandler): |
| 49 | + def post(self): |
| 50 | + try: |
| 51 | + upload = self.get_uploads()[0] |
| 52 | + user_photo = UserPhoto( |
| 53 | + user=users.get_current_user().user_id(), |
| 54 | + blob_key=upload.key()) |
| 55 | + user_photo.put() |
| 56 | + |
| 57 | + self.redirect('/view_photo/%s' % upload.key()) |
| 58 | + |
| 59 | + except: |
| 60 | + self.error(500) |
| 61 | +# [END upload_handler] |
| 62 | + |
| 63 | + |
| 64 | +# [START download_handler] |
| 65 | +class ViewPhotoHandler(blobstore_handlers.BlobstoreDownloadHandler): |
| 66 | + def get(self, photo_key): |
| 67 | + if not blobstore.get(photo_key): |
| 68 | + self.error(404) |
| 69 | + else: |
| 70 | + self.send_blob(photo_key) |
| 71 | +# [END download_handler] |
| 72 | + |
| 73 | + |
| 74 | +app = webapp2.WSGIApplication([ |
| 75 | + ('/', PhotoUploadFormHandler), |
| 76 | + ('/upload_photo', PhotoUploadHandler), |
| 77 | + ('/view_photo/([^/]+)?', ViewPhotoHandler), |
| 78 | +], debug=True) |
| 79 | +# [END all] |
0 commit comments