Exemplo n.º 1
0
def s3_thumb_path(item, size, exists=False, ext='jpg'):
    """Get the thumbnail path for the given item and size.

    :param item: A 2-tuple with a subdir name and an ID. If given a
        ORM mapped class with _thumb_dir and id attributes, the info
        can be extracted automatically.
    :type item: ``tuple`` or mapped class instance
    :param size: Size key to display, see ``thumb_sizes`` in
        :mod:`mediacore.config.app_config`
    :type size: str
    :param exists: If enabled, checks to see if the file actually exists.
        If it doesn't exist, ``None`` is returned.
    :type exists: bool
    :param ext: The extension to use, defaults to jpg.
    :type ext: str
    :returns: The absolute system path or ``None``.
    :rtype: str

    """
    if not item:
        return None

    # fallback to normal thumb handling if no S3 engine is enabled.
    bucket_url = get_s3_bucket_url()
    if not bucket_url:
        return None

    image_dir, item_id = normalize_thumb_item(item)
    image = '%s/%s%s.%s' % (image_dir, item_id, size, ext)
    image_path = image # use the relative path

    if exists and not s3_thumb_exists(image_path):
        return None
    return image_path
Exemplo n.º 2
0
def s3_create_default_thumbs_for(item):
    """Create copies of the default thumbs for the given item.

    This copies the default files (all named with an id of 'new') to
    use the given item's id. This means there could be lots of duplicate
    copies of the default thumbs, but at least we can always use the
    same url when rendering.

    :param item: A 2-tuple with a subdir name and an ID. If given a
        ORM mapped class with _thumb_dir and id attributes, the info
        can be extracted automatically.
    :type item: ``tuple`` or mapped class instance
    """
    # fallback to normal thumb handling if no S3 engine is enabled.
    storage = get_s3_storage()
    if not storage:
        return None
    bucket = storage.connect_to_bucket()

    image_dir, item_id = normalize_thumb_item(item)

    for key in config['thumb_sizes'][image_dir].iterkeys():
        src_file = os.path.join(config['cache.dir'], 'images', thumb_path((image_dir, 'new'), key))
        dst_file = thumb_path(item, key)
        key = Key(bucket)
        key.key = dst_file

        key.set_metadata('is_default_thumb', '1')
        key.set_contents_from_filename(src_file, {'Content-Type': 'image/jpeg'})
        key.set_acl('public-read')
    return True
Exemplo n.º 3
0
def s3_thumb_url(item, size, qualified=False, exists=False):
    """Get the thumbnail url for the given item and size.

    :param item: A 2-tuple with a subdir name and an ID. If given a
        ORM mapped class with _thumb_dir and id attributes, the info
        can be extracted automatically.
    :type item: ``tuple`` or mapped class instance
    :param size: Size key to display, see ``thumb_sizes`` in
        :mod:`mediacore.config.app_config`
    :type size: str
    :param qualified: If ``True`` return the full URL including the domain.
    :type qualified: bool
    :param exists: If enabled, checks to see if the file actually exists.
        If it doesn't exist, ``None`` is returned.
    :type exists: bool
    :returns: The relative or absolute URL.
    :rtype: str

    """
    if not item:
        return None

    # fallback to normal thumb handling if no S3 engine is enabled.
    bucket_url = get_s3_bucket_url()
    if not bucket_url:
        return None

    image_dir, item_id = normalize_thumb_item(item)
    image = '%s/%s%s.jpg' % (image_dir, item_id, size)
    image_path = image # use the relative path

    if exists and not s3_thumb_exists(image):
        return None
    return bucket_url + image
Exemplo n.º 4
0
def s3_create_thumbs_for(item, image_file, image_filename):
    """Creates thumbnails in all sizes for a given Media or Podcast object.

    Side effects: Closes the open file handle passed in as image_file.

    :param item: A 2-tuple with a subdir name and an ID. If given a
        ORM mapped class with _thumb_dir and id attributes, the info
        can be extracted automatically.
    :type item: ``tuple`` or mapped class instance
    :param image_file: An open file handle for the original image file.
    :type image_file: file
    :param image_filename: The original filename of the thumbnail image.
    :type image_filename: unicode
    """
    # fallback to normal thumb handling if no S3 engine is enabled.
    storage = get_s3_storage()
    if not storage:
        return None
    bucket = storage.connect_to_bucket()

    image_dir, item_id = normalize_thumb_item(item)
    img = Image.open(image_file)

    # TODO: Allow other formats?
    for key, xy in config['thumb_sizes'][item._thumb_dir].iteritems():
        path = thumb_path(item, key)
        thumb_img = resize_thumb(img, xy)
        if thumb_img.mode != "RGB":
            thumb_img = thumb_img.convert("RGB")

        tmpfile = TemporaryFile()
        thumb_img.save(tmpfile, 'JPEG')

        key = Key(bucket)
        key.key = path
        key.set_contents_from_file(tmpfile, {'Content-Type': 'image/jpeg'})
        key.set_acl('public-read')

    # Backup the original image, ensuring there's no odd chars in the ext.
    # Thumbs from DailyMotion include an extra query string that needs to be
    # stripped off here.
    ext = os.path.splitext(image_filename)[1].lower()
    ext_match = _ext_filter.match(ext)
    if ext_match:
        backup_type = ext_match.group(1)
        backup_path = thumb_path(item, 'orig', ext=backup_type)
        image_file.seek(0)

        key = Key(bucket)
        key.key = backup_path
        key.set_contents_from_file(image_file,
            {'Content-Type': mimetypes.guess_type(backup_path)[0]})
        key.set_acl('public-read')

        image_file.close()
    return True
Exemplo n.º 5
0
def s3_has_default_thumbs(item):
    """Return True if the thumbs for the given item are the defaults.

    :param item: A 2-tuple with a subdir name and an ID. If given a
        ORM mapped class with _thumb_dir and id attributes, the info
        can be extracted automatically.
    :type item: ``tuple`` or mapped class instance
    """
    # fallback to normal thumb handling if no S3 engine is enabled.
    storage = get_s3_storage()
    if not storage:
        return None
    bucket = storage.connect_to_bucket()
    image_dir, item_id = normalize_thumb_item(item)
    key = bucket.get_key(thumb_path(item, 's'))
    return key and key.get_metadata('is_default_thumb') == '1'