Commit 446deafc authored by Gabriel Monnerat's avatar Gabriel Monnerat

Initial commit to imagemagick handler

git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk/utils@43633 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent 377ccb42
##############################################################################
#
# Copyright (c) 2009-2011 Nexedi SA and Contributors. All Rights Reserved.
# Gabriel M. Monnerat <gabriel@tiolive.com>
#
# WARNING: This program as such is intended to be used by professional
# programmers who take the whole responsibility of assessing all potential
# consequences resulting from its eventual inadequacies and bugs
# End users who are looking for a ready-to-use solution with commercial
# guarantees and support are strongly adviced to contract a Free Software
# Service Company
#
# 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 the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
##############################################################################
import re
from zope.interface import implements
from cloudooo.interfaces.handler import IHandler
from cloudooo.file import File
from subprocess import Popen, PIPE
from tempfile import mktemp
class ImageMagickHandler(object):
"""ImageMagicHandler is used to handler images."""
implements(IHandler)
def __init__(self, base_folder_url, data, source_format, **kw):
""" Load pdf document """
self.base_folder_url = base_folder_url
self.document = File(base_folder_url, data, source_format)
self.environment = kw.get("env", {})
def convert(self, destination_format=None, **kw):
"""Convert a image"""
output_url = mktemp(suffix='.%s' % destination_format,
dir=self.base_folder_url)
command = ["convert", self.document.getUrl(), output_url]
stdout, stderr = Popen(command,
stdout=PIPE,
stderr=PIPE,
env=self.environment).communicate()
try:
return open(output_url).read()
finally:
self.document.trash()
def getMetadata(self, base_document=False):
"""Returns a dictionary with all metadata of document.
along with the metadata.
"""
command = ["identify", "-verbose", self.document.getUrl()]
stdout, stderr = Popen(command,
stdout=PIPE,
stderr=PIPE,
env=self.environment).communicate()
metadata_dict = {}
for std in stdout.split("\n"):
std = std.strip()
if re.search("^[a-zA-Z]", std):
if std.count(":") > 1:
key, value = re.compile(".*\:\ ").split(std)
else:
key, value = std.split(":")
metadata_dict[key] = value.strip()
return metadata_dict
def setMetadata(self, metadata={}):
"""Returns image with new metadata.
Keyword arguments:
metadata -- expected an dictionary with metadata.
"""
raise NotImplementedError
#!/usr/bin/env python
##############################################################################
#
# Copyright (c) 2009-2011 Nexedi SA and Contributors. All Rights Reserved.
# Gabriel M. Monnerat <gabriel@tiolive.com>
#
# WARNING: This program as such is intended to be used by professional
# programmers who take the whole responsibility of assessing all potential
# consequences resulting from its eventual inadequacies and bugs
# End users who are looking for a ready-to-use solution with commercial
# guarantees and support are strongly adviced to contract a Free Software
# Service Company
#
# 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 the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
##############################################################################
from cloudooo.handler.tests import runHandlerUnitTest
def run():
runHandlerUnitTest.run("imagemagick")
##############################################################################
#
# Copyright (c) 2009-2010 Nexedi SA and Contributors. All Rights Reserved.
# Gabriel M. Monnerat <gabriel@tiolive.com>
#
# WARNING: This program as such is intended to be used by professional
# programmers who take the whole responsibility of assessing all potential
# consequences resulting from its eventual inadequacies and bugs
# End users who are looking for a ready-to-use solution with commercial
# guarantees and support are strongly adviced to contract a Free Software
# Service Company
#
# 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 the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
##############################################################################
import unittest
import magic
from cloudooo.handler.imagemagick.handler import ImageMagickHandler
from cloudooo.handler.tests.handlerTestCase import HandlerTestCase, make_suite
class TestImageMagickHandler(HandlerTestCase):
def afterSetUp(self):
self.kw = dict(env=dict(PATH=self.env_path))
def testConvertPNGtoJPG(self):
"""Test conversion of png to jpg"""
png_file = open("data/test.png").read()
handler = ImageMagickHandler(self.tmp_url, png_file, "png", **self.kw)
jpg_file = handler.convert("jpg")
mime = magic.Magic(mime=True)
jpg_mimetype = mime.from_buffer(jpg_file)
self.assertEquals("image/jpeg", jpg_mimetype)
def testgetMetadataFromImage(self):
"""Test if metadata is extracted from image correctly"""
png_file = open("data/test.png").read()
handler = ImageMagickHandler(self.tmp_url, png_file, "png", **self.kw)
metadata = handler.getMetadata()
self.assertEquals(metadata.get("Compression"), "Zip")
self.assertEquals(metadata.get("Colorspace"), "RGB")
self.assertEquals(metadata.get("Matte color"), "grey74")
def testsetMetadata(self):
""" Test if metadata are inserted correclty """
handler = ImageMagickHandler(self.tmp_url, "", "png", **self.kw)
self.assertRaises(NotImplementedError, handler.setMetadata)
def test_suite():
return make_suite(TestImageMagickHandler)
##############################################################################
#
# Copyright (c) 2009-2011 Nexedi SA and Contributors. All Rights Reserved.
# Gabriel M. Monnerat <gabriel@tiolive.com>
#
# WARNING: This program as such is intended to be used by professional
# programmers who take the whole responsibility of assessing all potential
# consequences resulting from its eventual inadequacies and bugs
# End users who are looking for a ready-to-use solution with commercial
# guarantees and support are strongly adviced to contract a Free Software
# Service Company
#
# 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 the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
##############################################################################
from cloudooo.handler.tests.handlerTestCase import HandlerTestCase, make_suite
from xmlrpclib import ServerProxy
from os.path import join
from base64 import encodestring, decodestring
from magic import Magic
DAEMON = True
class TestServer(HandlerTestCase):
"""Test XmlRpc Server. Needs cloudooo server started"""
def afterSetUp(self):
"""Creates a connection with cloudooo server"""
self.proxy = ServerProxy("http://%s:%s/RPC2" % \
(self.hostname, self.cloudooo_port), allow_none=True)
def testConvertPNGtoJPG(self):
"""Converts png to jpg"""
data = open(join('data', 'test.png'), 'r').read()
document = self.proxy.convertFile(encodestring(data),
"png",
"jpg")
mime = Magic(mime=True)
mimetype = mime.from_buffer(decodestring(document))
self.assertEquals(mimetype, "image/jpeg")
def testGetMetadataFromPNG(self):
"""test if metadata are extracted correctly"""
data = open(join('data', 'test.png'), 'r').read()
metadata = self.proxy.getFileMetadataItemList(encodestring(data), "png")
self.assertEquals(metadata["Compression"], 'Zip')
def test_suite():
return make_suite(TestServer)
......@@ -34,6 +34,7 @@ from interfaces.manager import IManager, IERP5Compatibility
from cloudooo.handler.ooo.handler import OOHandler
from cloudooo.handler.pdf.handler import PDFHandler
from cloudooo.handler.ffmpeg.handler import FFMPEGHandler
from cloudooo.handler.imagemagick.handler import ImageMagickHandler
from handler.ooo.mimemapper import mimemapper
from utils.utils import logger
from fnmatch import fnmatch
......@@ -41,7 +42,10 @@ import mimetypes
import pkg_resources
HANDLER_DICT = {"pdf": PDFHandler, "ooo": OOHandler, "ffmpeg": FFMPEGHandler}
HANDLER_DICT = {"pdf": PDFHandler,
"ooo": OOHandler,
"ffmpeg": FFMPEGHandler,
"imagemagick": ImageMagickHandler}
def getHandlerObject(source_format, destination_format, mimetype_registry):
......
......@@ -18,5 +18,8 @@ application/vnd.oasis.opendocument.text odt
# Image
#
image/gif gif
#
# Audio
#
audio/mp4 mp4
audio/mp3 mp3
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment