<s> import base64 <EOL> import os <EOL> from io import BytesIO <EOL> import requests <EOL> from thumbnails . conf import settings <EOL> from thumbnails . helpers import get_engine , get_storage_backend <EOL> class Thumbnail ( object ) : <EOL> size = None , None <EOL> image = None <EOL> _url = None <EOL> def __init__ ( self , name ) : <EOL> self . name = '<STR_LIT:/>' . join ( name ) <EOL> @ property <EOL> def path ( self ) : <EOL> return os . path . join ( settings . THUMBNAIL_PATH , '<STR_LIT>' . format ( self . name , self . extension ) ) <EOL> @ property <EOL> def url ( self ) : <EOL> if self . _url : <EOL> return self . _url <EOL> return '<STR_LIT:/>' . join ( [ settings . THUMBNAIL_URL , '<STR_LIT>' . format ( self . name , self . extension ) ] ) . replace ( '<STR_LIT>' , '<STR_LIT:/>' ) <EOL> @ property <EOL> def extension ( self ) : <EOL> return '<STR_LIT>' <EOL> @ property <EOL> def width ( self ) : <EOL> return self . size [ <NUM_LIT:0> ] <EOL> @ property <EOL> def height ( self ) : <EOL> return self . size [ <NUM_LIT:1> ] <EOL> @ property <EOL> def ratio ( self ) : <EOL> return float ( self . width ) / float ( self . height ) <EOL> @ property <EOL> def is_portrait ( self ) : <EOL> return self . ratio < <NUM_LIT:1> <EOL> @ property <EOL> def is_landscape ( self ) : <EOL> return self . ratio > <NUM_LIT:1> <EOL> @ property <EOL> def exists ( self ) : <EOL> return get_storage_backend ( ) . exists ( self . path ) <EOL> def save ( self , options ) : <EOL> return get_storage_backend ( ) . save ( self . path , get_engine ( ) . raw_data ( self . image , options ) ) <EOL> def alternative_resolution_path ( self , resolution ) : <EOL> return os . path . join ( <EOL> settings . THUMBNAIL_PATH , <EOL> '<STR_LIT>' . format ( self . name , resolution , self . extension ) <EOL> ) <EOL> def save_alternative_resolution ( self , resolution , image , options ) : <EOL> path = self . alternative_resolution_path ( resolution ) <EOL> return get_storage_backend ( ) . save ( path , get_engine ( ) . raw_data ( image , options ) ) <EOL> class SourceFile ( object ) : <EOL> def __init__ ( self , source_file ) : <EOL> if hasattr ( source_file , '<STR_LIT:name>' ) : <EOL> self . file = source_file . name <EOL> else : <EOL> self . file = source_file <EOL> def open ( self ) : <EOL> if self . file . startswith ( '<STR_LIT:http>' ) : <EOL> return requests . get ( self . file , stream = True ) . raw <EOL> elif self . file . startswith ( r'<STR_LIT>' ) : <EOL> return BytesIO ( base64 . b64decode ( self . file . replace ( '<STR_LIT>' , '<STR_LIT>' ) ) ) <EOL> return get_storage_backend ( ) . open ( self . file ) </s>
