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
This commit is contained in:
Akanksha 2015-10-29 04:43:03 +05:30 committed by Stan Lagun
parent 74d6d2207c
commit a1e546a23a
1 changed files with 19 additions and 4 deletions

View File

@ -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,