1
# -*- coding: utf-8 -*-
2
# Moovida - Home multimedia server
3
# Copyright (C) 2006-2009 Fluendo Embedded S.L. (www.fluendo.com).
6
# This file is available under one of two license agreements.
8
# This file is licensed under the GPL version 3.
9
# See "LICENSE.GPL" in the root of this distribution including a special
10
# exception to use Moovida with Fluendo's plugins.
12
# The GPL part of Moovida is also available under a commercial licensing
13
# agreement from Fluendo.
14
# See "LICENSE.Moovida" in the root directory of this distribution package
15
# for details on that license.
17
# Author: Olivier Tilloy <olivier@fluendo.com>
20
Basic utility functions.
23
from elisa.core import common
24
from elisa.core.utils import defer, caching
29
def get_and_cache_image(image_uri):
31
Get and cache an image for which we know a resource provider is available.
33
If the image is local or already cached on disk, do not perform the
36
The get request that may be performed to obtain the image is expected to
37
return a L{elisa.plugins.base.models.media.RawDataModel}.
39
@param image_uri: the URI of the image
40
@type image_uri: L{elisa.core.media_uri.MediaUri}
42
@return: a deferred fired when the image was cached, with the full path to
44
@rtype: L{elisa.core.utils.defer.Deferred} with C{unicode}
46
cache_file = caching.get_cached_image_path(image_uri)
47
if os.path.exists(cache_file):
48
return defer.succeed(cache_file)
50
def cache_data_to_file(model, cache_file):
51
return caching.cache_to_file(model.data, cache_file)
53
model, dfr = common.application.resource_manager.get(image_uri)
54
dfr.addCallback(cache_data_to_file, cache_file)
58
def get_and_cache_thumbnail(item, thumbnail_uri):
60
Get and cache a thumbnail for a given item.
62
If the thumbnail is local or already cached on disk, do not perform the
65
The given thumbnail URI is assumed to have a corresponding resource
66
provider registered and available (in case it is not a local resource).
68
The get request that may be performed to obtain the thumbnail is expected
69
to return a L{elisa.plugins.base.models.media.RawDataModel}.
71
@param item: any model for which we want to request a thumbnail
72
@param thumbnail_uri: the URI to the distant thumbnail
73
@type thumbnail_uri: L{elisa.core.media_uri.MediaUri}
75
@return: a deferred fired when the thumbnail was cached, with the full path
77
@rtype: L{elisa.core.utils.defer.Deferred} with C{unicode}
79
# This is a two-level caching mechanism:
80
# 1) First check if the item has a "thumbnail_path" attribute. If it does,
81
# trust that the referenced thumbnail exists and is valid;
82
# 2) If not, compute the thumbnail path and get it where needed.
84
# Note: the reference to the local path of the thumbnail being stored
85
# in the item's "thumbnail_path" attribute is a convention that was
86
# historically used here and there in various places of the code.
87
# Change where it is stored at your own risk! It may (or may not) break
88
# thumbnailing in various places, depending on how it was first
90
return defer.succeed(item.thumbnail_path)
91
except AttributeError:
92
def set_thumbnail_path(thumbnail_path, item):
93
item.thumbnail_path = thumbnail_path
96
dfr = get_and_cache_image(thumbnail_uri)
97
dfr.addCallback(set_thumbnail_path, item)