Example #1
0
    def from_upload(cls, upload, version, parse_data={}):
        upload.path = smart_path(nfd_str(upload.path))
        ext = os.path.splitext(upload.path)[1]

        f = cls(version=version)
        f.filename = f.generate_filename(extension=ext or '.zip')
        f.size = private_storage.size(upload.path)  # Size in bytes.
        f.status = mkt.STATUS_PENDING
        # Re-use the file-upload hash if we can, no need to regenerate a new
        # one if we can avoid that.
        f.hash = upload.hash or f.generate_hash(upload.path)
        f.save()

        log.debug('New file: %r from %r' % (f, upload))

        # Move the uploaded file from the temp location.
        copy_stored_file(
            upload.path,
            os.path.join(version.path_prefix, nfd_str(f.filename)),
            src_storage=private_storage,
            dst_storage=private_storage)

        if upload.validation:
            FileValidation.from_json(f, upload.validation)

        return f
Example #2
0
    def from_upload(cls, upload, version, parse_data={}):
        upload.path = smart_path(nfd_str(upload.path))
        ext = os.path.splitext(upload.path)[1]

        f = cls(version=version)
        f.filename = f.generate_filename(extension=ext or '.zip')
        f.size = private_storage.size(upload.path)  # Size in bytes.
        f.status = mkt.STATUS_PENDING
        # Re-use the file-upload hash if we can, no need to regenerate a new
        # one if we can avoid that.
        f.hash = upload.hash or f.generate_hash(upload.path)
        f.save()

        log.debug('New file: %r from %r' % (f, upload))

        # Move the uploaded file from the temp location.
        copy_stored_file(
            upload.path,
            os.path.join(version.path_prefix, nfd_str(f.filename)),
            src_storage=private_storage,
            dst_storage=private_storage)

        if upload.validation:
            FileValidation.from_json(f, upload.validation)

        return f
Example #3
0
    def handle_file_operations(self, upload):
        """Copy the file attached to a FileUpload to the Extension instance."""
        upload.path = smart_path(nfd_str(upload.path))

        if private_storage.exists(self.file_path):
            # The filename should not exist. If it does, it means we are trying
            # to re-upload the same version. This should have been caught
            # before, so just raise an exception.
            raise RuntimeError(
                'Trying to upload a file to a destination that already exists')

        # Copy file from fileupload. This uses private_storage for now as the
        # unreviewed, unsigned filename is private.
        copy_stored_file(
            upload.path, self.file_path,
            src_storage=private_storage, dst_storage=private_storage)
Example #4
0
    def handle_file_operations(self, upload):
        """Copy the file attached to a FileUpload to the Extension instance."""
        upload.path = smart_path(nfd_str(upload.path))

        if not self.slug:
            raise RuntimeError(
                'Trying to upload a file belonging to a slugless extension')

        if private_storage.exists(self.file_path):
            # The filename should not exist. If it does, it means we are trying
            # to re-upload the same version. This should have been caught
            # before, so just raise an exception.
            raise RuntimeError(
                'Trying to upload a file to a destination that already exists')

        # Copy file from fileupload. This uses private_storage for now as the
        # unreviewed, unsigned filename is private.
        copy_stored_file(upload.path, self.file_path)
Example #5
0
 def add_file(self, chunks, filename, size):
     filename = smart_str(filename)
     loc = os.path.join(settings.ADDONS_PATH, '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()
     # The buffer might have been read before, so rewind back at the start.
     if hasattr(chunks, 'seek'):
         chunks.seek(0)
     with private_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()
Example #6
0
 def add_file(self, chunks, filename, size):
     filename = smart_str(filename)
     loc = os.path.join(settings.ADDONS_PATH, '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()
     # The buffer might have been read before, so rewind back at the start.
     if hasattr(chunks, 'seek'):
         chunks.seek(0)
     with private_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()
Example #7
0
    def from_upload(cls, upload, version, parse_data={}):
        upload.path = smart_path(nfd_str(upload.path))
        ext = os.path.splitext(upload.path)[1]

        f = cls(version=version)
        f.filename = f.generate_filename(extension=ext or '.zip')
        f.size = storage.size(upload.path)  # Size in bytes.
        f.status = mkt.STATUS_PENDING
        f.hash = f.generate_hash(upload.path)
        f.save()

        log.debug('New file: %r from %r' % (f, upload))

        # Move the uploaded file from the temp location.
        copy_stored_file(upload.path, os.path.join(version.path_prefix,
                                                   nfd_str(f.filename)))
        if upload.validation:
            FileValidation.from_json(f, upload.validation)

        return f
Example #8
0
    def handle_file_operations(self, upload):
        """Handle file operations on an instance by using the FileUpload object
        passed to set filename, file_version on the LangPack instance, and
        moving the temporary file to its final destination."""
        upload.path = smart_path(nfd_str(upload.path))
        if not self.uuid:
            self.reset_uuid()
        if storage.exists(self.filename):
            # The filename should not exist. If it does, it means we are trying
            # to re-upload the same version. This should have been caught
            # before, so just raise an exception.
            raise RuntimeError(
                'Trying to upload a file to a destination that already exists')

        self.file_version = self.file_version + 1

        # Because we are only dealing with langpacks generated by Mozilla atm,
        # we can directly sign the file before copying it to its final
        # destination. The filename changes with the version, so when a new
        # file is uploaded we should still be able to serve the old one until
        # the new info is stored in the db.
        self.sign_and_move_file(upload)
Example #9
0
    def handle_file_operations(self, upload):
        """Handle file operations on an instance by using the FileUpload object
        passed to set filename, file_version on the LangPack instance, and
        moving the temporary file to its final destination."""
        upload.path = smart_path(nfd_str(upload.path))
        if not self.uuid:
            self.reset_uuid()
        if storage.exists(self.filename):
            # The filename should not exist. If it does, it means we are trying
            # to re-upload the same version. This should have been caught
            # before, so just raise an exception.
            raise RuntimeError(
                'Trying to upload a file to a destination that already exists')

        self.file_version = self.file_version + 1

        # Because we are only dealing with langpacks generated by Mozilla atm,
        # we can directly sign the file before copying it to its final
        # destination. The filename changes with the version, so when a new
        # file is uploaded we should still be able to serve the old one until
        # the new info is stored in the db.
        self.sign_and_move_file(upload)