@task
@use_master
def fetch_icon(pk, file_pk=None, **kw):
 webapp = Webapp.objects.get(pk=pk)
 log.info((u'[1@None] Fetching icon for webapp %s.' % webapp.name))
 if file_pk:
  file_obj = File.objects.get(pk=file_pk)
 else:
  file_obj = (webapp.current_version and webapp.current_version.all_files[0])
 manifest = webapp.get_manifest_json(file_obj)
 if ((not manifest) or ('icons' not in manifest)):
  webapp.update(icon_type='')
  return
 try:
  biggest = max((int(size) for size in manifest['icons']))
 except ValueError:
  log.error(('No icon to fetch for webapp "%s"' % webapp.name))
  return False
 icon_url = manifest['icons'][str(biggest)]
 if icon_url.startswith('data:image'):
  image_string = icon_url.split('base64,')[1]
  content = base64.decodestring(image_string)
 elif webapp.is_packaged:
  if icon_url.startswith('/'):
   icon_url = icon_url[1:]
  try:
   zf = SafeUnzip(private_storage.open(file_obj.file_path))
   zf.is_valid()
   content = zf.extract_path(icon_url)
  except (KeyError, forms.ValidationError):
   log.error((u'[Webapp:%s] Icon %s not found in archive' % (webapp, icon_url)))
   return False
 else:
  if (not urlparse.urlparse(icon_url).scheme):
   icon_url = (webapp.origin + icon_url)
  try:
   response = _fetch_content(icon_url)
  except Exception as e:
   log.error((u'[Webapp:%s] Failed to fetch icon for webapp: %s' % (webapp, e)))
   webapp.update(icon_type='')
   return False
  try:
   content = get_content_and_check_size(response, settings.MAX_ICON_UPLOAD_SIZE)
  except ResponseTooLargeException:
   log.warning((u'[Webapp:%s] Icon exceeds maximum size.' % webapp))
   return False
 log.info(('Icon fetching completed for app "%s"; saving icon' % webapp.name))
 save_icon(webapp, content)

@ task @ use_master def fetch_icon ( pk , file_pk = None , ** kw ) : webapp = Webapp . objects . get ( pk = pk ) log . info ( ( u'[1@None] Fetching icon for webapp %s.' % webapp . name ) ) if file_pk : file_obj = File . objects . get ( pk = file_pk ) else : file_obj = ( webapp . current_version and webapp . current_version . all_files [ 0 ] ) manifest = webapp . get_manifest_json ( file_obj ) if ( ( not manifest ) or ( 'icons' not in manifest ) ) : webapp . update ( icon_type = '' ) return try : biggest = max ( ( int ( size ) for size in manifest [ 'icons' ] ) ) except ValueError : log . error ( ( 'No icon to fetch for webapp "%s"' % webapp . name ) ) return False icon_url = manifest [ 'icons' ] [ str ( biggest ) ] if icon_url . startswith ( 'data:image' ) : image_string = icon_url . split ( 'base64,' ) [ 1 ] content = base64 . decodestring ( image_string ) elif webapp . is_packaged : if icon_url . startswith ( '/' ) : icon_url = icon_url [ 1 : ] try : zf = SafeUnzip ( private_storage . open ( file_obj . file_path ) ) zf . is_valid ( ) content = zf . extract_path ( icon_url ) except ( KeyError , forms . ValidationError ) : log . error ( ( u'[Webapp:%s] Icon %s not found in archive' % ( webapp , icon_url ) ) ) return False else : if ( not urlparse . urlparse ( icon_url ) . scheme ) : icon_url = ( webapp . origin + icon_url ) try : response = _fetch_content ( icon_url ) except Exception as e : log . error ( ( u'[Webapp:%s] Failed to fetch icon for webapp: %s' % ( webapp , e ) ) ) webapp . update ( icon_type = '' ) return False try : content = get_content_and_check_size ( response , settings . MAX_ICON_UPLOAD_SIZE ) except ResponseTooLargeException : log . warning ( ( u'[Webapp:%s] Icon exceeds maximum size.' % webapp ) ) return False log . info ( ( 'Icon fetching completed for app "%s"; saving icon' % webapp . name ) ) save_icon ( webapp , content )
take an extension pk and extract 128x128 icon from its zip file .