Exemplo n.º 1
0
 def frompath(klass, value):
     """Construct a URI from an unescaped filename."""
     # windows unicode path chars may break pathname2url; encode in UTF-8
     if isinstance(value, unicode): value = value.encode("UTF-8")
     return klass("file://" + pathname2url(value), escaped=True)
Exemplo n.º 2
0
    def frompath(klass, value):
        """Construct a URI from an unescaped filename."""

        return klass("file://" + pathname2url(value), escaped=True)
Exemplo n.º 3
0
def get_thumbnail(path, boundary):
    """Get a thumbnail of an image. Will create/use a thumbnail in
    the user's thumbnail directory if possible. Follows the
    Free Desktop specification.
    http://specifications.freedesktop.org/thumbnail-spec/"""

    width, height = boundary

    # embedded thumbnails come from /tmp/
    # and too big thumbnails make no sense
    if path.startswith(tempfile.gettempdir()) or \
        width > 256 or height > 256 or mtime(path) == 0:
        return gtk.gdk.pixbuf_new_from_file_at_size(path, width, height)

    if width <= 128 and height <= 128:
        size_name = "normal"
        thumb_size = 128
    else:
        size_name = "large"
        thumb_size = 256

    thumb_folder = get_thumbnail_folder()
    cache_dir = os.path.join(thumb_folder, size_name)
    mkdir(cache_dir, 0700)

    bytes = path
    if isinstance(path, unicode):
        bytes = path.encode("utf-8")
    uri = "file://" + pathname2url(bytes)
    thumb_name = hash.md5(uri).hexdigest() + ".png"

    thumb_path = os.path.join(cache_dir, thumb_name)

    pb = meta_mtime = None
    if os.path.exists(thumb_path):
        pb = gtk.gdk.pixbuf_new_from_file(thumb_path)
        meta_mtime = pb.get_option("tEXt::Thumb::MTime")
        meta_mtime = meta_mtime and int(meta_mtime)

    if not pb or meta_mtime != int(mtime(path)):
        pb = gtk.gdk.pixbuf_new_from_file(path)

        #Too small picture, no thumbnail needed
        if pb.get_width() < thumb_size and pb.get_height() < thumb_size:
            return scale(pb, boundary)

        mime = gtk.gdk.pixbuf_get_file_info(path)[0]['mime_types'][0]
        options = {
            "tEXt::Thumb::Image::Width": str(pb.get_width()),
            "tEXt::Thumb::Image::Height": str(pb.get_height()),
            "tEXt::Thumb::URI": uri,
            "tEXt::Thumb::MTime": str(int(mtime(path))),
            "tEXt::Thumb::Size": str(os.path.getsize(fsnative(path))),
            "tEXt::Thumb::Mimetype": mime,
            "tEXt::Software": "QuodLibet"
            }

        pb = scale(pb, (thumb_size, thumb_size))
        pb.save(thumb_path, "png", options)
        os.chmod(thumb_path, 0600)

    return scale(pb, boundary)