diff --git a/appengine/images/api/blobstore.py b/appengine/images/api/blobstore.py new file mode 100644 index 000000000000..9a838ac68f34 --- /dev/null +++ b/appengine/images/api/blobstore.py @@ -0,0 +1,52 @@ +# Copyright 2015 Google Inc. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +""" +Sample application that demonstrates how to use the App Engine Images API. + +For more information, see README.md. +""" + +# [START all] +# [START thumbnailer] +from google.appengine.api import images +from google.appengine.ext import blobstore + +import webapp2 + + +class Thumbnailer(webapp2.RequestHandler): + def get(self): + blob_key = self.request.get("blob_key") + if blob_key: + blob_info = blobstore.get(blob_key) + + if blob_info: + img = images.Image(blob_key=blob_key) + img.resize(width=80, height=100) + img.im_feeling_lucky() + thumbnail = img.execute_transforms(output_encoding=images.JPEG) + + self.response.headers['Content-Type'] = 'image/jpeg' + self.response.out.write(thumbnail) + return + + # Either "blob_key" wasn't provided, or there was no value with that ID + # in the Blobstore. + self.error(404) +# [END thumbnailer] + + +app = webapp2.WSGIApplication([('/img', Thumbnailer)], debug=True) +# [END all] diff --git a/appengine/images/api/blobstore_test.py b/appengine/images/api/blobstore_test.py new file mode 100644 index 000000000000..a0f5e0d4129d --- /dev/null +++ b/appengine/images/api/blobstore_test.py @@ -0,0 +1,45 @@ +# Copyright 2015 Google Inc. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import blobstore +import mock +import pytest +import webtest + + +@pytest.fixture +def app(testbed): + return webtest.TestApp(blobstore.app) + + +def test_img(app): + with mock.patch('blobstore.images') as mock_images: + with mock.patch('blobstore.blobstore') as mock_blobstore: + mock_blobstore.get.return_value = b'123' + mock_images.resize.return_value = 'asdf' + mock_images.im_feeling_lucky.return_value = 'gsdf' + + response = app.get('/img?blob_key=123') + + assert response.status_int == 200 + + +def test_img_missing(app): + # Bogus blob_key, should get error + app.get('/img?blob_key=123', status=404) + + +def test_no_img_id(app): + # No blob_key, should get error + app.get('/img', status=404) diff --git a/appengine/images/api/main_test.py b/appengine/images/api/main_test.py index 68e7808f6227..2cad660af474 100644 --- a/appengine/images/api/main_test.py +++ b/appengine/images/api/main_test.py @@ -45,5 +45,5 @@ def test_img_missing(app): def test_no_img_id(app): - # Bogus image id, should get error + # No image id, should get error app.get('/img', status=404)