From a1e546a23abb3064e4f9e43f3fde7a6b9448bf15 Mon Sep 17 00:00:00 2001 From: Akanksha Date: Thu, 29 Oct 2015 04:43:03 +0530 Subject: [PATCH] Improve logo validation during package uploading Used PIL to allow support for png, jpeg and gif file formats for upload. An exception is raised if the image size exceeds 500 KiB. Change-Id: Ifd2b8bf689214022664b7bccf846c0de63cc0bfe Closes-Bug:1497257 --- murano/packages/package_base.py | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/murano/packages/package_base.py b/murano/packages/package_base.py index 40598c63f..df989da06 100644 --- a/murano/packages/package_base.py +++ b/murano/packages/package_base.py @@ -128,12 +128,27 @@ class PackageBase(package.Package): self._source_directory, file_name or default_name) if not os.path.isfile(full_path) and not file_name: return + + allowed_ftype = ('png', 'jpeg', 'gif') + allowed_size = 500 * 1024 try: - if imghdr.what(full_path) != 'png': - raise exceptions.PackageLoadError( - '{0} is not in PNG format'.format(what_image)) - with open(full_path) as stream: + + if imghdr.what(full_path) not in allowed_ftype: + msg = _('{0}: Unsupported Format. Only {1} allowed').format( + what_image, ', '.join(allowed_ftype)) + + raise exceptions.PackageLoadError(msg) + + fsize = os.stat(full_path).st_size + if fsize > allowed_size: + msg = _('{0}: Uploaded image size {1} is too large. ' + 'Max allowed size is {2}').format( + what_image, fsize, allowed_size) + raise exceptions.PackageLoadError(msg) + + with open(full_path, 'rb') as stream: return stream.read() + except Exception as ex: trace = sys.exc_info()[2] six.reraise(exceptions.PackageLoadError,