Skip to content

Commit 10be4ae

Browse files
authored
fixed file size check for outputs (#580)
* fixed file size check for outputs * use humanize * updated docs for maxsingleinputsize option
1 parent 41eef5c commit 10be4ae

3 files changed

Lines changed: 23 additions & 11 deletions

File tree

docs/configuration.rst

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,10 @@ configuration file <https://docs.pycsw.org/en/latest/configuration.html>`_.
113113
number of processor cores. -1 for no limit.
114114

115115
:maxrequestsize:
116-
maximal request size. 0 for no limit
116+
maximal request size. 0 for no limit.
117+
118+
:maxsingleinputsize:
119+
maximal request size for a single input. 0 for no limit.
117120

118121
:maxprocesses:
119122
maximal number of requests being stored in queue, waiting till they can be

pywps/inout/basic.py

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
from collections import namedtuple
3131
from copy import deepcopy
3232
from io import BytesIO
33+
import humanize
3334

3435

3536
_SOURCE_TYPE = namedtuple('SOURCE_TYPE', 'MEMORY, FILE, STREAM, DATA, URL')
@@ -446,12 +447,15 @@ def url(self, value):
446447

447448
@property
448449
def file(self):
450+
"""Downloads URL and return file pointer.
451+
Checks if size is allowed before download.
452+
"""
449453
if self._file is not None:
450454
return self._file
451455

452456
self._file = self._build_file_name(href=self.url)
453457

454-
max_byte_size = self.max_input_size()
458+
max_byte_size = self.max_size()
455459

456460
# Create request
457461
try:
@@ -460,21 +464,24 @@ def file(self):
460464
except Exception as e:
461465
raise NoApplicableCode('File reference error: {}'.format(e))
462466

463-
error_message = 'File size for input "{}" exceeded. Maximum allowed: {} megabytes'.format(
464-
self.inpt.get('identifier', '?'), max_byte_size)
467+
error_message = 'File size for input "{}" exceeded. Maximum allowed: {}'.format(
468+
self.inpt.get('identifier', '?'), humanize.naturalsize(max_byte_size))
465469

466-
if int(data_size) > int(max_byte_size):
467-
raise FileSizeExceeded(error_message)
470+
if int(max_byte_size) > 0:
471+
if int(data_size) > int(max_byte_size):
472+
raise FileSizeExceeded(error_message)
468473

469474
try:
470475
with open(self._file, 'wb') as f:
471476
data_size = 0
472477
for chunk in reference_file.iter_content(chunk_size=1024):
473478
data_size += len(chunk)
474-
if int(data_size) > int(max_byte_size):
475-
raise FileSizeExceeded(error_message)
479+
if int(max_byte_size) > 0:
480+
if int(data_size) > int(max_byte_size):
481+
raise FileSizeExceeded(error_message)
476482
f.write(chunk)
477-
483+
except FileSizeExceeded:
484+
raise
478485
except Exception as e:
479486
raise NoApplicableCode(e)
480487

@@ -511,14 +518,15 @@ def _openurl(href, data=None):
511518
return req
512519

513520
@staticmethod
514-
def max_input_size():
521+
def max_size():
515522
"""Calculates maximal size for input file based on configuration
516523
and units.
517524
518525
:return: maximum file size in bytes
519526
"""
520527
ms = config.get_config_value('server', 'maxsingleinputsize')
521-
return config.get_size_mb(ms) * 1024**2
528+
byte_size = config.get_size_mb(ms) * 1024**2
529+
return byte_size
522530

523531

524532
class SimpleHandler(DataHandler):

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ python-dateutil
66
requests
77
SQLAlchemy
88
werkzeug
9+
humanize

0 commit comments

Comments
 (0)