Esempio n. 1
0
    def get_context(self):
        cfg = Config(SECURITY_KEY='ACME-SEC')
        cfg.LOADER = "thumbor.loaders.file_loader"
        cfg.FILE_LOADER_ROOT_PATH = '/tmp/path/that/does/not/exist'

        cfg.RESULT_STORAGE = 'thumbor.result_storages.file_storage'
        cfg.RESULT_STORAGE_EXPIRATION_SECONDS = 60
        cfg.RESULT_STORAGE_FILE_STORAGE_ROOT_PATH = self.root_path
        cfg.FFMPEG_PATH = which('ffmpeg')

        cfg.USE_GIFSICLE_ENGINE = True
        cfg.AUTO_WEBP = True
        cfg.OPTIMIZERS = [
            'thumbor.optimizers.gifv',
        ]

        importer = Importer(cfg)
        importer.import_modules()
        server = ServerParameters(8889, 'localhost', 'thumbor.conf', None,
                                  'info', None)
        server.security_key = 'ACME-SEC'
        ctx = Context(server, cfg, importer)
        ctx.server.gifsicle_path = which('gifsicle')

        return ctx
Esempio n. 2
0
    def get_app(self):
        cfg = Config(SECURITY_KEY='ACME-SEC')
        server_params = ServerParameters(None, None, None, None, None, None)
        server_params.gifsicle_path = which('gifsicle')

        cfg.DETECTORS = [
            'thumbor.detectors.face_detector',
            'thumbor.detectors.profile_detector',
            'thumbor.detectors.glasses_detector',
            'thumbor.detectors.feature_detector',
        ]
        cfg.STORAGE = 'thumbor.storages.no_storage'
        cfg.LOADER = 'thumbor.loaders.file_loader'
        cfg.FILE_LOADER_ROOT_PATH = os.path.join(os.path.dirname(__file__), 'imgs')
        cfg.ENGINE = getattr(self, 'engine', None)
        cfg.USE_GIFSICLE_ENGINE = True
        cfg.FFMPEG_PATH = which('ffmpeg')
        cfg.ENGINE_THREADPOOL_SIZE = 10
        cfg.OPTIMIZERS = [
            'thumbor.optimizers.gifv',
        ]
        if not cfg.ENGINE:
            return None

        importer = Importer(cfg)
        importer.import_modules()
        ctx = Context(server_params, cfg, importer)
        application = ThumborServiceApp(ctx)

        return application
Esempio n. 3
0
    def get_app(self):
        cfg = Config(SECURITY_KEY='ACME-SEC')
        server_params = ServerParameters(None, None, None, None, None, None)
        server_params.gifsicle_path = which('gifsicle')

        cfg.DETECTORS = [
            'thumbor.detectors.face_detector',
            'thumbor.detectors.profile_detector',
            'thumbor.detectors.glasses_detector',
            'thumbor.detectors.feature_detector',
        ]
        cfg.STORAGE = 'thumbor.storages.no_storage'
        cfg.LOADER = 'thumbor.loaders.file_loader'
        cfg.FILE_LOADER_ROOT_PATH = os.path.join(os.path.dirname(__file__),
                                                 'imgs')
        cfg.ENGINE = getattr(self, 'engine', None)
        cfg.USE_GIFSICLE_ENGINE = True
        cfg.FFMPEG_PATH = which('ffmpeg')
        cfg.ENGINE_THREADPOOL_SIZE = 10
        cfg.OPTIMIZERS = [
            'thumbor.optimizers.gifv',
        ]
        if not cfg.ENGINE:
            return None

        importer = Importer(cfg)
        importer.import_modules()
        ctx = Context(server_params, cfg, importer)
        application = ThumborServiceApp(ctx)

        return application
Esempio n. 4
0
 def get_config(self):
     return Config(SECURITY_KEY='ACME-SEC',
                   ENGINE='thumbor.engines.mp4',
                   LOADER="thumbor.loaders.file_loader",
                   FILE_LOADER_ROOT_PATH=STORAGE_PATH,
                   STORAGE='thumbor.storages.no_storage',
                   FFMPEG_PATH=which('ffmpeg'),
                   FFPROBE_PATH=which('ffprobe'),
                   CONVERT_PATH=which('convert'))
Esempio n. 5
0
    def get_context(self):
        cfg = Config(SECURITY_KEY="ACME-SEC")
        cfg.LOADER = "thumbor.loaders.file_loader"
        cfg.FILE_LOADER_ROOT_PATH = self.loader_path
        cfg.FFMPEG_PATH = which("ffmpeg")
        cfg.OPTIMIZERS = ["thumbor.optimizers.gifv"]

        importer = Importer(cfg)
        importer.import_modules()
        server = ServerParameters(8889, "localhost", "thumbor.conf", None, "info", None)
        server.security_key = "ACME-SEC"
        ctx = Context(server, cfg, importer)
        ctx.server.gifsicle_path = which("gifsicle")
        return ctx
Esempio n. 6
0
    def test_should_optimize_jpeg(self):
        response = self.fetch('/unsafe/200x200/image.jpg')

        tmp_fd, tmp_file_path = tempfile.mkstemp(suffix='.jpg')
        f = os.fdopen(tmp_fd, 'w')
        f.write(response.body)
        f.close()

        exiftool = which('exiftool')
        if not exiftool:
            raise AssertionError('exiftool was not found. Please install it to run thumbor\'s tests.')

        command = [
            exiftool,
            tmp_file_path,
            '-DeviceModel',
            '-EncodingProcess'
        ]

        try:
            with open(os.devnull) as null:
                output = subprocess.check_output(command, stdin=null)

            expect(response.code).to_equal(200)
            expect(output).to_equal('Encoding Process                : Progressive DCT, Huffman coding\n')
        finally:
            os.remove(tmp_file_path)
Esempio n. 7
0
    def get_context(self):
        cfg = Config(SECURITY_KEY='ACME-SEC')
        cfg.LOADER = "thumbor.loaders.file_loader"
        cfg.FILE_LOADER_ROOT_PATH = self.loader_path
        cfg.FFMPEG_PATH = which('ffmpeg')
        cfg.OPTIMIZERS = [
            'thumbor.optimizers.gifv',
        ]

        importer = Importer(cfg)
        importer.import_modules()
        server = ServerParameters(8889, 'localhost', 'thumbor.conf', None, 'info', None)
        server.security_key = 'ACME-SEC'
        ctx = Context(server, cfg, importer)
        ctx.server.gifsicle_path = which('gifsicle')
        return ctx
Esempio n. 8
0
    def get_context(self):
        cfg = Config(SECURITY_KEY='ACME-SEC')
        cfg.LOADER = "thumbor.loaders.file_loader"
        cfg.FILE_LOADER_ROOT_PATH = self.loader_path
        cfg.FFMPEG_PATH = which('ffmpeg')
        cfg.OPTIMIZERS = [
            'thumbor.optimizers.gifv',
        ]

        importer = Importer(cfg)
        importer.import_modules()
        server = ServerParameters(8889, 'localhost', 'thumbor.conf', None, 'info', None)
        server.security_key = 'ACME-SEC'
        ctx = Context(server, cfg, importer)
        ctx.server.gifsicle_path = which('gifsicle')
        return ctx
Esempio n. 9
0
    def get_app(self):
        cfg = Config(SECURITY_KEY='ACME-SEC')
        cfg.LOADER = "thumbor.loaders.file_loader"
        cfg.FILE_LOADER_ROOT_PATH = storage_path
        cfg.RESULT_STORAGE = 'thumbor.result_storages.file_storage'
        cfg.RESULT_STORAGE_EXPIRATION_SECONDS = 60
        cfg.RESULT_STORAGE_FILE_STORAGE_ROOT_PATH = tempfile.mkdtemp(
            prefix='thumbor_test')
        cfg.USE_GIFSICLE_ENGINE = True
        cfg.AUTO_WEBP = True
        cfg.OPTIMIZERS = [
            'thumbor.optimizers.gifv',
        ]

        importer = Importer(cfg)
        importer.import_modules()
        server = ServerParameters(8889, 'localhost', 'thumbor.conf', None,
                                  'info', None)
        server.security_key = 'ACME-SEC'
        ctx = Context(server, cfg, importer)
        ctx.server.gifsicle_path = which('gifsicle')
        application = ThumborServiceApp(ctx)

        self.engine = PILEngine(ctx)

        return application
Esempio n. 10
0
    def test_should_optimize_jpeg(self):
        response = self.fetch('/unsafe/200x200/image.jpg')

        tmp_fd, tmp_file_path = tempfile.mkstemp(suffix='.jpg')
        f = os.fdopen(tmp_fd, 'w')
        f.write(response.body)
        f.close()

        exiftool = which('exiftool')
        if not exiftool:
            raise AssertionError(
                'exiftool was not found. Please install it to run thumbor\'s tests.'
            )

        command = [exiftool, tmp_file_path, '-DeviceModel', '-EncodingProcess']

        try:
            with open(os.devnull) as null:
                output = subprocess.check_output(command, stdin=null)

            expect(response.code).to_equal(200)
            expect(output).to_equal(
                'Encoding Process                : Progressive DCT, Huffman coding\n'
            )
        finally:
            os.remove(tmp_file_path)
Esempio n. 11
0
    def get_optimizer(self):
        conf = Config()
        conf.STATSD_HOST = ''
        conf.JPEGTRAN_PATH = which('jpegtran')
        ctx = Context(config=conf)
        optimizer = Optimizer(ctx)

        return optimizer
Esempio n. 12
0
 def run_gifsicle(self, command):
     if not self.context.server.gifsicle_path:
         gifsicle_path = self.context.config.GIFSICLE_PATH or which('gifsicle')
         if not gifsicle_path:
             raise RuntimeError(
                 "gif engine was requested, but gifsicle binary cannot be found")
         self.context.server.gifsicle_path = gifsicle_path
     return super(Engine, self).run_gifsicle(command)
Esempio n. 13
0
    def get_optimizer(self):
        conf = Config()
        conf.STATSD_HOST = ''
        conf.JPEGTRAN_PATH = which('jpegtran')
        ctx = Context(config=conf)
        optimizer = Optimizer(ctx)

        return optimizer
Esempio n. 14
0
    def get_context(self):
        conf = Config()
        conf.STATSD_HOST = ''
        conf.FFMPEG_PATH = which('ffmpeg')
        ctx = Context(config=conf)
        ctx.request = RequestParameters()
        ctx.request.filters.append('gifv')

        return ctx
Esempio n. 15
0
def config(storage_path, ffmpeg_path):
    Config.allow_environment_variables()
    return Config(SECURITY_KEY='changeme',
                  LOADER='thumbor.loaders.file_loader',
                  FILTERS=[],
                  FILE_LOADER_ROOT_PATH=storage_path,
                  FFMPEG_PATH=ffmpeg_path,
                  FFPROBE_PATH=(os.getenv('FFPROBE_PATH') or which('ffprobe')),
                  STORAGE='thumbor.storages.no_storage')
Esempio n. 16
0
    def get_context(self):
        conf = Config()
        conf.STATSD_HOST = ''
        conf.FFMPEG_PATH = which('ffmpeg')
        ctx = Context(config=conf)
        ctx.request = RequestParameters()
        ctx.request.filters.append('gifv')

        return ctx
Esempio n. 17
0
def test_server_gifsicle_none_calls_which(mocker, context, gif_buffer):
    assert which(
        'gifsicle') is not None, "Test cannot run without gifsicle in PATH"

    mocker.spy(thumbor_video_engine.engines.gif, 'which')
    mocker.spy(GifEngine, 'run_gifsicle')

    context.server.gifsicle_path = None

    engine = GifEngine(context)
    engine.load(gif_buffer, '.gif')

    assert GifEngine.run_gifsicle.mock_calls == [
        mocker.call(mocker.ANY, '--info'),
    ]
    assert thumbor_video_engine.engines.gif.which.mock_calls == [
        mocker.call('gifsicle')
    ]
    assert context.server.gifsicle_path == which('gifsicle')
Esempio n. 18
0
def ffprobe(buf, extension=None, flat=True):
    """
    Returns a dict based on the json output of ffprobe. If ``flat`` is ``True``,
    the 'format' key-values are made top-level, as well as the first video stream
    in the file (the rest are discarded). Any 'stream' keys that have the same
    name as a key in 'format' are prefixed with ``stream_``.
    """
    global FFPROBE_PATH

    if FFPROBE_PATH is None:
        FFPROBE_PATH = which('ffprobe')

    if FFPROBE_PATH is None:
        raise FFmpegError("Could not find ffprobe executable")

    with named_tmp_file(data=buf, extension=extension) as input_file:
        command = [
            FFPROBE_PATH, '-hide_banner', '-loglevel', 'fatal', '-show_error',
            '-show_format', '-show_streams', '-print_format', 'json',
            '-i', input_file,
        ]

        proc = Popen(command, stdout=PIPE, stdin=PIPE, stderr=PIPE)

        stdout, stderr = proc.communicate()

        try:
            probe_data = json.loads(stdout)
        except ValueError:
            probe_data = None

        if not isinstance(probe_data, dict):
            raise FFmpegError("ffprobe returned invalid data")

        if 'error' in probe_data:
            raise FFmpegError("%(string)s (%(code)s)" % probe_data['error'])

        if 'format' not in probe_data or 'streams' not in probe_data:
            raise FFmpegError("ffprobe returned invalid data")

        if not flat:
            return probe_data

        try:
            video_stream = next(s for s in probe_data['streams'] if s['codec_type'] == 'video')
        except StopIteration:
            raise FFmpegError("File is missing a video stream")

        data = probe_data['format']
        for k, v in six.iteritems(video_stream):
            if k in data:
                k = 'stream_%s' % k
            data[k] = v
        return data
Esempio n. 19
0
    def get_context(self):
        cfg = Config(SECURITY_KEY="ACME-SEC")
        cfg.LOADER = "thumbor.loaders.file_loader"
        cfg.FILE_LOADER_ROOT_PATH = "/tmp/path/that/does/not/exist"

        cfg.RESULT_STORAGE = "thumbor.result_storages.file_storage"
        cfg.RESULT_STORAGE_EXPIRATION_SECONDS = 60
        cfg.RESULT_STORAGE_FILE_STORAGE_ROOT_PATH = self.root_path
        cfg.FFMPEG_PATH = which("ffmpeg")

        cfg.USE_GIFSICLE_ENGINE = True
        cfg.AUTO_WEBP = True
        cfg.OPTIMIZERS = ["thumbor.optimizers.gifv"]

        importer = Importer(cfg)
        importer.import_modules()
        server = ServerParameters(8889, "localhost", "thumbor.conf", None, "info", None)
        server.security_key = "ACME-SEC"
        ctx = Context(server, cfg, importer)
        ctx.server.gifsicle_path = which("gifsicle")

        return ctx
Esempio n. 20
0
    def get_context(self):
        cfg = Config(SECURITY_KEY='ACME-SEC')
        cfg.LOADER = "thumbor.loaders.file_loader"
        cfg.FILE_LOADER_ROOT_PATH = self.loader_path
        cfg.STORAGE = "thumbor.storages.no_storage"
        cfg.MAX_PIXELS = 1000

        importer = Importer(cfg)
        importer.import_modules()
        server = ServerParameters(8889, 'localhost', 'thumbor.conf', None, 'info', None)
        server.security_key = 'ACME-SEC'
        ctx = Context(server, cfg, importer)
        ctx.server.gifsicle_path = which('gifsicle')
        return ctx
Esempio n. 21
0
    def get_context(self):
        cfg = Config(SECURITY_KEY="ACME-SEC")
        cfg.LOADER = "thumbor.loaders.file_loader"
        cfg.FILE_LOADER_ROOT_PATH = self.loader_path
        cfg.STORAGE = "thumbor.storages.no_storage"
        cfg.MAX_PIXELS = 1000

        importer = Importer(cfg)
        importer.import_modules()
        server = ServerParameters(8889, "localhost", "thumbor.conf", None, "info", None)
        server.security_key = "ACME-SEC"
        ctx = Context(server, cfg, importer)
        ctx.server.gifsicle_path = which("gifsicle")
        return ctx
Esempio n. 22
0
    def get_context(self):
        cfg = Config(SECURITY_KEY="ACME-SEC")
        cfg.LOADER = "thumbor.loaders.file_loader"
        cfg.FILE_LOADER_ROOT_PATH = self.loader_path
        cfg.JPEGTRAN_PATH = which("jpegtran")
        cfg.PROGRESSIVE_JPEG = True
        cfg.OPTIMIZERS = ["thumbor.optimizers.jpegtran"]

        importer = Importer(cfg)
        importer.import_modules()
        server = ServerParameters(8889, "localhost", "thumbor.conf", None, "info", None)
        server.security_key = "ACME-SEC"
        ctx = Context(server, cfg, importer)
        return ctx
Esempio n. 23
0
    def get_context(self):
        cfg = Config(SECURITY_KEY='ACME-SEC')
        cfg.LOADER = "thumbor.loaders.file_loader"
        cfg.FILE_LOADER_ROOT_PATH = self.loader_path
        cfg.AUTO_WEBP = True
        cfg.USE_GIFSICLE_ENGINE = True

        importer = Importer(cfg)
        importer.import_modules()
        server = ServerParameters(8889, 'localhost', 'thumbor.conf', None, 'info', None)
        server.security_key = 'ACME-SEC'
        ctx = Context(server, cfg, importer)
        ctx.server.gifsicle_path = which('gifsicle')
        return ctx
Esempio n. 24
0
    def get_context(self):
        cfg = Config(SECURITY_KEY='ACME-SEC')
        cfg.LOADER = "thumbor.loaders.file_loader"
        cfg.FILE_LOADER_ROOT_PATH = self.loader_path
        cfg.AUTO_WEBP = True
        cfg.USE_GIFSICLE_ENGINE = True

        importer = Importer(cfg)
        importer.import_modules()
        server = ServerParameters(8889, 'localhost', 'thumbor.conf', None, 'info', None)
        server.security_key = 'ACME-SEC'
        ctx = Context(server, cfg, importer)
        ctx.server.gifsicle_path = which('gifsicle')
        return ctx
Esempio n. 25
0
    def get_context(self):
        cfg = Config(SECURITY_KEY='ACME-SEC')
        cfg.LOADER = "thumbor.loaders.file_loader"
        cfg.FILE_LOADER_ROOT_PATH = self.loader_path
        cfg.STORAGE = "thumbor.storages.no_storage"
        cfg.RESPECT_ORIENTATION = True

        importer = Importer(cfg)
        importer.import_modules()
        server = ServerParameters(8889, 'localhost', 'thumbor.conf', None, 'info', None)
        server.security_key = 'ACME-SEC'
        self.context = Context(server, cfg, importer)
        self.context.server.gifsicle_path = which('gifsicle')
        return self.context
Esempio n. 26
0
def validate_config(config, server_parameters):
    if server_parameters.security_key is None:
        server_parameters.security_key = config.SECURITY_KEY

    if not isinstance(server_parameters.security_key, basestring):
        raise RuntimeError(
            'No security key was found for this instance of thumbor. ' +
            'Please provide one using the conf file or a security key file.')

    if config.USE_GIFSICLE_ENGINE:
        server_parameters.gifsicle_path = which('gifsicle')
        if server_parameters.gifsicle_path is None:
            raise RuntimeError(
                'If using USE_GIFSICLE_ENGINE configuration to True, the `gifsicle` binary must be in the PATH '
                'and must be an executable.')
Esempio n. 27
0
    def get_context(self):
        cfg = Config(SECURITY_KEY='ACME-SEC')
        cfg.LOADER = "thumbor.loaders.file_loader"
        cfg.FILE_LOADER_ROOT_PATH = self.loader_path

        cfg.RESULT_STORAGE = 'thumbor.result_storages.file_storage'
        cfg.RESULT_STORAGE_EXPIRATION_SECONDS = 60
        cfg.RESULT_STORAGE_FILE_STORAGE_ROOT_PATH = self.root_path

        cfg.USE_GIFSICLE_ENGINE = True
        cfg.FFMPEG_PATH = which('ffmpeg')
        cfg.AUTO_WEBP = True
        cfg.OPTIMIZERS = [
            'thumbor.optimizers.gifv',
        ]

        importer = Importer(cfg)
        importer.import_modules()
        server = ServerParameters(8889, 'localhost', 'thumbor.conf', None, 'info', None)
        server.security_key = 'ACME-SEC'
        ctx = Context(server, cfg, importer)
        ctx.server.gifsicle_path = which('gifsicle')

        return ctx
Esempio n. 28
0
    def get_context(self):
        cfg = Config(SECURITY_KEY='ACME-SEC')
        cfg.LOADER = "thumbor.loaders.file_loader"
        cfg.FILE_LOADER_ROOT_PATH = self.loader_path
        cfg.JPEGTRAN_PATH = which('jpegtran')
        cfg.PROGRESSIVE_JPEG = True
        cfg.OPTIMIZERS = [
            'thumbor.optimizers.jpegtran',
        ]

        importer = Importer(cfg)
        importer.import_modules()
        server = ServerParameters(8889, 'localhost', 'thumbor.conf', None, 'info', None)
        server.security_key = 'ACME-SEC'
        ctx = Context(server, cfg, importer)
        return ctx
Esempio n. 29
0
def validate_config(config, server_parameters):
    if server_parameters.security_key is None:
        server_parameters.security_key = config.SECURITY_KEY

    if not isinstance(server_parameters.security_key, basestring):
        raise RuntimeError(
            'No security key was found for this instance of thumbor. ' +
            'Please provide one using the conf file or a security key file.')

    if config.USE_GIFSICLE_ENGINE:
        server_parameters.gifsicle_path = which('gifsicle')
        if server_parameters.gifsicle_path is None:
            raise RuntimeError(
                'If using USE_GIFSICLE_ENGINE configuration to True, the `gifsicle` binary must be in the PATH '
                'and must be an executable.'
            )
Esempio n. 30
0
    def get_context(self):
        cfg = Config(SECURITY_KEY='ACME-SEC')
        cfg.LOADER = "thumbor.loaders.file_loader"
        cfg.FILE_LOADER_ROOT_PATH = self.loader_path
        cfg.RESULT_STORAGE = 'thumbor.result_storages.file_storage'
        cfg.RESULT_STORAGE_EXPIRATION_SECONDS = 60
        cfg.RESULT_STORAGE_FILE_STORAGE_ROOT_PATH = self.root_path
        cfg.AUTO_WEBP = True

        importer = Importer(cfg)
        importer.import_modules()
        server = ServerParameters(8889, 'localhost', 'thumbor.conf', None, 'info', None)
        server.security_key = 'ACME-SEC'
        ctx = Context(server, cfg, importer)
        ctx.request = self.get_request()
        ctx.server.gifsicle_path = which('gifsicle')
        return ctx
Esempio n. 31
0
    def get_app(self):
        cfg = Config(SECURITY_KEY="ACME-SEC")
        cfg.LOADER = "thumbor.loaders.file_loader"
        cfg.FILE_LOADER_ROOT_PATH = storage_path
        cfg.OPTIMIZERS = ["thumbor.optimizers.gifv"]

        importer = Importer(cfg)
        importer.import_modules()
        server = ServerParameters(8889, "localhost", "thumbor.conf", None, "info", None)
        server.security_key = "ACME-SEC"
        ctx = Context(server, cfg, importer)
        ctx.server.gifsicle_path = which("gifsicle")
        application = ThumborServiceApp(ctx)

        self.engine = PILEngine(ctx)

        return application
    def get_app(self):
        cfg = Config(SECURITY_KEY='ACME-SEC')
        cfg.LOADER = "thumbor.loaders.file_loader"
        cfg.FILE_LOADER_ROOT_PATH = storage_path
        cfg.AUTO_WEBP = True

        importer = Importer(cfg)
        importer.import_modules()
        server = ServerParameters(8889, 'localhost', 'thumbor.conf', None, 'info', None)
        server.security_key = 'ACME-SEC'
        ctx = Context(server, cfg, importer)
        ctx.server.gifsicle_path = which('gifsicle')
        application = ThumborServiceApp(ctx)

        self.engine = PILEngine(ctx)

        return application
Esempio n. 33
0
    def get_context(self):
        cfg = Config(SECURITY_KEY='ACME-SEC')
        cfg.LOADER = "thumbor.loaders.file_loader"
        cfg.FILE_LOADER_ROOT_PATH = self.loader_path
        cfg.JPEGTRAN_PATH = which('jpegtran')
        cfg.PROGRESSIVE_JPEG = True,
        cfg.RESULT_STORAGE_STORES_UNSAFE = True,
        cfg.OPTIMIZERS = [
            'thumbor.optimizers.jpegtran',
        ]

        importer = Importer(cfg)
        importer.import_modules()
        server = ServerParameters(8889, 'localhost', 'thumbor.conf', None, 'info', None)
        server.security_key = 'ACME-SEC'
        ctx = Context(server, cfg, importer)
        return ctx
Esempio n. 34
0
    def test_should_optimize_jpeg(self):
        response = self.fetch("/unsafe/200x200/hidrocarbonetos_9.jpg")

        tmp_fd, tmp_file_path = tempfile.mkstemp(suffix=".jpg")
        f = os.fdopen(tmp_fd, "w")
        f.write(response.body)
        f.close()

        command = [which("exiftool"), tmp_file_path, "-DeviceModel", "-EncodingProcess"]

        with open(os.devnull) as null:
            output = subprocess.check_output(command, stdin=null)

        expect(response.code).to_equal(200)
        expect(output).to_equal("Encoding Process                : Progressive DCT, Huffman coding\n")

        os.remove(tmp_file_path)
    def get_app(self):
        cfg = Config(SECURITY_KEY='ACME-SEC', OPTIPNG_PATH=which('optipng'), OPTIPNG_LEVEL=1)
        cfg.LOADER = 'thumbor.loaders.file_loader'
        cfg.FILE_LOADER_ROOT_PATH = storage_path
        cfg.OPTIMIZERS = [
            'thumbor_plugins.optimizers.optipng',
        ]

        importer = Importer(cfg)
        importer.import_modules()
        server = ServerParameters(8889, 'localhost', 'thumbor.conf', None, 'info', None)
        server.security_key = 'ACME-SEC'
        ctx = Context(server, cfg, importer)
        application = ThumborServiceApp(ctx)

        self.engine = PILEngine(ctx)

        return application
Esempio n. 36
0
    def get_app(self):
        cfg = Config(SECURITY_KEY='ACME-SEC')
        cfg.LOADER = "thumbor.loaders.file_loader"
        cfg.FILE_LOADER_ROOT_PATH = storage_path
        cfg.AUTO_WEBP = True

        importer = Importer(cfg)
        importer.import_modules()
        server = ServerParameters(8889, 'localhost', 'thumbor.conf', None,
                                  'info', None)
        server.security_key = 'ACME-SEC'
        ctx = Context(server, cfg, importer)
        ctx.server.gifsicle_path = which('gifsicle')
        application = ThumborServiceApp(ctx)

        self.engine = PILEngine(ctx)

        return application
Esempio n. 37
0
        def topic(self):
            config = Config()
            server = ServerParameters(
                8889, 'localhost', 'thumbor.conf', None, 'info', None
            )

            context = Context(server, config, Importer(config))
            context.server.gifsicle_path = which('gifsicle')

            context.request = RequestParameters()

            with open("%s/animated_image.gif" % FIXTURES_FOLDER, "rb") as f:
                buffer = f.read()

            engine = GifEngine(context=context)
            engine.load(buffer, '.gif')

            return engine.read()
    def get_app(self):
        cfg = Config(SECURITY_KEY='ACME-SEC', OPTIPNG_PATH=which('optipng'), OPTIPNG_LEVEL=1)
        cfg.LOADER = 'thumbor.loaders.file_loader'
        cfg.FILE_LOADER_ROOT_PATH = storage_path
        cfg.OPTIMIZERS = [
            'thumbor_plugins.optimizers.optipng',
        ]

        importer = Importer(cfg)
        importer.import_modules()
        server = ServerParameters(8889, 'localhost', 'thumbor.conf', None, 'info', None)
        server.security_key = 'ACME-SEC'
        ctx = Context(server, cfg, importer)
        application = ThumborServiceApp(ctx)

        self.engine = PILEngine(ctx)

        return application
Esempio n. 39
0
def validate_config(config, server_parameters):
    if server_parameters.security_key is None:
        server_parameters.security_key = config.SECURITY_KEY

    if not isinstance(server_parameters.security_key, basestring):
        raise RuntimeError(
            'No security key was found for this instance of thumbor. ' +
            'Please provide one using the conf file or a security key file.')

    if config.ENGINE or config.USE_GIFSICLE_ENGINE:
        # Error on Image.open when image pixel count is above MAX_IMAGE_PIXELS
        warnings.simplefilter('error', Image.DecompressionBombWarning)

    if config.USE_GIFSICLE_ENGINE:
        server_parameters.gifsicle_path = which('gifsicle')
        if server_parameters.gifsicle_path is None:
            raise RuntimeError(
                'If using USE_GIFSICLE_ENGINE configuration to True, the `gifsicle` binary must be in the PATH '
                'and must be an executable.')
Esempio n. 40
0
    def get_app(self):
        cfg = Config(
            SECURITY_KEY='ACME-SEC',
            LOADER='thumbor.loaders.file_loader',
            RESULT_STORAGE_STORES_UNSAFE=False,
            RESULT_STORAGE_EXPIRATION_SECONDS=2592000,
            FILE_LOADER_ROOT_PATH=storage_path,
            OPTIMIZERS=[],
            USE_GIFSICLE_ENGINE=True,
        )

        importer = Importer(cfg)
        importer.import_modules()
        server = ServerParameters(8889, 'localhost', 'thumbor.conf', None, 'info', None)
        server.security_key = 'ACME-SEC'
        server.gifsicle_path = which('gifsicle')
        ctx = Context(server, cfg, importer)
        application = ThumborServiceApp(ctx)

        return application
Esempio n. 41
0
def validate_config(config, server_parameters):
    if server_parameters.security_key is None:
        server_parameters.security_key = config.SECURITY_KEY

    if not isinstance(server_parameters.security_key, basestring):
        raise RuntimeError(
            'No security key was found for this instance of thumbor. ' +
            'Please provide one using the conf file or a security key file.')

    if config.ENGINE or config.USE_GIFSICLE_ENGINE:
        # Error on Image.open when image pixel count is above MAX_IMAGE_PIXELS
        warnings.simplefilter('error', Image.DecompressionBombWarning)

    if config.USE_GIFSICLE_ENGINE:
        server_parameters.gifsicle_path = which('gifsicle')
        if server_parameters.gifsicle_path is None:
            raise RuntimeError(
                'If using USE_GIFSICLE_ENGINE configuration to True, the `gifsicle` binary must be in the PATH '
                'and must be an executable.'
            )
Esempio n. 42
0
    def get_app(self):
        cfg = Config(
            SECURITY_KEY='ACME-SEC',
            LOADER='thumbor.loaders.file_loader',
            RESULT_STORAGE_STORES_UNSAFE=False,
            RESULT_STORAGE_EXPIRATION_SECONDS=2592000,
            FILE_LOADER_ROOT_PATH=storage_path,
            OPTIMIZERS=[],
            USE_GIFSICLE_ENGINE=True,
        )

        importer = Importer(cfg)
        importer.import_modules()
        server = ServerParameters(8889, 'localhost', 'thumbor.conf', None,
                                  'info', None)
        server.security_key = 'ACME-SEC'
        server.gifsicle_path = which('gifsicle')
        ctx = Context(server, cfg, importer)
        application = ThumborServiceApp(ctx)

        return application
Esempio n. 43
0
    def get_app(self):
        cfg = Config(SECURITY_KEY="ACME-SEC")
        cfg.LOADER = "thumbor.loaders.file_loader"
        cfg.FILE_LOADER_ROOT_PATH = storage_path
        cfg.RESULT_STORAGE = "thumbor.result_storages.file_storage"
        cfg.RESULT_STORAGE_EXPIRATION_SECONDS = 60
        cfg.RESULT_STORAGE_FILE_STORAGE_ROOT_PATH = tempfile.mkdtemp(prefix="thumbor_test")
        cfg.USE_GIFSICLE_ENGINE = True
        cfg.AUTO_WEBP = True
        cfg.OPTIMIZERS = ["thumbor.optimizers.gifv"]

        importer = Importer(cfg)
        importer.import_modules()
        server = ServerParameters(8889, "localhost", "thumbor.conf", None, "info", None)
        server.security_key = "ACME-SEC"
        ctx = Context(server, cfg, importer)
        ctx.server.gifsicle_path = which("gifsicle")
        application = ThumborServiceApp(ctx)

        self.engine = PILEngine(ctx)

        return application
Esempio n. 44
0
    def test_should_optimize_jpeg(self):
        response = self.fetch('/unsafe/200x200/hidrocarbonetos_9.jpg')

        tmp_fd, tmp_file_path = tempfile.mkstemp(suffix='.jpg')
        f = os.fdopen(tmp_fd, 'w')
        f.write(response.body)
        f.close()

        command = [
            which('exiftool'),
            tmp_file_path,
            '-DeviceModel',
            '-EncodingProcess'
        ]

        with open(os.devnull) as null:
            output = subprocess.check_output(command, stdin=null)

        expect(response.code).to_equal(200)
        expect(output).to_equal('Encoding Process                : Progressive DCT, Huffman coding\n')

        os.remove(tmp_file_path)
    def __init__(self, context):
        if context.config.get('MANHOLE_DEBUGGING', None):
            logger.debug('Installing manhole')
            socket = 'manhole-%s' % context.server.port
            socket_path = os.path.join(
                tempfile.gettempdir(),
                socket
            )

            manhole.install(socket_path=socket_path)

        # The gifsicle engine needs to work, regardless of
        # USE_GIFSICLE_ENGINE being on or not
        context.server.gifsicle_path = which('gifsicle')

        # T178072 Disable Thumbor's built-in EXIF parsing, which
        # emits logger.error messages constantly because it's trying
        # to parse our truncated buffer. EXIF parsing is done in our
        # imagemagick engine instead.
        thumbor.engines.METADATA_AVAILABLE = False

        super(App, self).__init__(context)
Esempio n. 46
0
def context(config):
    config.ENGINE = 'thumbor_video_engine.engines.video'

    importer = Importer(config)
    importer.import_modules()

    server = ServerParameters(None,
                              'localhost',
                              'thumbor.conf',
                              None,
                              'info',
                              config.APP_CLASS,
                              gifsicle_path=which('gifsicle'))
    server.security_key = config.SECURITY_KEY

    req = RequestParameters()

    configure_log(config, 'DEBUG')

    with Context(server=server, config=config, importer=importer) as context:
        context.request = req
        context.request.engine = context.modules.engine
        yield context
Esempio n. 47
0
 def get_server(self):
     server = ServerParameters(8889, "localhost", "thumbor.conf", None, "info", None)
     server.security_key = "ACME-SEC"
     server.gifsicle_path = which("gifsicle")
     return server
Esempio n. 48
0
 def setUp(self):
     self.pngcrush_path = which('pngcrush')
     if not (os.path.isfile(self.pngcrush_path) and os.access(self.pngcrush_path, os.X_OK)):
         raise unittest.SkipTest("Unable to locate pngcrush at {}".format(self.pngcrush_path))
Esempio n. 49
0
    def test_can_which_by_path(self):
        result = which('/bin/ls')
        expect(result).to_equal('/bin/ls')

        result = which('/tmp')
        expect(result).to_be_null()
Esempio n. 50
0
 def get_server(self):
     server = ServerParameters(8889, 'localhost', 'thumbor.conf', None, 'info', None)
     server.security_key = 'ACME-SEC'
     server.gifsicle_path = which('gifsicle')
     return server
Esempio n. 51
0
    def test_can_which_by_env(self):
        result = which('ls')
        expect(result).to_equal('/bin/ls')

        result = which('invalid-command')
        expect(result).to_be_null()
Esempio n. 52
0
 def get_server(self):
     server = ServerParameters(8889, 'localhost', 'thumbor.conf', None,
                               'info', None)
     server.security_key = 'ACME-SEC'
     server.gifsicle_path = which('gifsicle')
     return server
Esempio n. 53
0
    def test_can_which_by_env(self):
        result = which('ls')
        expect(result).to_equal('/bin/ls')

        result = which('invalid-command')
        expect(result).to_be_null()
Esempio n. 54
0
def main(arguments=None):  # NOQA
    '''Runs thumbor server with the specified arguments.'''

    server_parameters = get_server_parameters(arguments)

    lookup_paths = [os.curdir,
                    expanduser('~'),
                    '/etc/',
                    dirname(__file__)]

    config = Config.load(server_parameters.config_path, conf_name='thumbor.conf', lookup_paths=lookup_paths)

    if (config.THUMBOR_LOG_CONFIG and config.THUMBOR_LOG_CONFIG != ''):
        logging.config.dictConfig(config.THUMBOR_LOG_CONFIG)
    else:
        logging.basicConfig(
            level=getattr(logging, server_parameters.log_level.upper()),
            format=config.THUMBOR_LOG_FORMAT,
            datefmt=config.THUMBOR_LOG_DATE_FORMAT
        )

    importer = Importer(config)
    importer.import_modules()

    if importer.error_handler_class is not None:
        importer.error_handler = importer.error_handler_class(config)

    if server_parameters.security_key is None:
        server_parameters.security_key = config.SECURITY_KEY

    if not isinstance(server_parameters.security_key, basestring):
        raise RuntimeError(
            'No security key was found for this instance of thumbor. ' +
            'Please provide one using the conf file or a security key file.')

    if config.USE_GIFSICLE_ENGINE:
        server_parameters.gifsicle_path = which('gifsicle')
        if server_parameters.gifsicle_path is None:
            raise RuntimeError(
                'If using USE_GIFSICLE_ENGINE configuration to True, the `gifsicle` binary must be in the PATH '
                'and must be an executable.'
            )

    context = Context(
        server=server_parameters,
        config=config,
        importer=importer
    )

    application = importer.import_class(server_parameters.app_class)(context)

    server = HTTPServer(application)

    if context.server.fd is not None:
        fd_number = get_as_integer(context.server.fd)
        if fd_number is None:
            with open(context.server.fd, 'r') as sock:
                fd_number = sock.fileno()

        sock = socket.fromfd(fd_number,
                             socket.AF_INET | socket.AF_INET6,
                             socket.SOCK_STREAM)
        server.add_socket(sock)
    else:
        server.bind(context.server.port, context.server.ip)

    server.start(1)

    try:
        logging.debug('thumbor running at %s:%d' % (context.server.ip, context.server.port))
        tornado.ioloop.IOLoop.instance().start()
    except KeyboardInterrupt:
        print
        print "-- thumbor closed by user interruption --"
    finally:
        context.thread_pool.cleanup()
 def setUp(self):
     self.pngcrush_path = which('pngcrush')
     if not (os.path.isfile(self.pngcrush_path) and os.access(self.pngcrush_path, os.X_OK)):
         raise unittest.SkipTest("Unable to locate pngcrush at {}".format(self.pngcrush_path))
 def setUp(self):
     self.optipng_path = which('optipng')
     if not (os.path.isfile(self.optipng_path) and os.access(self.optipng_path, os.X_OK)):
         raise unittest.SkipTest("Unable to locate optipng at {}".format(self.optipng_path))
Esempio n. 57
0
 def setUp(self):
     self.optipng_path = which('optipng')
     if not (os.path.isfile(self.optipng_path) and os.access(self.optipng_path, os.X_OK)):
         raise unittest.SkipTest("Unable to locate optipng at {}".format(self.optipng_path))
Esempio n. 58
0
    def test_can_which_by_path(self):
        result = which('/bin/ls')
        expect(result).to_equal('/bin/ls')

        result = which('/tmp')
        expect(result).to_be_null()
Esempio n. 59
0
def main(arguments=None):
    '''Runs thumbor server with the specified arguments.'''

    server_parameters = get_server_parameters(arguments)

    lookup_paths = [os.curdir, expanduser('~'), '/etc/', dirname(__file__)]

    config = Config.load(server_parameters.config_path,
                         conf_name='thumbor.conf',
                         lookup_paths=lookup_paths)

    if (config.THUMBOR_LOG_CONFIG and config.THUMBOR_LOG_CONFIG != ''):
        logging.config.dictConfig(config.THUMBOR_LOG_CONFIG)
    else:
        logging.basicConfig(level=getattr(logging,
                                          server_parameters.log_level.upper()),
                            format=config.THUMBOR_LOG_FORMAT,
                            datefmt=config.THUMBOR_LOG_DATE_FORMAT)

    importer = Importer(config)
    importer.import_modules()

    if importer.error_handler_class is not None:
        importer.error_handler = importer.error_handler_class(config)

    if server_parameters.security_key is None:
        server_parameters.security_key = config.SECURITY_KEY

    if not isinstance(server_parameters.security_key, basestring):
        raise RuntimeError(
            'No security key was found for this instance of thumbor. ' +
            'Please provide one using the conf file or a security key file.')

    if config.USE_GIFSICLE_ENGINE:
        server_parameters.gifsicle_path = which('gifsicle')
        if server_parameters.gifsicle_path is None:
            raise RuntimeError(
                'If using USE_GIFSICLE_ENGINE configuration to True, the `gifsicle` binary must be in the PATH and must be an executable.'
            )

    context = Context(server=server_parameters,
                      config=config,
                      importer=importer)

    application = importer.import_class(server_parameters.app_class)(context)

    server = HTTPServer(application)

    if context.server.fd is not None:
        fd_number = get_as_integer(context.server.fd)
        if fd_number is None:
            with open(context.server.fd, 'r') as sock:
                fd_number = sock.fileno()

        sock = socket.fromfd(fd_number, socket.AF_INET | socket.AF_INET6,
                             socket.SOCK_STREAM)
        server.add_socket(sock)
    else:
        server.bind(context.server.port, context.server.ip)

    server.start(1)

    try:
        logging.debug('thumbor running at %s:%d' %
                      (context.server.ip, context.server.port))
        tornado.ioloop.IOLoop.instance().start()
    except KeyboardInterrupt:
        print
        print "-- thumbor closed by user interruption --"
    finally:
        context.thread_pool.cleanup()