def fetch_icon(webapp, **kw): """Downloads a webapp icon from the location specified in the manifest. Returns False if icon was not able to be retrieved """ log.info(u'[1@None] Fetching icon for webapp %s.' % webapp.name) manifest = webapp.get_manifest_json() if not manifest or not 'icons' in manifest: # Set the icon type to empty. webapp.update(icon_type='') return try: biggest = max(int(size) for size in manifest['icons']) except ValueError: 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) else: if webapp.is_packaged: # Get icons from package. if icon_url.startswith('/'): icon_url = icon_url[1:] try: zf = SafeUnzip(webapp.get_latest_file().file_path) zf.is_valid() content = zf.extract_path(icon_url) except (KeyError, forms.ValidationError): # Not found in archive. 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, e: log.error(u'[Webapp:%s] Failed to fetch icon for webapp: %s' % (webapp, e.message)) # Set the icon type to empty. webapp.update(icon_type='') return False size_error_message = _('Your icon must be less than %s bytes.') 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
def fetch_icon(webapp, **kw): """Downloads a webapp icon from the location specified in the manifest. Returns False if icon was not able to be retrieved """ log.info(u'[1@None] Fetching icon for webapp %s.' % webapp.name) manifest = webapp.get_manifest_json() if not manifest or not 'icons' in manifest: # Set the icon type to empty. 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) else: if webapp.is_packaged: # Get icons from package. if icon_url.startswith('/'): icon_url = icon_url[1:] try: zf = SafeUnzip(webapp.get_latest_file().file_path) zf.is_valid() content = zf.extract_path(icon_url) except (KeyError, forms.ValidationError): # Not found in archive. 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, e: log.error(u'[Webapp:%s] Failed to fetch icon for webapp: %s' % (webapp, e)) # Set the icon type to empty. 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
def watermark_install_rdf(self, user): """ Reads the install_rdf out of an addon and writes the user information into it. """ inzip = SafeUnzip(self.file_path) inzip.is_valid() try: install = inzip.extract_path("install.rdf") data = RDF(install) data.set(user.email, self.version.addon.get_watermark_hash(user)) except Exception, e: log.error("Could not alter install.rdf in file: %s for %s, %s" % (self.pk, user.pk, e)) raise
def get_localepicker(self): """ For a file that is part of a language pack, extract the chrome/localepicker.properties file and return as a string. """ start = time.time() zip = SafeUnzip(self.file_path) if not zip.is_valid(fatal=False): return '' try: manifest = zip.extract_path('chrome.manifest') except KeyError, e: log.info('No file named: chrome.manifest in file: %s' % self.pk) return ''
def watermark_install_rdf(self, user): """ Reads the install_rdf out of an addon and writes the user information into it. """ inzip = SafeUnzip(self.file_path) inzip.is_valid() try: install = inzip.extract_path('install.rdf') data = RDF(install) data.set(user.email, self.version.addon.get_watermark_hash(user)) except Exception, e: log.error('Could not alter install.rdf in file: %s for %s, %s' % (self.pk, user.pk, e)) raise
def get_rdf(self, tmp): assert tmp unzip = SafeUnzip(tmp) unzip.is_valid() return RDF(unzip.extract_path('install.rdf'))
def test_extract_path(self): zip = SafeUnzip(self.xpi_path('langpack-localepicker')) assert zip.is_valid() assert'locale browser de' in zip.extract_path('chrome.manifest')
def test_extract_path(self): zip = SafeUnzip(self.xpi_path("langpack-localepicker")) assert zip.is_valid() assert "locale browser de" in zip.extract_path("chrome.manifest")