# HG changeset patch # User paulb # Date 1193182087 0 # Node ID f12f4511f75ad741f739dfbf79e738b90393b15a # Parent f088392648a8986c2f52c91db8485b4a50ab4040 [project @ 2007-10-23 23:28:07 by paulb] Initialise FileContent fields with streams instead of strings. Make content a property of FileContent rather than a normal instance attribute. diff -r f088392648a8 -r f12f4511f75a WebStack/Helpers/Request.py --- a/WebStack/Helpers/Request.py Tue Oct 23 23:27:17 2007 +0000 +++ b/WebStack/Helpers/Request.py Tue Oct 23 23:28:07 2007 +0000 @@ -58,7 +58,7 @@ self.length = self.length - len(data) return data - def readline(self): + def readline(self, n=None): "Reads a single line of data from the message body." @@ -176,20 +176,35 @@ Instances of this class contain the following attributes: + * stream - a stream object through which the content of an uploaded file + may be accessed * content - a plain string containing the contents of the uploaded file * headers - a dictionary containing the headers associated with the uploaded file """ - def __init__(self, content, headers=None): + def __init__(self, stream, headers=None): """ - Initialise the object with 'content' and optional 'headers' describing - the content. + Initialise the object with a 'stream' through which the file can be + read, along with optional 'headers' describing the content. """ - self.content = content + self.stream = stream self.headers = headers or {} + self.cache = None + + def __getattr__(self, name): + if name != "content": + raise AttributeError, name + if self.cache is not None: + return self.cache + if hasattr(self.stream, "seek"): + self.stream.seek(0) + return self.stream.read() + else: + self.cache = self.stream.read() + return self.cache def __str__(self): return self.content @@ -280,9 +295,9 @@ # Detect stray FileUpload objects (eg. with Zope). if hasattr(field_value, "read"): - return FileContent(field_value.read(), parse_headers(field_value.headers)) + return FileContent(field_value, parse_headers(field_value.headers)) else: - return FileContent(field_value.value, parse_headers(field_value.headers)) + return FileContent(field_value.file, parse_headers(field_value.headers)) else: return get_body_field(field_value, encoding)