<s> """<STR_LIT>""" <EOL> __author__ = '<STR_LIT>' <EOL> __copyright__ = '<STR_LIT>' <EOL> __licence__ = '<STR_LIT>' <EOL> __email__ = '<STR_LIT>' <EOL> from django . core . exceptions import ValidationError <EOL> from django . conf import settings <EOL> from ninecms . utils . transliterate import transliterate <EOL> from subprocess import check_output , call , CalledProcessError <EOL> import os <EOL> def path_file_name ( instance , context , filename ) : <EOL> """<STR_LIT>""" <EOL> page_type_name = transliterate ( instance . node . page_type . name , True , True ) <EOL> context = transliterate ( context , True , True ) <EOL> group = transliterate ( instance . group , True , True ) <EOL> filename = transliterate ( filename , True , True ) <EOL> return os . path . join ( * filter ( None , ( '<STR_LIT>' , page_type_name , context , group , filename ) ) ) <EOL> def image_path_file_name ( instance , filename ) : <EOL> """<STR_LIT>""" <EOL> return path_file_name ( instance , '<STR_LIT:image>' , filename ) <EOL> def file_path_file_name ( instance , filename ) : <EOL> """<STR_LIT>""" <EOL> return path_file_name ( instance , '<STR_LIT:file>' , filename ) <EOL> def video_path_file_name ( instance , filename ) : <EOL> """<STR_LIT>""" <EOL> return path_file_name ( instance , '<STR_LIT>' , filename ) <EOL> def validate_ext ( value , valid ) : <EOL> """<STR_LIT>""" <EOL> ext = os . path . splitext ( value . name ) [ <NUM_LIT:1> ] <EOL> if ext not in valid : <EOL> raise ValidationError ( "<STR_LIT>" ) <EOL> def validate_file_ext ( value ) : <EOL> """<STR_LIT>""" <EOL> validate_ext ( value , [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> def validate_video_ext ( value ) : <EOL> """<STR_LIT>""" <EOL> validate_ext ( value , [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> def find_all ( filename ) : <EOL> """<STR_LIT>""" <EOL> path = os . path . dirname ( filename ) <EOL> name = os . path . basename ( filename ) <EOL> result = [ ] <EOL> for root , dirs , files in os . walk ( path ) : <EOL> if name in files : <EOL> result . append ( os . path . join ( root , name ) ) <EOL> return result <EOL> def delete_all ( filename ) : <EOL> """<STR_LIT>""" <EOL> for file in find_all ( filename ) : <EOL> os . remove ( file ) <EOL> def image_style ( image , style ) : <EOL> """<STR_LIT>""" <EOL> if not image : <EOL> return image <EOL> url = image . url <EOL> url_path = '<STR_LIT:/>' . join ( url . split ( '<STR_LIT:/>' ) [ : - <NUM_LIT:1> ] ) <EOL> img_path_file_name = str ( image . file ) <EOL> img_file_name = os . path . basename ( img_path_file_name ) <EOL> style_url_path = '<STR_LIT:/>' . join ( ( url_path , style ) ) <EOL> style_url = '<STR_LIT:/>' . join ( ( style_url_path , img_file_name ) ) <EOL> style_path = os . path . join ( os . path . dirname ( img_path_file_name ) , style ) <EOL> style_path_file_name = os . path . join ( style_path , img_file_name ) <EOL> style_def = settings . IMAGE_STYLES [ style ] <EOL> if not os . path . exists ( style_path_file_name ) : <EOL> if not os . path . exists ( style_path ) : <EOL> os . makedirs ( style_path ) <EOL> by = chr ( <NUM_LIT> ) <EOL> plus = chr ( <NUM_LIT> ) <EOL> try : <EOL> source_size_str = check_output ( [ '<STR_LIT>' , img_path_file_name ] ) . decode ( ) <EOL> except CalledProcessError : <EOL> return url <EOL> source_size_str = source_size_str [ len ( img_path_file_name ) : ] . split ( '<STR_LIT:U+0020>' ) [ <NUM_LIT:2> ] <EOL> source_size_array = source_size_str . split ( by ) <EOL> source_size_x = int ( source_size_array [ <NUM_LIT:0> ] ) <EOL> source_size_y = int ( source_size_array [ <NUM_LIT:1> ] ) <EOL> target_size_x = style_def [ '<STR_LIT:size>' ] [ <NUM_LIT:0> ] <EOL> target_size_y = style_def [ '<STR_LIT:size>' ] [ <NUM_LIT:1> ] <EOL> target_size_str = str ( target_size_x ) + by + str ( target_size_y ) <EOL> if style_def [ '<STR_LIT:type>' ] == '<STR_LIT>' : <EOL> if target_size_x > source_size_x and target_size_y > source_size_y : <EOL> target_size_str = source_size_str <EOL> call ( [ '<STR_LIT>' , img_path_file_name , '<STR_LIT>' , target_size_str , '<STR_LIT>' , style_path_file_name ] ) <EOL> elif style_def [ '<STR_LIT:type>' ] == '<STR_LIT>' : <EOL> call ( [ '<STR_LIT>' , img_path_file_name , '<STR_LIT>' , target_size_str , '<STR_LIT>' , style_path_file_name ] ) <EOL> elif style_def [ '<STR_LIT:type>' ] == '<STR_LIT>' : <EOL> source_ratio = float ( source_size_x ) / float ( source_size_y ) <EOL> target_ratio = float ( target_size_x ) / float ( target_size_y ) <EOL> if source_ratio > target_ratio : <EOL> crop_target_size_x = source_size_y * target_ratio <EOL> crop_target_size_y = source_size_y <EOL> offset = ( source_size_x - crop_target_size_x ) / <NUM_LIT:2> <EOL> crop_size_str = str ( crop_target_size_x ) + by + str ( crop_target_size_y ) + plus + str ( offset ) + plus + '<STR_LIT:0>' <EOL> else : <EOL> crop_target_size_x = source_size_x <EOL> crop_target_size_y = source_size_x / target_ratio <EOL> offset = ( source_size_y - crop_target_size_y ) / <NUM_LIT:2> <EOL> crop_size_str = str ( crop_target_size_x ) + by + str ( crop_target_size_y ) + plus + '<STR_LIT:0>' + plus + str ( offset ) <EOL> call ( [ '<STR_LIT>' , img_path_file_name , '<STR_LIT>' , crop_size_str , style_path_file_name ] ) <EOL> call ( [ '<STR_LIT>' , style_path_file_name , '<STR_LIT>' , target_size_str , '<STR_LIT>' , style_path_file_name ] ) <EOL> return style_url </s>
