def test_thumbnailing_nonseekable_svg_file():
    class DummyFile(BytesIO):
        def tell(self):
            raise IOError('DummyFile does not support tell/seek')
        seek = tell

    source = Thumbnailer(file=DummyFile(TEST_SVG), name='test.svg')
    source.url = '/media/test.svg'
    assert thumbnail(source) is None
def test_thumbnailing_nonseekable_svg_file():
    class DummyFile(BytesIO):
        def tell(self):
            raise IOError('DummyFile does not support tell/seek')
        seek = tell

    source = Thumbnailer(file=DummyFile(TEST_SVG), name='test.svg')
    source.url = '/media/test.svg'

    # Well if the merchant tries his luck with non seekable file
    # and decides to name it as svg then he might not have working
    # front. Also not worth thumbnail these anyways so might as well
    # return the source url.
    assert thumbnail(source) == source.url
def test_thumbnail_cache():
    image1 = factories.get_random_filer_image()
    image2 = factories.get_random_filer_image()
    media = ProductMedia.objects.create(product=factories.get_default_product(), file=image2)

    cache_key, cached_url = _get_cached_thumbnail_url(image1, alias=None, generate=True)
    assert cache_key and not cached_url
    url = thumbnail(image1)
    cache_key, cached_url = _get_cached_thumbnail_url(image1, alias=None, generate=True)
    assert cache_key and cached_url == url

    cache_key, cached_url = _get_cached_thumbnail_url(media, alias=None, generate=True)
    assert cache_key and not cached_url
    url = thumbnail(media)
    cache_key, cached_url = _get_cached_thumbnail_url(media, alias=None, generate=True)
    assert cache_key and cached_url == url

    img_url = "http://www.shuup.com/logo.png"
    cache_key, cached_url = _get_cached_thumbnail_url(img_url, alias=None, generate=True)
    assert cache_key and not cached_url
    url = thumbnail(img_url)
    cache_key, cached_url = _get_cached_thumbnail_url(img_url, alias=None, generate=True)
    assert cache_key and cached_url == url

    source = Thumbnailer(file=BytesIO(TEST_PNG), name="logo.png")
    source.url = '/media/logo.png'
    cache_key, cached_url = _get_cached_thumbnail_url(source, alias=None, generate=True)
    assert cache_key and not cached_url
    url = thumbnail(source)
    cache_key, cached_url = _get_cached_thumbnail_url(source, alias=None, generate=True)
    assert cache_key and cached_url == url

    # check whether caches are bumped
    image1.save()
    cache_key, cached_url = _get_cached_thumbnail_url(image1, alias=None, generate=True)
    assert cache_key and not cached_url

    media.save()
    cache_key, cached_url = _get_cached_thumbnail_url(media, alias=None, generate=True)
    assert cache_key and not cached_url

    media.delete()
    image1.delete()
    image2.delete()
        def get_images(self, instance):
            images = []
            for path in self.preview_paths:
                file_data = open(os.path.join(settings.MEDIA_ROOT, path), 'r')

                class MockImage(object):
                    pass

                image = MockImage()
                image.file = Thumbnailer(File(file_data), name=path)
                images.append(image)
            return images
Beispiel #5
0
def file_details(path):
    '''
    Returns a dict of info about a Path
    '''
    # st = path.stat()
    content_type, encoding = mimetypes.guess_type(path.name)

    if path.is_dir():
        img = static('filem/img/mimetypes/inode-directory.png')
        content_type = 'inode/directory'
    elif content_type is None:
        img = static('filem/img/mimetypes/unknown.png')
    elif content_type.startswith('image/'):
        tf = Thumbnailer(name=str(path.relative_to(ROOT)))
        img = tf.get_thumbnail({
            'size': (64, 64),
            'crop': 'auto',
            'upscale': True,
        }).url
    else:
        try:
            img = static('filem/img/mimetypes/%s.png' %
                         (content_type.replace('/', '-'), ))
        except:
            img = static('filem/img/mimetypes/unknown.png')
    return {
        'name': path.name,
        'is_dir': path.is_dir(),
        'is_file': path.is_file(),
        'is_symlink': path.is_symlink(),
        # 'size': st.st_size,
        # 'mode': st.st_mode,
        # 'mtime': st.st_mtime,
        # 'ctime': st.st_ctime,
        'content-type': content_type,
        # 'encoding': encoding,
        'thumb': img,
    }
Beispiel #6
0
def file_details(path):
    '''
    Returns a dict of info about a Path
    '''
    # st = path.stat()
    content_type, encoding = mimetypes.guess_type(path.name)

    if path.is_dir():
        img = static('filem/img/mimetypes/inode-directory.png')
        content_type = 'inode/directory'
    elif content_type is None:
        img = static('filem/img/mimetypes/unknown.png')
    elif content_type.startswith('image/'):
        tf = Thumbnailer(name=str(path.relative_to(ROOT)))
        img = tf.get_thumbnail({
            'size': (64, 64),
            'crop': 'auto',
            'upscale': True,
        }).url
    else:
        try:
            img = static('filem/img/mimetypes/%s.png' % (content_type.replace('/', '-'),))
        except:
            img = static('filem/img/mimetypes/unknown.png')
    return {
        'name': path.name,
        'is_dir': path.is_dir(),
        'is_file': path.is_file(),
        'is_symlink': path.is_symlink(),
        # 'size': st.st_size,
        # 'mode': st.st_mode,
        # 'mtime': st.st_mtime,
        # 'ctime': st.st_ctime,
        'content-type': content_type,
        # 'encoding': encoding,
        'thumb': img,
    }
Beispiel #7
0
def do_thumbnailing(content, name, *args, **kwargs):
    source = Thumbnailer(file=BytesIO(content), name=name)
    source.url = '/media/' + name
    return thumbnail(source, *args, **kwargs)
Beispiel #8
0
def test_thumbnailing_svg_without_url():
    source = Thumbnailer(file=BytesIO(TEST_SVG), name='test.svg')
    assert not hasattr(source, 'url')
    assert thumbnail(source) is None
Beispiel #9
0
def test_thumbnailing_with_none_as_thumbnailer():
    source = Thumbnailer(file=BytesIO(TEST_PNG), name='test.png')
    source.easy_thumbnails_thumbnailer = None
    assert thumbnail(source) is None
def test_thumbnail_cache():
    image1 = factories.get_random_filer_image()
    image2 = factories.get_random_filer_image()
    media = ProductMedia.objects.create(
        product=factories.get_default_product(), file=image2)

    cache_key, cached_url = _get_cached_thumbnail_url(image1,
                                                      alias=None,
                                                      generate=True)
    assert cache_key and not cached_url
    url = thumbnail(image1)
    cache_key, cached_url = _get_cached_thumbnail_url(image1,
                                                      alias=None,
                                                      generate=True)
    assert cache_key and cached_url == url

    cache_key, cached_url = _get_cached_thumbnail_url(media,
                                                      alias=None,
                                                      generate=True)
    assert cache_key and not cached_url
    url = thumbnail(media)
    cache_key, cached_url = _get_cached_thumbnail_url(media,
                                                      alias=None,
                                                      generate=True)
    assert cache_key and cached_url == url

    img_url = "http://www.shuup.com/logo.png"
    cache_key, cached_url = _get_cached_thumbnail_url(img_url,
                                                      alias=None,
                                                      generate=True)
    assert cache_key and not cached_url
    url = thumbnail(img_url)
    cache_key, cached_url = _get_cached_thumbnail_url(img_url,
                                                      alias=None,
                                                      generate=True)
    assert cache_key and cached_url == url

    source = Thumbnailer(file=BytesIO(TEST_PNG), name="logo.png")
    source.url = '/media/logo.png'
    cache_key, cached_url = _get_cached_thumbnail_url(source,
                                                      alias=None,
                                                      generate=True)
    assert cache_key and not cached_url
    url = thumbnail(source)
    cache_key, cached_url = _get_cached_thumbnail_url(source,
                                                      alias=None,
                                                      generate=True)
    assert cache_key and cached_url == url

    # check whether caches are bumped
    image1.save()
    cache_key, cached_url = _get_cached_thumbnail_url(image1,
                                                      alias=None,
                                                      generate=True)
    assert cache_key and not cached_url

    media.save()
    cache_key, cached_url = _get_cached_thumbnail_url(media,
                                                      alias=None,
                                                      generate=True)
    assert cache_key and not cached_url

    media.delete()
    image1.delete()
    image2.delete()
def test_thumbnailing_with_none_as_thumbnailer():
    source = Thumbnailer(file=BytesIO(TEST_PNG), name='test.png')
    source.easy_thumbnails_thumbnailer = None
    assert thumbnail(source) is None
def do_thumbnailing(content, name, *args, **kwargs):
    source = Thumbnailer(file=BytesIO(content), name=name)
    source.url = '/media/' + name
    return thumbnail(source, *args, **kwargs)
Beispiel #13
0
def thumbnail(imagefile):
    t = Thumbnailer(imagefile)
    f = t.get_thumbnail({'size':(200,200)})
    return '<img src="%s" alt="An Image">' % f._get_url()