<s> """<STR_LIT>""" <EOL> import base64 <EOL> from datetime import datetime <EOL> from datetime import timedelta <EOL> import json <EOL> from protorpc import messages <EOL> from protorpc import remote <EOL> import yaml <EOL> from models import Bitdoc <EOL> from google . appengine . api import app_identity <EOL> import endpoints <EOL> config = yaml . load ( open ( '<STR_LIT>' , '<STR_LIT:r>' ) ) <EOL> APP_HOSTNAME = config [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> ALLOWED_CLIENT_IDS = [ endpoints . API_EXPLORER_CLIENT_ID ] <EOL> ALLOWED_CLIENT_IDS . extend ( config [ '<STR_LIT>' ] [ '<STR_LIT>' ] ) <EOL> AUDIENCE = config [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> GCS_API_URL = '<STR_LIT>' <EOL> GCS_BUCKET = config [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> class ListImagesRequest ( messages . Message ) : <EOL> """<STR_LIT>""" <EOL> limit = messages . IntegerField ( <NUM_LIT:1> , default = <NUM_LIT:10> ) <EOL> offset = messages . IntegerField ( <NUM_LIT:2> , default = <NUM_LIT:0> ) <EOL> class ListImage ( messages . Message ) : <EOL> """<STR_LIT>""" <EOL> user = messages . StringField ( <NUM_LIT:1> ) <EOL> timestamp = messages . StringField ( <NUM_LIT:2> ) <EOL> image_link = messages . StringField ( <NUM_LIT:3> ) <EOL> image_8bit_link = messages . StringField ( <NUM_LIT:4> ) <EOL> timestamp_8bit = messages . StringField ( <NUM_LIT:5> ) <EOL> key = messages . StringField ( <NUM_LIT:6> ) <EOL> class ListImagesResponse ( messages . Message ) : <EOL> """<STR_LIT>""" <EOL> images = messages . MessageField ( ListImage , <NUM_LIT:1> , repeated = True ) <EOL> class StorageSignedUrlRequest ( messages . Message ) : <EOL> """<STR_LIT>""" <EOL> filename = messages . StringField ( <NUM_LIT:1> ) <EOL> owner = messages . StringField ( <NUM_LIT:2> ) <EOL> class StorageSignedUrlResponse ( messages . Message ) : <EOL> """<STR_LIT>""" <EOL> form_action = messages . StringField ( <NUM_LIT:1> ) <EOL> bucket = messages . StringField ( <NUM_LIT:2> ) <EOL> policy = messages . StringField ( <NUM_LIT:3> ) <EOL> signature = messages . StringField ( <NUM_LIT:4> ) <EOL> google_access_id = messages . StringField ( <NUM_LIT:5> ) <EOL> filename = messages . StringField ( <NUM_LIT:6> ) <EOL> def GetEndpointsAuthUser ( ) : <EOL> """<STR_LIT>""" <EOL> if endpoints . get_current_user ( ) is None : <EOL> raise endpoints . UnauthorizedException ( '<STR_LIT>' ) <EOL> return endpoints . get_current_user ( ) <EOL> @ endpoints . api ( name = '<STR_LIT:image>' , version = '<STR_LIT>' , description = '<STR_LIT>' , <EOL> allowed_client_ids = ALLOWED_CLIENT_IDS , <EOL> audiences = AUDIENCE , <EOL> hostname = APP_HOSTNAME ) <EOL> class ImageApi ( remote . Service ) : <EOL> """<STR_LIT>""" <EOL> @ endpoints . method ( ListImagesRequest , ListImagesResponse , <EOL> path = '<STR_LIT>' , http_method = '<STR_LIT:GET>' , name = '<STR_LIT>' ) <EOL> def ListImages ( self , request ) : <EOL> """<STR_LIT>""" <EOL> GetEndpointsAuthUser ( ) <EOL> images = [ ] <EOL> query = Bitdoc . all ( ) . order ( '<STR_LIT>' ) <EOL> for item in query . fetch ( limit = request . limit , offset = request . offset ) : <EOL> images . append ( <EOL> ListImage ( <EOL> user = item . user , <EOL> timestamp = item . timestamp_strsafe , <EOL> image_link = item . image_link , <EOL> image_8bit_link = item . image_8bit_link , <EOL> timestamp_8bit = item . timestamp_8bit_strsafe , <EOL> key = str ( item . key ( ) ) <EOL> ) <EOL> ) <EOL> return ListImagesResponse ( images = images ) <EOL> @ endpoints . method ( StorageSignedUrlRequest , StorageSignedUrlResponse , <EOL> path = '<STR_LIT>' , <EOL> http_method = '<STR_LIT:POST>' , <EOL> name = '<STR_LIT>' ) <EOL> def GenerateStorageSignedUrl ( self , request ) : <EOL> """<STR_LIT>""" <EOL> GetEndpointsAuthUser ( ) <EOL> if not request . filename : <EOL> raise endpoints . BadRequestException ( '<STR_LIT>' ) <EOL> if not request . owner : <EOL> raise endpoints . BadRequestException ( '<STR_LIT>' ) <EOL> expires = '<STR_LIT>' % ( datetime . utcnow ( ) + timedelta ( hours = <NUM_LIT:1> ) ) . isoformat ( ) [ : <NUM_LIT> ] <EOL> policy = base64 . b64encode ( json . dumps ( { <EOL> '<STR_LIT>' : expires , <EOL> '<STR_LIT>' : [ <EOL> [ '<STR_LIT>' , '<STR_LIT>' , GCS_BUCKET ] , <EOL> [ '<STR_LIT>' , '<STR_LIT>' , request . filename ] , <EOL> [ '<STR_LIT>' , '<STR_LIT>' , request . owner ] , <EOL> ] , <EOL> } ) ) <EOL> signature = base64 . b64encode ( app_identity . sign_blob ( policy ) [ <NUM_LIT:1> ] ) <EOL> return StorageSignedUrlResponse ( <EOL> form_action = GCS_API_URL % GCS_BUCKET , <EOL> bucket = GCS_BUCKET , <EOL> policy = policy , <EOL> signature = signature , <EOL> google_access_id = app_identity . get_service_account_name ( ) , <EOL> filename = request . filename <EOL> ) <EOL> server = endpoints . api_server ( <EOL> [ <EOL> ImageApi <EOL> ] , <EOL> restricted = False <EOL> ) </s>
