<s> """<STR_LIT>""" <EOL> __author__ = '<STR_LIT>' <EOL> import sys <EOL> sys . path . insert ( <NUM_LIT:0> , '<STR_LIT>' ) <EOL> import cloudstorage as gcs <EOL> import email <EOL> import httplib2 <EOL> import json <EOL> import os <EOL> import utils <EOL> import uuid <EOL> import webapp2 <EOL> from google . appengine . api import app_identity <EOL> from google . appengine . ext import blobstore <EOL> from google . appengine . ext . webapp import blobstore_handlers <EOL> from apiclient . discovery import build <EOL> from apiclient . errors import HttpError <EOL> from oauth2client . client import AccessTokenCredentials <EOL> my_default_retry_params = gcs . RetryParams ( initial_delay = <NUM_LIT> , <EOL> max_delay = <NUM_LIT> , <EOL> backoff_factor = <NUM_LIT:2> , <EOL> max_retry_period = <NUM_LIT:15> ) <EOL> gcs . set_default_retry_params ( my_default_retry_params ) <EOL> bucket = "<STR_LIT:/>" + os . environ . get ( "<STR_LIT>" , app_identity . get_default_gcs_bucket_name ( ) ) <EOL> class UploadHandler ( webapp2 . RequestHandler ) : <EOL> _metainfo = None <EOL> _content_type = None <EOL> _content = None <EOL> _token = None <EOL> _service = None <EOL> def dispatch ( self ) : <EOL> self . _checkauth ( ) <EOL> self . _decode ( ) <EOL> if self . _token is None : <EOL> self . abort ( <NUM_LIT> ) <EOL> else : <EOL> credentials = AccessTokenCredentials ( self . _token , "<STR_LIT>" ) <EOL> http = httplib2 . Http ( ) <EOL> http = credentials . authorize ( http ) <EOL> http . timeout = <NUM_LIT> <EOL> self . _service = build ( "<STR_LIT>" , "<STR_LIT>" , http = http , discoveryServiceUrl = utils . discovery_service_url ) <EOL> super ( UploadHandler , self ) . dispatch ( ) <EOL> def _checkauth ( self ) : <EOL> if "<STR_LIT>" in self . request . headers : <EOL> self . _token = self . request . headers [ "<STR_LIT>" ] . split ( "<STR_LIT:U+0020>" ) [ <NUM_LIT:1> ] <EOL> def _decode ( self ) : <EOL> """<STR_LIT>""" <EOL> content_type = self . request . content_type <EOL> if content_type == "<STR_LIT>" or content_type == "<STR_LIT>" : <EOL> message = "<STR_LIT>" + self . request . headers [ "<STR_LIT:Content-Type>" ] + "<STR_LIT:\r\n>" <EOL> message += self . request . body <EOL> msg = email . message_from_string ( message ) <EOL> if not msg . is_multipart ( ) : <EOL> return <EOL> for payload in msg . get_payload ( ) : <EOL> content_type = payload . get_content_type ( ) <EOL> if content_type . startswith ( "<STR_LIT>" ) or content_type . startswith ( "<STR_LIT>" ) or content_type . startswith ( "<STR_LIT>" ) : <EOL> if self . _content is None : <EOL> self . _content_type = content_type <EOL> self . _content = payload . get_payload ( decode = True ) <EOL> elif content_type == "<STR_LIT:application/json>" : <EOL> if self . _metainfo is None : <EOL> self . _metainfo = json . loads ( payload . get_payload ( ) ) <EOL> return <EOL> if content_type . startswith ( "<STR_LIT>" ) or content_type . startswith ( "<STR_LIT>" ) or content_type . startswith ( "<STR_LIT>" ) : <EOL> self . _content_type = content_type <EOL> if "<STR_LIT>" in self . request . headers and self . request . headers [ "<STR_LIT>" ] . lower ( ) == "<STR_LIT>" : <EOL> self . _content = self . request . body . decode ( "<STR_LIT>" ) <EOL> else : <EOL> self . _content = self . request . body <EOL> class InsertHandler ( UploadHandler ) : <EOL> def post ( self ) : <EOL> self . response . content_type = "<STR_LIT:application/json>" <EOL> if self . _content is None : <EOL> self . response . status = <NUM_LIT> <EOL> self . response . out . write ( utils . createError ( <NUM_LIT> , "<STR_LIT>" ) ) <EOL> if self . _metainfo is None : <EOL> request = self . _service . internal ( ) . timeline ( ) . insert ( body = { } ) <EOL> else : <EOL> request = self . _service . internal ( ) . timeline ( ) . insert ( body = self . _metainfo ) <EOL> try : <EOL> card = request . execute ( ) <EOL> except HttpError as e : <EOL> self . response . status = e . resp . status <EOL> self . response . out . write ( e . content ) <EOL> return <EOL> write_retry_params = gcs . RetryParams ( backoff_factor = <NUM_LIT> ) <EOL> file_name = str ( uuid . uuid4 ( ) ) <EOL> gcs_file = gcs . open ( bucket + "<STR_LIT:/>" + file_name , <EOL> '<STR_LIT:w>' , <EOL> content_type = self . _content_type , <EOL> retry_params = write_retry_params ) <EOL> gcs_file . write ( self . _content ) <EOL> gcs_file . close ( ) <EOL> if not "<STR_LIT>" in card : <EOL> card [ "<STR_LIT>" ] = [ ] <EOL> attachment = { <EOL> "<STR_LIT:id>" : file_name , <EOL> "<STR_LIT>" : self . _content_type , <EOL> "<STR_LIT>" : "<STR_LIT>" % ( utils . base_url , card [ "<STR_LIT:id>" ] , file_name ) , <EOL> "<STR_LIT>" : False <EOL> } <EOL> card [ "<STR_LIT>" ] . append ( attachment ) <EOL> request = self . _service . internal ( ) . timeline ( ) . update ( id = card [ "<STR_LIT:id>" ] , body = card ) <EOL> try : <EOL> result = request . execute ( ) <EOL> except HttpError as e : <EOL> self . response . status = e . resp . status <EOL> self . response . out . write ( e . content ) <EOL> return <EOL> self . response . status = <NUM_LIT:200> <EOL> self . response . out . write ( json . dumps ( result ) ) <EOL> class UpdateHandler ( UploadHandler ) : <EOL> def put ( self , id ) : <EOL> self . response . content_type = "<STR_LIT:application/json>" <EOL> if self . _content is None : <EOL> self . response . status = <NUM_LIT> <EOL> self . response . out . write ( utils . createError ( <NUM_LIT> , "<STR_LIT>" ) ) <EOL> request = self . _service . timeline ( ) . get ( id = id ) <EOL> try : <EOL> card = request . execute ( ) <EOL> except HttpError as e : <EOL> self . response . status = e . resp . status <EOL> self . response . out . write ( e . content ) <EOL> return <EOL> write_retry_params = gcs . RetryParams ( backoff_factor = <NUM_LIT> ) <EOL> file_name = str ( uuid . uuid4 ( ) ) <EOL> gcs_file = gcs . open ( bucket + "<STR_LIT:/>" + file_name , <EOL> '<STR_LIT:w>' , <EOL> content_type = self . _content_type , <EOL> retry_params = write_retry_params ) <EOL> gcs_file . write ( self . _content ) <EOL> gcs_file . close ( ) <EOL> if self . _metainfo is None : <EOL> new_card = { } <EOL> else : <EOL> new_card = self . _metainfo <EOL> if "<STR_LIT>" in card : <EOL> new_card [ "<STR_LIT>" ] = card [ "<STR_LIT>" ] <EOL> else : <EOL> new_card [ "<STR_LIT>" ] = [ ] <EOL> attachment = { <EOL> "<STR_LIT:id>" : file_name , <EOL> "<STR_LIT>" : self . _content_type , <EOL> "<STR_LIT>" : "<STR_LIT>" % ( utils . base_url , card [ "<STR_LIT:id>" ] , file_name ) , <EOL> "<STR_LIT>" : False <EOL> } <EOL> new_card [ "<STR_LIT>" ] . append ( attachment ) <EOL> request = self . _service . internal ( ) . timeline ( ) . update ( id = card [ "<STR_LIT:id>" ] , body = new_card ) <EOL> try : <EOL> result = request . execute ( ) <EOL> except HttpError as e : <EOL> self . response . status = e . resp . status <EOL> self . response . out . write ( e . content ) <EOL> return <EOL> self . response . status = <NUM_LIT:200> <EOL> self . response . out . write ( json . dumps ( result ) ) <EOL> class AttachmentInsertHandler ( UploadHandler ) : <EOL> def post ( self , id ) : <EOL> self . response . content_type = "<STR_LIT:application/json>" <EOL> if self . _content is None : <EOL> self . response . status = <NUM_LIT> <EOL> self . response . out . write ( utils . createError ( <NUM_LIT> , "<STR_LIT>" ) ) <EOL> request = self . _service . timeline ( ) . get ( id = id ) <EOL> try : <EOL> card = request . execute ( ) <EOL> except HttpError as e : <EOL> self . response . status = e . resp . status <EOL> self . response . out . write ( e . content ) <EOL> return <EOL> write_retry_params = gcs . RetryParams ( backoff_factor = <NUM_LIT> ) <EOL> file_name = str ( uuid . uuid4 ( ) ) <EOL> gcs_file = gcs . open ( bucket + "<STR_LIT:/>" + file_name , <EOL> '<STR_LIT:w>' , <EOL> content_type = self . _content_type , <EOL> retry_params = write_retry_params ) <EOL> gcs_file . write ( self . _content ) <EOL> gcs_file . close ( ) <EOL> if not "<STR_LIT>" in card : <EOL> card [ "<STR_LIT>" ] = [ ] <EOL> attachment = { <EOL> "<STR_LIT:id>" : file_name , <EOL> "<STR_LIT>" : self . _content_type , <EOL> "<STR_LIT>" : "<STR_LIT>" % ( utils . base_url , card [ "<STR_LIT:id>" ] , file_name ) , <EOL> "<STR_LIT>" : False <EOL> } <EOL> card [ "<STR_LIT>" ] . append ( attachment ) <EOL> request = self . _service . internal ( ) . timeline ( ) . update ( id = card [ "<STR_LIT:id>" ] , body = card ) <EOL> try : <EOL> result = request . execute ( ) <EOL> except HttpError as e : <EOL> self . response . status = e . resp . status <EOL> self . response . out . write ( e . content ) <EOL> return <EOL> self . response . status = <NUM_LIT:200> <EOL> self . response . out . write ( json . dumps ( result ) ) <EOL> class DownloadHandler ( UploadHandler , blobstore_handlers . BlobstoreDownloadHandler ) : <EOL> def get ( self , id , attachment ) : <EOL> request = self . _service . timeline ( ) . get ( id = id ) <EOL> try : <EOL> request . execute ( ) <EOL> except HttpError as e : <EOL> self . response . status = e . resp . status <EOL> self . response . out . write ( e . content ) <EOL> return <EOL> blob_key = blobstore . create_gs_key ( "<STR_LIT>" + bucket + "<STR_LIT:/>" + attachment ) <EOL> self . send_blob ( blob_key ) <EOL> app = webapp2 . WSGIApplication ( <EOL> [ <EOL> ( r"<STR_LIT>" , DownloadHandler ) , <EOL> ( r"<STR_LIT>" , AttachmentInsertHandler ) , <EOL> ( r"<STR_LIT>" , UpdateHandler ) , <EOL> ( "<STR_LIT>" , InsertHandler ) <EOL> ] , <EOL> debug = True <EOL> ) </s>
