def add_file(self, chunks, filename, size): if not self.uuid: self.uuid = self._meta.get_field('uuid')._create_uuid().hex filename = smart_str(u'{0}_{1}'.format(self.uuid, filename)) loc = os.path.join(user_media_path('addons'), 'temp', uuid.uuid4().hex) base, ext = os.path.splitext(smart_path(filename)) # Change a ZIP to an XPI, to maintain backward compatibility # with older versions of Firefox and to keep the rest of the XPI code # path as consistent as possible for ZIP uploads. # See: https://github.com/mozilla/addons-server/pull/2785 if ext == '.zip': ext = '.xpi' if ext in EXTENSIONS: loc += ext log.info('UPLOAD: %r (%s bytes) to %r' % (filename, size, loc)) hash = hashlib.sha256() with storage.open(loc, 'wb') as fd: for chunk in chunks: hash.update(chunk) fd.write(chunk) self.path = loc self.name = filename self.hash = 'sha256:%s' % hash.hexdigest() self.save()
def from_upload(cls, upload, version, platform, is_beta=False, parse_data={}): addon = version.addon file_ = cls(version=version, platform=platform) upload.path = smart_path(nfd_str(upload.path)) ext = os.path.splitext(upload.path)[1] if ext == '.jar': ext = '.xpi' file_.filename = file_.generate_filename(extension=ext or '.xpi') # Size in bytes. file_.size = storage.size(upload.path) data = cls.get_jetpack_metadata(upload.path) if 'sdkVersion' in data and data['sdkVersion']: file_.jetpack_version = data['sdkVersion'][:10] file_.no_restart = parse_data.get('no_restart', False) file_.strict_compatibility = parse_data.get('strict_compatibility', False) file_.is_multi_package = parse_data.get('is_multi_package', False) file_.is_experiment = parse_data.get('is_experiment', False) file_.is_webextension = parse_data.get('is_webextension', False) if is_beta and addon.status == amo.STATUS_PUBLIC: file_.status = amo.STATUS_BETA file_.hash = file_.generate_hash(upload.path) file_.original_hash = file_.hash if upload.validation: validation = json.loads(upload.validation) if validation['metadata'].get('requires_chrome'): file_.requires_chrome = True file_.save() log.debug('New file: %r from %r' % (file_, upload)) # Move the uploaded file from the temp location. destinations = [version.path_prefix] if file_.status in amo.MIRROR_STATUSES: destinations.append(version.mirror_path_prefix) for dest in destinations: copy_stored_file(upload.path, os.path.join(dest, nfd_str(file_.filename))) if upload.validation: # Import loop. from olympia.devhub.tasks import annotate_validation_results from olympia.devhub.utils import ValidationAnnotator validation = annotate_validation_results(validation) FileValidation.from_json(file_, validation) # Copy annotations from any previously approved file. ValidationAnnotator(file_).update_annotations() return file_
def from_upload(cls, upload, version, platform, is_beta=False, parse_data={}): addon = version.addon file_ = cls(version=version, platform=platform) upload.path = smart_path(nfd_str(upload.path)) ext = os.path.splitext(upload.path)[1] if ext == '.jar': ext = '.xpi' file_.filename = file_.generate_filename(extension=ext or '.xpi') # Size in bytes. file_.size = storage.size(upload.path) data = cls.get_jetpack_metadata(upload.path) if 'sdkVersion' in data and data['sdkVersion']: file_.jetpack_version = data['sdkVersion'][:10] if file_.jetpack_version: Tag(tag_text='jetpack').save_tag(addon) file_.no_restart = parse_data.get('no_restart', False) file_.strict_compatibility = parse_data.get('strict_compatibility', False) file_.is_multi_package = parse_data.get('is_multi_package', False) file_.is_experiment = parse_data.get('is_experiment', False) file_.is_webextension = parse_data.get('is_webextension', False) if is_beta and addon.status == amo.STATUS_PUBLIC: file_.status = amo.STATUS_BETA file_.hash = file_.generate_hash(upload.path) file_.original_hash = file_.hash if upload.validation: validation = json.loads(upload.validation) if validation['metadata'].get('requires_chrome'): file_.requires_chrome = True file_.save() log.debug('New file: %r from %r' % (file_, upload)) # Move the uploaded file from the temp location. destinations = [version.path_prefix] if file_.status in amo.MIRROR_STATUSES: destinations.append(version.mirror_path_prefix) for dest in destinations: copy_stored_file(upload.path, os.path.join(dest, nfd_str(file_.filename))) if upload.validation: # Import loop. from olympia.devhub.tasks import annotate_validation_results from olympia.devhub.utils import ValidationAnnotator validation = annotate_validation_results(validation) FileValidation.from_json(file_, validation) # Copy annotations from any previously approved file. ValidationAnnotator(file_).update_annotations() return file_
def add_file(self, chunks, filename, size): if not self.uuid: self.uuid = self._meta.get_field('uuid')._create_uuid().hex filename = smart_str(u'{0}_{1}'.format(self.uuid, filename)) loc = os.path.join(user_media_path('addons'), 'temp', uuid.uuid4().hex) base, ext = os.path.splitext(smart_path(filename)) if ext in EXTENSIONS: loc += ext log.info('UPLOAD: %r (%s bytes) to %r' % (filename, size, loc)) hash = hashlib.sha256() with storage.open(loc, 'wb') as fd: for chunk in chunks: hash.update(chunk) fd.write(chunk) self.path = loc self.name = filename self.hash = 'sha256:%s' % hash.hexdigest() self.save()
def add_file(self, chunks, filename, size): if not self.uuid: self.uuid = self._meta.get_field('uuid')._create_uuid().hex filename = smart_str(u'{0}_{1}'.format(self.uuid, filename)) loc = os.path.join(user_media_path('addons'), 'temp', uuid.uuid4().hex) base, ext = os.path.splitext(smart_path(filename)) is_crx = False # Change a ZIP to an XPI, to maintain backward compatibility # with older versions of Firefox and to keep the rest of the XPI code # path as consistent as possible for ZIP uploads. # See: https://github.com/mozilla/addons-server/pull/2785 if ext == '.zip': ext = '.xpi' # If the extension is a CRX, we need to do some actual work to it # before we just convert it to an XPI. We strip the header from the # CRX, then it's good; see more about the CRX file format here: # https://developer.chrome.com/extensions/crx if ext == '.crx': ext = '.xpi' is_crx = True if ext in EXTENSIONS: loc += ext log.info('UPLOAD: %r (%s bytes) to %r' % (filename, size, loc)) if is_crx: hash = write_crx_as_xpi(chunks, storage, loc) else: hash = hashlib.sha256() with storage.open(loc, 'wb') as file_destination: for chunk in chunks: hash.update(chunk) file_destination.write(chunk) self.path = loc self.name = filename self.hash = 'sha256:%s' % hash.hexdigest() self.save()
def add_file(self, chunks, filename, size): if not self.uuid: self.uuid = self._meta.get_field('uuid')._create_uuid() filename = force_bytes(u'{0}_{1}'.format(self.uuid.hex, filename)) loc = os.path.join(user_media_path('addons'), 'temp', uuid.uuid4().hex) base, ext = os.path.splitext(smart_path(filename)) is_crx = False # Change a ZIP to an XPI, to maintain backward compatibility # with older versions of Firefox and to keep the rest of the XPI code # path as consistent as possible for ZIP uploads. # See: https://github.com/mozilla/addons-server/pull/2785 if ext == '.zip': ext = '.xpi' # If the extension is a CRX, we need to do some actual work to it # before we just convert it to an XPI. We strip the header from the # CRX, then it's good; see more about the CRX file format here: # https://developer.chrome.com/extensions/crx if ext == '.crx': ext = '.xpi' is_crx = True if ext in EXTENSIONS: loc += ext log.info('UPLOAD: %r (%s bytes) to %r' % (filename, size, loc)) if is_crx: hash = write_crx_as_xpi(chunks, storage, loc) else: hash = hashlib.sha256() with storage.open(loc, 'wb') as file_destination: for chunk in chunks: hash.update(chunk) file_destination.write(chunk) self.path = loc self.name = filename self.hash = 'sha256:%s' % hash.hexdigest() self.save()