<s> import uuid <EOL> import mimetypes <EOL> from thumbor . handlers import ImageApiHandler <EOL> from thumbor . engines import BaseEngine <EOL> class ImageUploadHandler ( ImageApiHandler ) : <EOL> def post ( self ) : <EOL> if self . multipart_form_data ( ) : <EOL> file_data = self . request . files [ '<STR_LIT>' ] [ <NUM_LIT:0> ] <EOL> body = file_data [ '<STR_LIT:body>' ] <EOL> filename = file_data [ '<STR_LIT:filename>' ] <EOL> else : <EOL> body = self . request . body <EOL> filename = self . request . headers . get ( '<STR_LIT>' ) <EOL> if self . validate ( body ) : <EOL> if not filename : <EOL> content_type = self . request . headers . get ( '<STR_LIT:Content-Type>' , BaseEngine . get_mimetype ( body ) ) <EOL> extension = mimetypes . guess_extension ( content_type . split ( '<STR_LIT:;>' , <NUM_LIT:1> ) [ <NUM_LIT:0> ] , False ) <EOL> if extension is None : <EOL> extension = mimetypes . guess_extension ( BaseEngine . get_mimetype ( body ) , False ) <EOL> if extension == '<STR_LIT>' : <EOL> extension = '<STR_LIT>' <EOL> if extension is None : <EOL> extension = '<STR_LIT>' <EOL> filename = self . context . config . UPLOAD_DEFAULT_FILENAME + extension <EOL> image_id = str ( uuid . uuid4 ( ) . hex ) <EOL> self . write_file ( image_id , body ) <EOL> self . set_status ( <NUM_LIT> ) <EOL> self . set_header ( '<STR_LIT>' , self . location ( image_id , filename ) ) <EOL> def multipart_form_data ( self ) : <EOL> if '<STR_LIT>' not in self . request . files or not self . request . files [ '<STR_LIT>' ] : <EOL> return False <EOL> else : <EOL> return True <EOL> def location ( self , image_id , filename ) : <EOL> base_uri = self . request . uri <EOL> return '<STR_LIT>' % ( base_uri , image_id , filename ) </s>
