#NEWLINE# def put_file( self , key , file , * args , **kwargs ) : #NEWLINE# #INDENT# bufsize = ( 1024 * 1024 ) #NEWLINE# phash = self . hashfunc ( ) #NEWLINE# if ( not key ) : #NEWLINE# #INDENT# if isinstance ( file , str ) : #NEWLINE# #INDENT# with open ( file , 'rb' ) as source : #NEWLINE# #INDENT# while True : #NEWLINE# #INDENT# buf = source . read ( bufsize ) #NEWLINE# phash . update ( buf ) #NEWLINE# if ( len ( buf ) < bufsize ) : #NEWLINE# #INDENT# break #NEWLINE# #UNINDENT# #UNINDENT# return self . _dstore . put_file ( self . _template . format ( phash . hexdigest ( ) ) , file , * args , ** kwargs ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# tmpfile = tempfile . NamedTemporaryFile ( delete = False ) #NEWLINE# try : #NEWLINE# #INDENT# while True : #NEWLINE# #INDENT# buf = file . read ( bufsize ) #NEWLINE# phash . update ( buf ) #NEWLINE# tmpfile . write ( buf ) #NEWLINE# if ( len ( buf ) < bufsize ) : #NEWLINE# #INDENT# break #NEWLINE# #UNINDENT# #UNINDENT# tmpfile . close ( ) #NEWLINE# return self . _dstore . put_file ( self . _template . format ( phash . hexdigest ( ) ) , bufsize . name , * args , ** kwargs ) #NEWLINE# #UNINDENT# finally : #NEWLINE# #INDENT# try : #NEWLINE# #INDENT# os . unlink ( tmpfile . name ) #NEWLINE# #UNINDENT# except OSError as e : #NEWLINE# #INDENT# if ( 2 == e . errno ) : #NEWLINE# #INDENT# pass #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# raise #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# return self . _dstore . put_file ( key , file , * args , ** kwargs )