open up media asset type for all kinds of external files
git-svn-id: svn://svn.cy55.de/Zope3/src/cybertools/trunk@3204 fd906abe-77d9-0310-91a1-e0d9ade77398
This commit is contained in:
parent
cb373433fa
commit
2fc1345ffa
4 changed files with 33 additions and 5 deletions
|
@ -1,5 +1,5 @@
|
||||||
#
|
#
|
||||||
# Copyright (c) 2007 Helmut Merz helmutm@cy55.de
|
# Copyright (c) 2009 Helmut Merz helmutm@cy55.de
|
||||||
#
|
#
|
||||||
# This program is free software; you can redistribute it and/or modify
|
# This program is free software; you can redistribute it and/or modify
|
||||||
# it under the terms of the GNU General Public License as published by
|
# it under the terms of the GNU General Public License as published by
|
||||||
|
|
|
@ -80,7 +80,7 @@ class MediaAssetFile(object):
|
||||||
|
|
||||||
def getContentType(self, variant=None):
|
def getContentType(self, variant=None):
|
||||||
contentType = self.getMimeType()
|
contentType = self.getMimeType()
|
||||||
if variant == None:
|
if variant is None:
|
||||||
return contentType
|
return contentType
|
||||||
outputFormat = None
|
outputFormat = None
|
||||||
# Scan all statements for a defintion of an output format
|
# Scan all statements for a defintion of an output format
|
||||||
|
@ -94,7 +94,7 @@ class MediaAssetFile(object):
|
||||||
# Return default type if no defintion was found
|
# Return default type if no defintion was found
|
||||||
if not outputFormat:
|
if not outputFormat:
|
||||||
baseType = getMimeBasetype(contentType)
|
baseType = getMimeBasetype(contentType)
|
||||||
return DEFAULT_FORMATS.get(baseType)
|
return DEFAULT_FORMATS.get(baseType) or contentType
|
||||||
return outputFormat
|
return outputFormat
|
||||||
|
|
||||||
def transform(self, rules=None):
|
def transform(self, rules=None):
|
||||||
|
|
|
@ -48,12 +48,21 @@ class PILTransform(object):
|
||||||
implements(IFileTransform)
|
implements(IFileTransform)
|
||||||
|
|
||||||
def open(self, path):
|
def open(self, path):
|
||||||
|
try:
|
||||||
self.im = Image.open(path)
|
self.im = Image.open(path)
|
||||||
|
except IOError, e:
|
||||||
|
from logging import getLogger
|
||||||
|
getLogger('cybertools.media.piltransform.PILTransform').warn(e)
|
||||||
|
self.im = None
|
||||||
|
|
||||||
def rotate(self, angle, resize):
|
def rotate(self, angle, resize):
|
||||||
|
if self.im is None:
|
||||||
|
return
|
||||||
self.im = self.im.rotate(angle,Image.BICUBIC)
|
self.im = self.im.rotate(angle,Image.BICUBIC)
|
||||||
|
|
||||||
def color(self, mode):
|
def color(self, mode):
|
||||||
|
if self.im is None:
|
||||||
|
return
|
||||||
if not mode:
|
if not mode:
|
||||||
return
|
return
|
||||||
mode = mode.upper()
|
mode = mode.upper()
|
||||||
|
@ -64,6 +73,8 @@ class PILTransform(object):
|
||||||
self.im = self.im.convert(mode)
|
self.im = self.im.convert(mode)
|
||||||
|
|
||||||
def crop(self, relWidth, relHeight, alignX=0.5, alignY=0.5):
|
def crop(self, relWidth, relHeight, alignX=0.5, alignY=0.5):
|
||||||
|
if self.im is None:
|
||||||
|
return
|
||||||
alignX = min(max(alignX, 0.0), 1.0)
|
alignX = min(max(alignX, 0.0), 1.0)
|
||||||
alignY = min(max(alignY, 0.0), 1.0)
|
alignY = min(max(alignY, 0.0), 1.0)
|
||||||
w, h = self.im.size
|
w, h = self.im.size
|
||||||
|
@ -83,9 +94,13 @@ class PILTransform(object):
|
||||||
self.im = self.im.crop(box)
|
self.im = self.im.crop(box)
|
||||||
|
|
||||||
def resize(self, width, height):
|
def resize(self, width, height):
|
||||||
|
if self.im is None:
|
||||||
|
return
|
||||||
dims = (width, height)
|
dims = (width, height)
|
||||||
self.im.thumbnail(dims, Image.ANTIALIAS)
|
self.im.thumbnail(dims, Image.ANTIALIAS)
|
||||||
|
|
||||||
def save(self, path, mimetype):
|
def save(self, path, mimetype):
|
||||||
|
if self.im is None:
|
||||||
|
return
|
||||||
format = mimetypeToPIL(mimetype)
|
format = mimetypeToPIL(mimetype)
|
||||||
self.im.save(path)
|
self.im.save(path)
|
||||||
|
|
|
@ -44,7 +44,9 @@ class FileSystemStorage(object):
|
||||||
def getDir(self, address, subDir=None):
|
def getDir(self, address, subDir=None):
|
||||||
subDir = subDir or self.subDir
|
subDir = subDir or self.subDir
|
||||||
if self.rootDir is None:
|
if self.rootDir is None:
|
||||||
|
if subDir:
|
||||||
return os.path.join(subDir, address)
|
return os.path.join(subDir, address)
|
||||||
|
return address
|
||||||
return os.path.join(self.rootDir, subDir, address)
|
return os.path.join(self.rootDir, subDir, address)
|
||||||
|
|
||||||
def setData(self, address, data, params={}):
|
def setData(self, address, data, params={}):
|
||||||
|
@ -65,6 +67,7 @@ class FileSystemStorage(object):
|
||||||
def getData(self, address, params={}):
|
def getData(self, address, params={}):
|
||||||
subDir = params.get('subdirectory')
|
subDir = params.get('subdirectory')
|
||||||
fn = self.getDir(address, subDir)
|
fn = self.getDir(address, subDir)
|
||||||
|
#print '***', [self.rootDir, subDir, address], fn
|
||||||
try:
|
try:
|
||||||
f = open(fn, 'rb')
|
f = open(fn, 'rb')
|
||||||
data = f.read()
|
data = f.read()
|
||||||
|
@ -76,6 +79,16 @@ class FileSystemStorage(object):
|
||||||
#'File %r cannot be read.' % fn)
|
#'File %r cannot be read.' % fn)
|
||||||
return ''
|
return ''
|
||||||
|
|
||||||
|
def getSize(self, address, params={}):
|
||||||
|
subDir = params.get('subdirectory')
|
||||||
|
fn = self.getDir(address, subDir)
|
||||||
|
try:
|
||||||
|
return os.path.getsize(fn)
|
||||||
|
except IOError, e:
|
||||||
|
from logging import getLogger
|
||||||
|
getLogger('cybertools.storage.filesystem.FileSystemStorage').warn(e)
|
||||||
|
return 0
|
||||||
|
|
||||||
def getUniqueAddress(self, address, params={}):
|
def getUniqueAddress(self, address, params={}):
|
||||||
subDir = params.get('subdirectory')
|
subDir = params.get('subdirectory')
|
||||||
return self.getDir(address, subDir)
|
return self.getDir(address, subDir)
|
||||||
|
|
Loading…
Add table
Reference in a new issue