Merge "Fix unreachable 'ImageSizeLimitExceeded' exception in image-upload"

This commit is contained in:
Zuul 2018-06-05 02:07:12 +00:00 committed by Gerrit Code Review
commit 08b87aaa3b
2 changed files with 10 additions and 9 deletions

View File

@ -271,20 +271,23 @@ class LimitingReader(object):
Reader designed to fail when reading image data past the configured
allowable amount.
"""
def __init__(self, data, limit):
def __init__(self, data, limit,
exception_class=exception.ImageSizeLimitExceeded):
"""
:param data: Underlying image data object
:param limit: maximum number of bytes the reader should allow
:param exception_class: Type of exception to be raised
"""
self.data = data
self.limit = limit
self.bytes_read = 0
self.exception_class = exception_class
def __iter__(self):
for chunk in self.data:
self.bytes_read += len(chunk)
if self.bytes_read > self.limit:
raise exception.ImageSizeLimitExceeded()
raise self.exception_class()
else:
yield chunk
@ -292,7 +295,7 @@ class LimitingReader(object):
result = self.data.read(i)
self.bytes_read += len(result)
if self.bytes_read > self.limit:
raise exception.ImageSizeLimitExceeded()
raise self.exception_class()
return result

View File

@ -299,12 +299,10 @@ class ImageProxy(glance.domain.proxy.Image):
if remaining is not None:
# NOTE(jbresnah) we are trying to enforce a quota, put a limit
# reader on the data
data = utils.LimitingReader(data, remaining)
try:
self.image.set_data(data, size=size)
except exception.ImageSizeLimitExceeded:
raise exception.StorageQuotaFull(image_size=size,
remaining=remaining)
data = utils.LimitingReader(
data, remaining, exception_class=exception.StorageQuotaFull)
self.image.set_data(data, size=size)
# NOTE(jbresnah) If two uploads happen at the same time and neither
# properly sets the size attribute[1] then there is a race condition