Author: duncan
Date: Mon Jan 1 12:50:16 2007
New Revision: 8891
Modified:
branches/rel-1/freevo/freevo_config.py
branches/rel-1/freevo/src/helpers/cache.py
branches/rel-1/freevo/src/helpers/webserver.py
branches/rel-1/freevo/src/util/fileops.py
branches/rel-1/freevo/src/www/htdocs/library.rpy
Log:
Added webserver cache functions
Added WWW_CACHEDIR as FREEVO_CACHEDIR
Updated cache to cache webserver images
Modified: branches/rel-1/freevo/freevo_config.py
==============================================================================
--- branches/rel-1/freevo/freevo_config.py (original)
+++ branches/rel-1/freevo/freevo_config.py Mon Jan 1 12:50:16 2007
@@ -1753,6 +1753,11 @@
WEBSERVER_GID = 0
#
+# Webserver cache directory
+#
+WWW_CACHEDIR = FREEVO_CACHEDIR
+
+#
# Some sizes for the images in the web library
#
WWW_IMAGE_THUMBNAIL_SIZE = (200, 200)
Modified: branches/rel-1/freevo/src/helpers/cache.py
==============================================================================
--- branches/rel-1/freevo/src/helpers/cache.py (original)
+++ branches/rel-1/freevo/src/helpers/cache.py Mon Jan 1 12:50:16 2007
@@ -7,11 +7,11 @@
#
# Notes:
#
-# Todo:
+# Todo:
#
# -----------------------------------------------------------------------
# Freevo - A Home Theater PC framework
-# Copyright (C) 2002 Krister Lagerstrom, et al.
+# Copyright (C) 2002 Krister Lagerstrom, et al.
# Please see the file freevo/Docs/CREDITS for a complete list of authors.
#
# This program is free software; you can redistribute it and/or modify
@@ -55,10 +55,12 @@
delete old files from previous versions of freevo which are not
needed anymore
"""
+ #TODO Add WWW_LINK_CACHE and WWW_IMAGE_CACNE
print 'deleting old cache files from older freevo version....',
sys.__stdout__.flush()
del_list = []
+ #for name in ('image-viewer-thumb.jpg', 'thumbnails', 'audio', 'mmpython',
'disc', 'image_cache', 'link_cache'):
for name in ('image-viewer-thumb.jpg', 'thumbnails', 'audio', 'mmpython',
'disc'):
if os.path.exists(os.path.join(config.FREEVO_CACHEDIR, name)):
del_list.append(os.path.join(config.FREEVO_CACHEDIR, name))
@@ -75,13 +77,14 @@
util.rmrf(f)
else:
os.unlink(f)
- print 'deleted %s file(s)' % len(del_list)
+ print 'deleted %s file%s' % (len(del_list), len(del_list) == 1 and '' or
's')
def delete_old_files_2():
"""
delete cachfiles/entries for files which don't exists anymore
"""
+ #TODO Add WWW_LINK_CACHE and WWW_IMAGE_CACNE
print 'deleting old cachefiles...............................',
sys.__stdout__.flush()
num = 0
@@ -91,7 +94,7 @@
if not vfs.isfile(file[len(config.OVERLAY_DIR):-4]):
os.unlink(file)
num += 1
- print 'deleted %s file(s)' % num
+ print 'deleted %s file%s' % (num, num == 1 and '' or 's')
print 'deleting cache for directories not existing anymore...',
subdirs = util.get_subdirs_recursively(config.OVERLAY_DIR)
@@ -119,7 +122,7 @@
del data[key]
util.save_pickle(data, filename)
print 'done'
-
+
def cache_directories(rebuild):
"""
@@ -142,7 +145,7 @@
if os.path.isdir(d[1]):
all_dirs.append(d[1])
util.mediainfo.cache_recursive(all_dirs, verbose=True)
-
+
def cache_thumbnails():
"""
@@ -150,7 +153,7 @@
"""
import cStringIO
import stat
-
+
print 'checking thumbnails...................................',
sys.__stdout__.flush()
@@ -175,15 +178,15 @@
except OSError:
pass
- for bad_dir in ('.xvpics', '.thumbnails', '.pics'):
+ for bad_dir in ('.svn', '.xvpics', '.thumbnails', '.pics'):
if filename.find('/' + bad_dir + '/') > 0:
try:
files.remove(filename)
except:
pass
-
- print '%s file(s)' % len(files)
-
+
+ print '%s file%s' % (len(files), len(files) == 1 and '' or 's')
+
for filename in files:
fname = filename
if len(fname) > 65:
@@ -195,7 +198,61 @@
if files:
print
-
+
+def cache_www_thumbnails():
+ """
+ cache all image files for web server by creating thumbnails
+ """
+ import cStringIO
+ import stat
+
+ print 'checking www thumbnails...............................',
+ sys.__stdout__.flush()
+
+ files = []
+ for d in config.VIDEO_ITEMS + config.AUDIO_ITEMS + config.IMAGE_ITEMS:
+ try:
+ d = d[1]
+ except:
+ pass
+ if not os.path.isdir(d):
+ continue
+ files += util.match_files_recursively(d, config.IMAGE_SUFFIX) + \
+ util.match_files_recursively(vfs.getoverlay(d),
config.IMAGE_SUFFIX)
+
+ cache_dir = util.fileops.www_image_cachedir()
+ files = util.misc.unique(files)
+ for filename in copy.copy(files):
+ sinfo = os.stat(filename)
+ filepath = filename.replace("/", "_").replace(".", "_") + ".jpg"
+ thumb = os.path.join(cache_dir, filepath)
+ try:
+ if os.stat(thumb)[stat.ST_MTIME] > sinfo[stat.ST_MTIME]:
+ files.remove(filename)
+ except OSError:
+ pass
+
+ for bad_dir in ('.svn', '.xvpics', '.thumbnails', '.pics'):
+ if filename.find('/' + bad_dir + '/') > 0:
+ try:
+ files.remove(filename)
+ except:
+ pass
+
+ print '%s file%s' % (len(files), len(files) == 1 and '' or 's')
+
+ for filename in files:
+ fname = filename
+ if len(fname) > 65:
+ fname = fname[:20] + ' [...] ' + fname[-40:]
+ print ' %4d/%-4d %s' % (files.index(filename)+1, len(files), fname)
+
+ util.cache_www_image(filename)
+
+ if files:
+ print
+
+
def create_metadata():
"""
scan files and create metadata
@@ -229,7 +286,7 @@
elif util.match_suffix(dir[1], fxditem.mimetype.suffix()):
fxd.append(dir[1])
-
+
items = playlist.mimetype.get(None, util.misc.unique(pl))
# ignore fxd files for now, they can't store metainfo
@@ -278,7 +335,7 @@
rec = util.get_subdirs_recursively(d)
subdirs['all'] += rec
subdirs[type] += rec
-
+
subdirs['all'] = util.misc.unique(subdirs['all'])
subdirs['all'].sort(lambda l, o: cmp(l.upper(), o.upper()))
@@ -336,7 +393,7 @@
print
print_help()
sys.exit(1)
-
+
if __name__ == "__main__":
os.umask(config.UMASK)
if len(sys.argv)>1 and sys.argv[1] == '--help':
@@ -352,7 +409,7 @@
files = util.match_files_recursively(dirname,
config.VIDEO_SUFFIX)
else:
print_error_and_exit()
-
+
elif os.path.isdir(sys.argv[2]):
dirname = os.path.abspath(sys.argv[2])
files = util.match_files(dirname, config.VIDEO_SUFFIX)
@@ -406,7 +463,7 @@
print 'Cache too old, forcing rebuild'
rebuild = 2
complete_update = int(time.time())
-
+
except ImportError:
print
print 'Error: unable to read kaa.metadata version information'
@@ -418,14 +475,14 @@
print
start = time.clock()
-
+
activate_plugins = []
for type in ('video', 'audio', 'image', 'games'):
if plugin.is_active(type):
# activate all mimetype plugins
plugin.init_special_plugin(type)
activate_plugins.append(type)
-
+
for type in 'VIDEO', 'AUDIO', 'IMAGE':
for d in copy.copy(getattr(config, '%s_ITEMS' % type)):
if not isstring(d):
@@ -441,13 +498,14 @@
# we have time here, don't use exif thumbnails
config.IMAGE_USE_EXIF_THUMBNAIL = 0
-
+
cache_directories(rebuild)
if config.CACHE_IMAGES:
cache_thumbnails()
+ cache_www_thumbnails()
create_metadata()
create_tv_pickle()
-
+
# close db
util.mediainfo.sync()
Modified: branches/rel-1/freevo/src/helpers/webserver.py
==============================================================================
--- branches/rel-1/freevo/src/helpers/webserver.py (original)
+++ branches/rel-1/freevo/src/helpers/webserver.py Mon Jan 1 12:50:16 2007
@@ -105,7 +105,7 @@
(title, path) = item
root.putChild(path.replace("/", "_"), static.File(path))
root.putChild(config.TV_RECORD_DIR.replace("/", "_"),
static.File(config.TV_RECORD_DIR))
- root.putChild(config.FREEVO_CACHEDIR.replace("/", "_"),
static.File(config.FREEVO_CACHEDIR))
+ root.putChild(config.WWW_CACHEDIR.replace("/", "_"),
static.File(config.WWW_CACHEDIR))
root.putChild('vhost', vhost.VHostMonsterResource())
rewriter = rewrite.RewriterResource(root, helpimagesrewrite)
Modified: branches/rel-1/freevo/src/util/fileops.py
==============================================================================
--- branches/rel-1/freevo/src/util/fileops.py (original)
+++ branches/rel-1/freevo/src/util/fileops.py Mon Jan 1 12:50:16 2007
@@ -40,7 +40,7 @@
import traceback
# image stuff
-import kaa.imlib2 as Image
+import kaa.imlib2 as imlib2
from kaa.metadata.image import EXIF as exif
@@ -472,7 +472,7 @@
if thumbnail:
try:
- image = Image.open_from_memory(thumbnail)
+ image = imlib2.open_from_memory(thumbnail)
except Exception, e:
print 'Invalid thumbnail for %s' % filename
if config.DEBUG:
@@ -486,7 +486,7 @@
f.close()
if tags.has_key('JPEGThumbnail'):
- image = Image.open_from_memory(tags['JPEGThumbnail'])
+ image = imlib2.open_from_memory(tags['JPEGThumbnail'])
except Exception, e:
print 'Error loading thumbnail %s' % filename
if config.DEBUG:
@@ -494,7 +494,7 @@
if not image or image.width < 100 or image.height < 100:
try:
- image = Image.open(filename)
+ image = imlib2.open(filename)
except Exception, e:
print 'error caching image %s' % filename
if config.DEBUG:
@@ -540,3 +540,40 @@
return create_thumbnail(filename, thumbnail)
+
+def www_link_cachedir():
+ '''returns the www link cache directory name
+ if the directory does not exist it is created
+ '''
+ cache_dir = '%s/link_cache/' % (config.WWW_CACHEDIR)
+ if not os.path.isdir(cache_dir):
+ os.mkdir(cache_dir,
stat.S_IMODE(os.stat(config.WWW_CACHEDIR)[stat.ST_MODE]))
+ return cache_dir
+
+
+def www_image_cachedir():
+ '''returns the www image cache directory name
+ if the directory does not exist it is created
+ '''
+ cache_dir = '%s/image_cache/' % (config.WWW_CACHEDIR)
+ if not os.path.isdir(cache_dir):
+ os.mkdir(cache_dir,
stat.S_IMODE(os.stat(config.WWW_CACHEDIR)[stat.ST_MODE]))
+ return cache_dir
+
+
+def cache_www_image(filename):
+ '''cache an image for webservers
+ when the image is larger than WWW_IMAGE_THRESHOLD_SIZE then the image size
+ is returned. Otherwise the thumbnail size is returned
+ '''
+ threshold_size = config.WWW_IMAGE_THRESHOLD_SIZE
+ thumbnail_size = config.WWW_IMAGE_THUMBNAIL_SIZE
+ cache_dir = www_image_cache()
+ imagepath = filename.replace("/", "_").replace(".", "_") + ".jpg"
+ thumb_path = os.path.join(cache_dir, imagepath)
+ image = imlib2.open(filename)
+ thumb = image.scale_preserve_aspect(config.WWW_IMAGE_THUMBNAIL_SIZE)
+ thumb.save(thumb_path)
+ if image.size[0] < threshold_image.size[0] and image.size[1] <
threshold_image.size[1]:
+ return image.size
+ return thumb.size
Modified: branches/rel-1/freevo/src/www/htdocs/library.rpy
==============================================================================
--- branches/rel-1/freevo/src/www/htdocs/library.rpy (original)
+++ branches/rel-1/freevo/src/www/htdocs/library.rpy Mon Jan 1 12:50:16 2007
@@ -53,9 +53,7 @@
def __init__(self):
print '__init__(self)'
- self.cache_dir = '%s/image_cache/' % (config.FREEVO_CACHEDIR)
- if not os.path.isdir(self.cache_dir):
- os.mkdir(self.cache_dir,
stat.S_IMODE(os.stat(config.FREEVO_CACHEDIR)[stat.ST_MODE]))
+ self.cache_dir = util.fileops.www_image_cachedir()
self.allowed_dirs = []
self.allowed_dirs.extend(config.VIDEO_ITEMS)
self.allowed_dirs.extend(config.AUDIO_ITEMS)
@@ -80,9 +78,9 @@
'''
print 'convert_dir(self, dir_str=%r)' % (dir_str)
child_res = ""
- ### if the file starts with FREEVO_CACHEDIR return converted file
- if dir_str.startswith(config.FREEVO_CACHEDIR):
- child_res = config.FREEVO_CACHEDIR
+ ### if the file starts with WWW_CACHEDIR return converted file
+ if dir_str.startswith(config.WWW_CACHEDIR):
+ child_res = config.WWW_CACHEDIR
else:
for i in range(len(self.allowed_dirs)):
val = self.allowed_dirs[i][1]
-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys - and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
Freevo-cvslog mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/freevo-cvslog