<s> import os , hashlib , errno <EOL> from django . utils . encoding import force_text , force_bytes <EOL> from storages . compat import FileSystemStorage <EOL> class HashPathStorage ( FileSystemStorage ) : <EOL> """<STR_LIT>""" <EOL> def save ( self , name , content , max_length = None ) : <EOL> if name is None : <EOL> name = content . name <EOL> sha1 = hashlib . sha1 ( ) <EOL> for chunk in content . chunks ( ) : <EOL> sha1 . update ( force_bytes ( chunk ) ) <EOL> sha1sum = sha1 . hexdigest ( ) <EOL> name = os . path . join ( os . path . split ( name ) [ <NUM_LIT:0> ] , sha1sum [ : <NUM_LIT:1> ] , sha1sum [ <NUM_LIT:1> : <NUM_LIT:2> ] , sha1sum ) <EOL> dir_name , file_name = os . path . split ( name ) <EOL> if self . exists ( name ) : <EOL> return name <EOL> try : <EOL> os . makedirs ( os . path . join ( self . location , dir_name ) ) <EOL> except OSError as e : <EOL> if e . errno is not errno . EEXIST : <EOL> raise e <EOL> name = self . _save ( name , content ) <EOL> return force_text ( name . replace ( '<STR_LIT:\\>' , '<STR_LIT:/>' ) ) </s>
