1.1 --- a/WebStack/Helpers/Request.py Fri Feb 13 22:56:01 2009 +0100
1.2 +++ b/WebStack/Helpers/Request.py Fri Mar 20 22:45:11 2009 +0100
1.3 @@ -3,7 +3,7 @@
1.4 """
1.5 Request helper classes.
1.6
1.7 -Copyright (C) 2004, 2005, 2006 Paul Boddie <paul@boddie.org.uk>
1.8 +Copyright (C) 2004, 2005, 2006, 2007, 2008 Paul Boddie <paul@boddie.org.uk>
1.9
1.10 This library is free software; you can redistribute it and/or
1.11 modify it under the terms of the GNU Lesser General Public
1.12 @@ -215,6 +215,12 @@
1.13 self.name = name
1.14 self.value = value
1.15
1.16 +class FileTooLargeError(Exception):
1.17 +
1.18 + "An exception indicating that an uploaded file was too large."
1.19 +
1.20 + pass
1.21 +
1.22 class FileContent:
1.23
1.24 """
1.25 @@ -232,17 +238,22 @@
1.26 uploaded file
1.27 * headers - a dictionary containing the headers associated with the
1.28 uploaded file
1.29 + * limit - a limit, if previously specified, on the size of uploaded
1.30 + content
1.31 """
1.32
1.33 - def __init__(self, stream, headers=None):
1.34 + def __init__(self, stream, headers=None, limit=None):
1.35
1.36 """
1.37 Initialise the object with a 'stream' through which the file can be
1.38 - read, along with optional 'headers' describing the content.
1.39 + read, along with optional 'headers' describing the content. An optional
1.40 + 'limit' can be specified to state the maximum number of bytes that may
1.41 + be read before the content is considered too large.
1.42 """
1.43
1.44 self.stream = stream
1.45 self.headers = headers or HeaderDict()
1.46 + self.limit = limit
1.47 self.cache = None
1.48
1.49 def __getattr__(self, name):
1.50 @@ -258,9 +269,9 @@
1.51 return self.cache
1.52
1.53 if self.reset():
1.54 - return self.stream.read()
1.55 + return self._read()
1.56 else:
1.57 - self.cache = self.stream.read()
1.58 + self.cache = self._read()
1.59 return self.cache
1.60
1.61 elif name == "filename":
1.62 @@ -273,6 +284,22 @@
1.63 else:
1.64 raise AttributeError, name
1.65
1.66 + def _read(self):
1.67 +
1.68 + """
1.69 + Read from the stream up to any limit, raising an exception if the
1.70 + limit is exceeded.
1.71 + """
1.72 +
1.73 + if self.limit is not None:
1.74 + s = self.stream.read(self.limit)
1.75 + if self.stream.read(1):
1.76 + raise FileTooLargeError
1.77 + else:
1.78 + return s
1.79 + else:
1.80 + return self.stream.read()
1.81 +
1.82 def reset(self):
1.83
1.84 "Reset the stream providing the data, returning whether this succeeded."
2.1 --- a/WebStack/Repositories/Directory.py Fri Feb 13 22:56:01 2009 +0100
2.2 +++ b/WebStack/Repositories/Directory.py Fri Mar 20 22:45:11 2009 +0100
2.3 @@ -233,6 +233,7 @@
2.4
2.5 path = self.full_path(key)
2.6 edit_path = self.edit_path(key)
2.7 + # NOTE: Add fsync-related stuff here due to ext4 and other filesystems?
2.8 os.rename(edit_path, path)
2.9
2.10 def __delitem__(self, key):