当前位置: 首页>>代码示例>>Python>>正文


Python engines.BaseEngine类代码示例

本文整理汇总了Python中thumbor.engines.BaseEngine的典型用法代码示例。如果您正苦于以下问题:Python BaseEngine类的具体用法?Python BaseEngine怎么用?Python BaseEngine使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了BaseEngine类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: post

    def post(self):
        # Check if the image uploaded is a multipart/form-data
        if self.multipart_form_data():
            file_data = self.request.files['media'][0]
            body = file_data['body']

            # Retrieve filename from 'filename' field
            filename = file_data['filename']
        else:
            body = self.request.body

            # Retrieve filename from 'Slug' header
            filename = self.request.headers.get('Slug')

        # Check if the image uploaded is valid
        if self.validate(body):

            # Use the default filename for the uploaded images
            if not filename:
                content_type = self.request.headers.get('Content-Type', BaseEngine.get_mimetype(body))
                extension = mimetypes.guess_extension(content_type.split(';',1)[0], False)
                if extension is None: # Content-Type is unknown, try with body
                    extension = mimetypes.guess_extension(BaseEngine.get_mimetype(body), False)
                if extension == '.jpe':
                     extension = '.jpg'  # Hack because mimetypes return .jpe by default
                if extension is None: # Even body is unknown, return an empty string to be contat
                    extension = ''
                filename = self.context.config.UPLOAD_DEFAULT_FILENAME + extension

            # Build image id based on a random uuid (32 characters)
            id = str(uuid.uuid4().hex)
            self.write_file(id, body)
            self.set_status(201)
            self.set_header('Location', self.location(id, filename))
开发者ID:APSL,项目名称:thumbor,代码行数:34,代码来源:upload.py

示例2: _fetch

    def _fetch(self, url):
        storage = self.context.modules.storage
        buffer = yield gen.maybe_future(storage.get(url))
        mime = None

        if buffer is not None:
            self.context.statsd_client.incr('storage.hit')
            mime = BaseEngine.get_mimetype(buffer)
            self.context.request.extension = EXTENSION.get(mime, '.jpg')
            if mime == 'image/gif' and self.context.config.USE_GIFSICLE_ENGINE:
                self.context.request.engine = self.context.modules.gif_engine
            else:
                self.context.request.engine = self.context.modules.engine

            raise gen.Return([False, buffer, None])
        else:
            self.context.statsd_client.incr('storage.miss')

        buffer = yield self.context.modules.loader.load(self.context, url)

        if buffer is None:
            raise gen.Return([False, None, None])

        if mime is None:
            mime = BaseEngine.get_mimetype(buffer)

        if mime is None:
            raise gen.Return([False, None, None])

        self.context.request.extension = EXTENSION.get(mime, '.jpg')

        original_preserve = self.context.config.PRESERVE_EXIF_INFO
        self.context.config.PRESERVE_EXIF_INFO = True

        try:
            mime = BaseEngine.get_mimetype(buffer)
            self.context.request.extension = extension = EXTENSION.get(mime, None)

            if mime == 'image/gif' and self.context.config.USE_GIFSICLE_ENGINE:
                self.context.request.engine = self.context.modules.gif_engine
            else:
                self.context.request.engine = self.context.modules.engine

            self.context.request.engine.load(buffer, extension)
            normalized = self.context.request.engine.normalize()
            is_no_storage = isinstance(storage, NoStorage)
            is_mixed_storage = isinstance(storage, MixedStorage)
            is_mixed_no_file_storage = is_mixed_storage and isinstance(storage.file_storage, NoStorage)

            if not (is_no_storage or is_mixed_no_file_storage):
                buffer = self.context.request.engine.read(extension)
                storage.put(url, buffer)

            storage.put_crypto(url)
        finally:
            self.context.config.PRESERVE_EXIF_INFO = original_preserve

        raise gen.Return([normalized, None, self.context.request.engine])
开发者ID:lfalcao,项目名称:thumbor,代码行数:58,代码来源:__init__.py

示例3: define_image_type

    def define_image_type(self, context, result):
        if result is not None:
            image_extension = BaseEngine.get_mimetype(result)
        elif context.config.AUTO_WEBP and context.request.accepts_webp and not context.modules.engine.is_multiple():
            image_extension = ".webp"
        else:
            image_extension = context.request.format
            if image_extension is None:
                image_extension = context.modules.engine.extension
                logger.debug("No image format specified. Retrieving from the image extension: %s." % image_extension)
            else:
                image_extension = ".%s" % image_extension
                logger.debug("Image format specified as %s." % image_extension)

        content_type = CONTENT_TYPE.get(image_extension, CONTENT_TYPE[".jpg"])

        if context.request.meta:
            context.request.meta_callback = (
                context.config.META_CALLBACK_NAME or self.request.arguments.get("callback", [None])[0]
            )
            content_type = "text/javascript" if context.request.meta_callback else "application/json"
            logger.debug("Metadata requested. Serving content type of %s." % content_type)

        logger.debug("Content Type of %s detected." % content_type)

        return image_extension, content_type
开发者ID:rkmax,项目名称:thumbor,代码行数:26,代码来源:__init__.py

示例4: define_image_type

    def define_image_type(self, context, result):
        if result is not None:
            if isinstance(result, ResultStorageResult):
                buffer = result.buffer
            else:
                buffer = result
            image_extension = EXTENSION.get(BaseEngine.get_mimetype(buffer), ".jpg")
        else:
            image_extension = context.request.format
            if image_extension is not None:
                image_extension = ".%s" % image_extension
                logger.debug("Image format specified as %s." % image_extension)
            elif self.is_webp(context):
                image_extension = ".webp"
                logger.debug("Image format set by AUTO_WEBP as %s." % image_extension)
            else:
                image_extension = context.request.engine.extension
                logger.debug("No image format specified. Retrieving from the image extension: %s." % image_extension)

        content_type = CONTENT_TYPE.get(image_extension, CONTENT_TYPE[".jpg"])

        if context.request.meta:
            context.request.meta_callback = (
                context.config.META_CALLBACK_NAME or self.request.arguments.get("callback", [None])[0]
            )
            content_type = "text/javascript" if context.request.meta_callback else "application/json"
            logger.debug("Metadata requested. Serving content type of %s." % content_type)

        logger.debug("Content Type of %s detected." % content_type)

        return (image_extension, content_type)
开发者ID:nyimbi,项目名称:thumbor,代码行数:31,代码来源:__init__.py

示例5: put

    def put(self, path, data, metadata={}, reduced_redundancy=False, encrypt_key=False, callback=None):
        """
        Stores data at given path
        :param string path: Path or 'key' for created/updated object
        :param bytes data: Data to write
        :param dict metadata: Metadata to store with this data
        :param bool reduced_redundancy: Whether to reduce storage redundancy or not?
        :param bool encrypt_key: Encrypt data?
        :param callable callback: Called function once done
        """
        storage_class = 'REDUCED_REDUNDANCY' if reduced_redundancy else 'STANDARD'
        content_type = BaseEngine.get_mimetype(data) or 'application/octet-stream'

        args = dict(
            callback=callback,
            Bucket=self._bucket,
            Key=self._clean_key(path),
            Body=data,
            ContentType=content_type,
            Metadata=metadata,
            StorageClass=storage_class,
        )

        if encrypt_key:
            args['ServerSideEncryption'] = 'AES256'

        self._put_client.call(**args)
开发者ID:thumbor-community,项目名称:aws,代码行数:27,代码来源:bucket.py

示例6: define_image_type

    def define_image_type(self, context, result):
        if result is not None:
            image_extension = BaseEngine.get_mimetype(result)
        else:
            image_extension = context.request.format
            if image_extension is not None:
                image_extension = '.%s' % image_extension
                logger.debug('Image format specified as %s.' % image_extension)
            elif context.config.AUTO_WEBP and context.request.accepts_webp and not context.request.engine.is_multiple():
                image_extension = '.webp'
                logger.debug('Image format set by AUTO_WEBP as %s.' % image_extension)
            else:
                image_extension = context.request.engine.extension
                logger.debug('No image format specified. Retrieving from the image extension: %s.' % image_extension)

        content_type = CONTENT_TYPE.get(image_extension, CONTENT_TYPE['.jpg'])

        if context.request.meta:
            context.request.meta_callback = context.config.META_CALLBACK_NAME or self.request.arguments.get('callback', [None])[0]
            content_type = 'text/javascript' if context.request.meta_callback else 'application/json'
            logger.debug('Metadata requested. Serving content type of %s.' % content_type)

        logger.debug('Content Type of %s detected.' % content_type)

        return image_extension, content_type
开发者ID:SONIFI,项目名称:thumbor,代码行数:25,代码来源:__init__.py

示例7: handle_loader_loaded

            def handle_loader_loaded(buffer):
                if buffer is None:
                    callback(False, None)
                    return

                original_preserve = self.context.config.PRESERVE_EXIF_INFO
                self.context.config.PRESERVE_EXIF_INFO = True

                try:
                    mime = BaseEngine.get_mimetype(buffer)

                    if mime == '.gif' and self.context.config.USE_GIFSICLE_ENGINE:
                        self.context.request.engine = GifEngine(self.context)
                    else:
                        self.context.request.engine = self.context.modules.engine

                    self.context.request.engine.load(buffer, extension)
                    normalized = self.context.request.engine.normalize()
                    is_no_storage = isinstance(storage, NoStorage)
                    is_mixed_storage = isinstance(storage, MixedStorage)
                    is_mixed_no_file_storage = is_mixed_storage and isinstance(storage.file_storage, NoStorage)

                    if not (is_no_storage or is_mixed_no_file_storage):
                        buffer = self.context.request.engine.read()
                        storage.put(url, buffer)

                    storage.put_crypto(url)
                finally:
                    self.context.config.PRESERVE_EXIF_INFO = original_preserve

                callback(normalized, engine=self.context.request.engine)
开发者ID:SONIFI,项目名称:thumbor,代码行数:31,代码来源:__init__.py

示例8: get

    def get(self, callback):
        path = self.context.request.url
        file_abspath = self.normalize_path(path)
        if not self.validate_path(file_abspath):
            logger.warn("[RESULT_STORAGE] unable to read from outside root path: %s" % file_abspath)
            return None
        logger.debug("[RESULT_STORAGE] getting from %s" % file_abspath)

        if not exists(file_abspath) or self.is_expired(file_abspath):
            logger.debug("[RESULT_STORAGE] image not found at %s" % file_abspath)
            callback(None)
        else:
            with open(file_abspath, 'r') as f:
                buffer = f.read()

            result = ResultStorageResult(
                buffer=buffer,
                metadata={
                    'LastModified':  datetime.fromtimestamp(getmtime(file_abspath)).replace(tzinfo=pytz.utc),
                    'ContentLength': len(buffer),
                    'ContentType':   BaseEngine.get_mimetype(buffer)
                }
            )

            callback(result)
开发者ID:RealGeeks,项目名称:thumbor,代码行数:25,代码来源:file_storage.py

示例9: execute_image_operations

    def execute_image_operations(self):
        self.context.request.quality = None

        req = self.context.request
        conf = self.context.config

        req.extension = splitext(req.image_url)[-1].lower()

        should_store = self.context.config.RESULT_STORAGE_STORES_UNSAFE or not self.context.request.unsafe
        if self.context.modules.result_storage and should_store:
            result = self.context.modules.result_storage.get()
            if result is not None:
                mime = BaseEngine.get_mimetype(result)
                if mime == '.gif' and self.context.config.USE_GIFSICLE_ENGINE:
                    self.context.request.engine = GifEngine(self.context)
                else:
                    self.context.request.engine = self.context.modules.engine

                logger.debug('[RESULT_STORAGE] IMAGE FOUND: %s' % req.url)
                self.finish_request(self.context, result)
                return

        if conf.MAX_WIDTH and (not isinstance(req.width, basestring)) and req.width > conf.MAX_WIDTH:
            req.width = conf.MAX_WIDTH
        if conf.MAX_HEIGHT and (not isinstance(req.height, basestring)) and req.height > conf.MAX_HEIGHT:
            req.height = conf.MAX_HEIGHT

        req.meta_callback = conf.META_CALLBACK_NAME or self.request.arguments.get('callback', [None])[0]

        self.filters_runner = self.context.filters_factory.create_instances(self.context, self.context.request.filters)
        self.filters_runner.apply_filters(thumbor.filters.PHASE_PRE_LOAD, self.get_image)
开发者ID:SONIFI,项目名称:thumbor,代码行数:31,代码来源:__init__.py

示例10: define_image_type

    def define_image_type(self, context, result):
        if result is not None:
            if isinstance(result, ResultStorageResult):
                buffer = result.buffer
            else:
                buffer = result
            image_extension = EXTENSION.get(BaseEngine.get_mimetype(buffer), '.jpg')
        else:
            image_extension = context.request.format
            if image_extension is not None:
                image_extension = '.%s' % image_extension
                logger.debug('Image format specified as %s.' % image_extension)
            elif self.is_webp(context):
                image_extension = '.webp'
                logger.debug('Image format set by AUTO_WEBP as %s.' % image_extension)
            elif self.can_auto_convert_png_to_jpg():
                image_extension = '.jpg'
                logger.debug('Image format set by AUTO_PNG_TO_JPG as %s.' % image_extension)
            else:
                image_extension = context.request.engine.extension
                logger.debug('No image format specified. Retrieving from the image extension: %s.' % image_extension)

        content_type = CONTENT_TYPE.get(image_extension, CONTENT_TYPE['.jpg'])

        if context.request.meta:
            context.request.meta_callback = context.config.META_CALLBACK_NAME or self.request.arguments.get('callback', [None])[0]
            content_type = 'text/javascript' if context.request.meta_callback else 'application/json'
            logger.debug('Metadata requested. Serving content type of %s.' % content_type)

        logger.debug('Content Type of %s detected.' % content_type)

        return (image_extension, content_type)
开发者ID:caeugusmao,项目名称:thumbor,代码行数:32,代码来源:__init__.py

示例11: _load_results

    def _load_results(self, context):
        image_extension, content_type = self.define_image_type(context, None)

        quality = self.context.request.quality
        if quality is None:
            if image_extension == '.webp' and self.context.config.WEBP_QUALITY is not None:
                quality = self.context.config.get('WEBP_QUALITY')
            else:
                quality = self.context.config.QUALITY

        results = context.request.engine.read(image_extension, quality)

        if context.request.max_bytes is not None:
            results = self.reload_to_fit_in_kb(
                context.request.engine,
                results,
                image_extension,
                quality,
                context.request.max_bytes
            )
        if not context.request.meta:
            results = self.optimize(context, image_extension, results)
            # An optimizer might have modified the image format.
            content_type = BaseEngine.get_mimetype(results)

        return results, content_type
开发者ID:caeugusmao,项目名称:thumbor,代码行数:26,代码来源:__init__.py

示例12: validate

    def validate(self, body):
        conf = self.context.config
        mime = BaseEngine.get_mimetype(body)

        if mime == 'image/gif' and self.context.config.USE_GIFSICLE_ENGINE:
            engine = self.context.modules.gif_engine
        else:
            engine = self.context.modules.engine

        # Check if image is valid
        try:
            engine.load(body, None)
        except IOError:
            self._error(415, 'Unsupported Media Type')
            return False

        # Check weight constraints
        if (conf.UPLOAD_MAX_SIZE != 0 and len(self.request.body) > conf.UPLOAD_MAX_SIZE):
            self._error(
                412,
                'Image exceed max weight (Expected : %s, Actual : %s)' % (conf.UPLOAD_MAX_SIZE, len(self.request.body)))
            return False

        # Check size constraints
        size = engine.size
        if (conf.MIN_WIDTH > size[0] or conf.MIN_HEIGHT > size[1]):
            self._error(
                412,
                'Image is too small (Expected: %s/%s , Actual : %s/%s) % (conf.MIN_WIDTH, conf.MIN_HEIGHT, size[0], size[1])')
            return False
        return True
开发者ID:caeugusmao,项目名称:thumbor,代码行数:31,代码来源:__init__.py

示例13: test_auto_should_optimize_jpeg_to_jpeg

    def test_auto_should_optimize_jpeg_to_jpeg(self):
        optimizer = AutoOptimizer(self.get_context())
        temp = tempfile.NamedTemporaryFile()
        optimizer.optimize(None, fixtures_folder + '/img/bend.jpg', temp.name)

        temp_buffer = open(temp.name).read()
        self.assertTrue(BaseEngine.get_mimetype(temp_buffer) == 'image/jpeg', "MIME type should be image/jpeg")
开发者ID:PopSugar,项目名称:thumbor-plugins,代码行数:7,代码来源:test_optimizers.py

示例14: setUp

 def setUp(self):
     self.image = (
         (1, 2),
         (3, 4)
     )
     self.context = self.get_context()
     self.engine = BaseEngine(self.context)
     self.engine.flip_horizontally = mock.MagicMock()
     self.engine.flip_horizontally.side_effect = self.flip_horizontally
     self.engine.flip_vertically = mock.MagicMock()
     self.engine.flip_vertically.side_effect = self.flip_vertically
     self.engine.rotate = mock.MagicMock()
     self.engine.rotate.side_effect = self.rotate
开发者ID:caeugusmao,项目名称:thumbor,代码行数:13,代码来源:test_base_engine.py

示例15: _fetch

    def _fetch(self, url, extension, callback):
        storage = self.context.modules.storage
        buffer = storage.get(url)

        if buffer is not None:
            self.context.statsd_client.incr('storage.hit')
            mime = BaseEngine.get_mimetype(buffer)
            if mime == 'image/gif' and self.context.config.USE_GIFSICLE_ENGINE:
                self.context.request.engine = self.context.modules.gif_engine
            else:
                self.context.request.engine = self.context.modules.engine

            callback(False, buffer=buffer)
        else:
            self.context.statsd_client.incr('storage.miss')

            def handle_loader_loaded(buffer):
                if buffer is None:
                    callback(False, None)
                    return

                original_preserve = self.context.config.PRESERVE_EXIF_INFO
                self.context.config.PRESERVE_EXIF_INFO = True

                try:
                    mime = BaseEngine.get_mimetype(buffer)
                    extension = EXTENSION.get(mime,None)

                    if mime == 'image/gif' and self.context.config.USE_GIFSICLE_ENGINE:
                        self.context.request.engine = self.context.modules.gif_engine
                    else:
                        self.context.request.engine = self.context.modules.engine

                    self.context.request.engine.load(buffer, extension)
                    normalized = self.context.request.engine.normalize()
                    is_no_storage = isinstance(storage, NoStorage)
                    is_mixed_storage = isinstance(storage, MixedStorage)
                    is_mixed_no_file_storage = is_mixed_storage and isinstance(storage.file_storage, NoStorage)

                    if not (is_no_storage or is_mixed_no_file_storage):
                        buffer = self.context.request.engine.read(extension)
                        storage.put(url, buffer)

                    storage.put_crypto(url)
                finally:
                    self.context.config.PRESERVE_EXIF_INFO = original_preserve

                callback(normalized, engine=self.context.request.engine)

            self.context.modules.loader.load(self.context, url, handle_loader_loaded)
开发者ID:timothyhmchen,项目名称:thumbor,代码行数:50,代码来源:__init__.py


注:本文中的thumbor.engines.BaseEngine类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。