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:
parent
74d6d2207c
commit
a1e546a23a
@ -128,12 +128,27 @@ class PackageBase(package.Package):
|
|||||||
self._source_directory, file_name or default_name)
|
self._source_directory, file_name or default_name)
|
||||||
if not os.path.isfile(full_path) and not file_name:
|
if not os.path.isfile(full_path) and not file_name:
|
||||||
return
|
return
|
||||||
|
|
||||||
|
allowed_ftype = ('png', 'jpeg', 'gif')
|
||||||
|
allowed_size = 500 * 1024
|
||||||
try:
|
try:
|
||||||
if imghdr.what(full_path) != 'png':
|
|
||||||
raise exceptions.PackageLoadError(
|
if imghdr.what(full_path) not in allowed_ftype:
|
||||||
'{0} is not in PNG format'.format(what_image))
|
msg = _('{0}: Unsupported Format. Only {1} allowed').format(
|
||||||
with open(full_path) as stream:
|
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()
|
return stream.read()
|
||||||
|
|
||||||
except Exception as ex:
|
except Exception as ex:
|
||||||
trace = sys.exc_info()[2]
|
trace = sys.exc_info()[2]
|
||||||
six.reraise(exceptions.PackageLoadError,
|
six.reraise(exceptions.PackageLoadError,
|
||||||
|
Loading…
Reference in New Issue
Block a user