<s> import httplib <EOL> import mimetypes <EOL> from bottle import abort , get , local , request , response <EOL> from codalab . lib import spec_util , zip_util <EOL> from codalab . objects . permission import check_bundles_have_read_permission <EOL> @ get ( '<STR_LIT>' % spec_util . UUID_STR ) <EOL> @ get ( '<STR_LIT>' % spec_util . UUID_STR ) <EOL> def get_blob ( uuid , path = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> check_bundles_have_read_permission ( local . model , request . user , [ uuid ] ) <EOL> bundle = local . model . get_bundle ( uuid ) <EOL> target_info = local . download_manager . get_target_info ( uuid , path , <NUM_LIT:0> ) <EOL> if target_info is None : <EOL> abort ( httplib . NOT_FOUND , '<STR_LIT>' ) <EOL> if not path and bundle . metadata . name : <EOL> filename = bundle . metadata . name <EOL> else : <EOL> filename = target_info [ '<STR_LIT:name>' ] <EOL> if target_info [ '<STR_LIT:type>' ] == '<STR_LIT>' : <EOL> filename = filename + '<STR_LIT>' <EOL> fileobj = local . download_manager . stream_tarred_gzipped_directory ( uuid , path ) <EOL> elif target_info [ '<STR_LIT:type>' ] == '<STR_LIT:file>' : <EOL> if not zip_util . path_is_archive ( filename ) and request_accepts_gzip_encoding ( ) : <EOL> filename = filename + '<STR_LIT>' <EOL> fileobj = local . download_manager . stream_file ( uuid , path , gzipped = True ) <EOL> else : <EOL> fileobj = local . download_manager . stream_file ( uuid , path , gzipped = False ) <EOL> else : <EOL> abort ( httplib . FORBIDDEN , '<STR_LIT>' ) <EOL> mimetype , _ = mimetypes . guess_type ( filename , strict = False ) <EOL> response . set_header ( '<STR_LIT:Content-Type>' , mimetype or '<STR_LIT>' ) <EOL> if zip_util . get_archive_ext ( filename ) == '<STR_LIT>' and request_accepts_gzip_encoding ( ) : <EOL> filename = zip_util . strip_archive_ext ( filename ) <EOL> response . set_header ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> else : <EOL> response . set_header ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> response . set_header ( '<STR_LIT>' , '<STR_LIT>' % filename ) <EOL> return fileobj <EOL> def request_accepts_gzip_encoding ( ) : <EOL> if '<STR_LIT>' not in request . headers : <EOL> return False <EOL> for encoding in request . headers [ '<STR_LIT>' ] . split ( '<STR_LIT:U+002C>' ) : <EOL> encoding = encoding . strip ( ) . split ( '<STR_LIT:;>' ) <EOL> if encoding [ <NUM_LIT:0> ] == '<STR_LIT>' : <EOL> if len ( encoding ) > <NUM_LIT:1> and encoding [ <NUM_LIT:1> ] == '<STR_LIT>' : <EOL> return False <EOL> else : <EOL> return True <EOL> return False </s>
