comparison MoinMoin/action/AttachFile.py @ 3568:6fe1ea4d9d1a

use the open temporary file for file uploads (fixes big memory consumption for big file uploads) - Twisted and mod_python is completely untested
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Thu, 08 May 2008 10:10:12 +0200
parents 1052c105b16f
children 870cc4c47705
comparison
equal deleted inserted replaced
3567:ddf31f2ae8e3 3568:6fe1ea4d9d1a
166 'link': wikiutil.escape(link) 166 'link': wikiutil.escape(link)
167 } 167 }
168 return "\n<p>\n%s\n</p>\n" % attach_info 168 return "\n<p>\n%s\n</p>\n" % attach_info
169 169
170 170
171 def _write_stream(content, stream, bufsize=8192):
172 if isinstance(content, str):
173 stream.write(content)
174 elif isinstance(content, file):
175 import shutil
176 shutil.copyfileobj(content, stream, bufsize)
177
171 def add_attachment(request, pagename, target, filecontent, overwrite=0): 178 def add_attachment(request, pagename, target, filecontent, overwrite=0):
179 """ save <filecontent> to an attachment <target> of page <pagename>
180
181 filecontent can be either a str (in memory file content),
182 or an open file object (file content in e.g. a tempfile).
183 """
172 _ = request.getText 184 _ = request.getText
173 185
174 # replace illegal chars 186 # replace illegal chars
175 target = wikiutil.taintfilename(target) 187 target = wikiutil.taintfilename(target)
176 188
178 attach_dir = getAttachDir(request, pagename, create=1) 190 attach_dir = getAttachDir(request, pagename, create=1)
179 # save file 191 # save file
180 fpath = os.path.join(attach_dir, target).encode(config.charset) 192 fpath = os.path.join(attach_dir, target).encode(config.charset)
181 exists = os.path.exists(fpath) 193 exists = os.path.exists(fpath)
182 if exists and not overwrite: 194 if exists and not overwrite:
183 msg = _("Attachment '%(target)s' already exists.") % {'target': target, } 195 filesize = 0
196 msg = _("Attachment '%(target)s' already exists.") % {'target': target, } # XXX unused?
184 else: 197 else:
185 if exists: 198 if exists:
186 try: 199 try:
187 os.remove(fpath) 200 os.remove(fpath)
188 except: 201 except:
189 pass 202 pass
190 stream = open(fpath, 'wb') 203 stream = open(fpath, 'wb')
191 try: 204 try:
192 stream.write(filecontent) 205 _write_stream(filecontent, stream)
193 finally: 206 finally:
194 stream.close() 207 stream.close()
195 208
196 _addLogEntry(request, 'ATTNEW', pagename, target) 209 _addLogEntry(request, 'ATTNEW', pagename, target)
197 210
198 event = FileAttachedEvent(request, pagename, target, len(filecontent)) 211 filesize = os.path.getsize(fpath)
212 event = FileAttachedEvent(request, pagename, target, filesize)
199 send_event(event) 213 send_event(event)
200 214
201 return target 215 return target, filesize
202 216
203 217
204 ############################################################################# 218 #############################################################################
205 ### Internal helpers 219 ### Internal helpers
206 ############################################################################# 220 #############################################################################
581 if bsindex >= 0: 595 if bsindex >= 0:
582 target = target[bsindex+1:] 596 target = target[bsindex+1:]
583 597
584 # add the attachment 598 # add the attachment
585 try: 599 try:
586 add_attachment(request, pagename, target, filecontent, overwrite=overwrite) 600 target, bytes = add_attachment(request, pagename, target, filecontent, overwrite=overwrite)
587 601
588 bytes = len(filecontent)
589 msg = _("Attachment '%(target)s' (remote name '%(filename)s')" 602 msg = _("Attachment '%(target)s' (remote name '%(filename)s')"
590 " with %(bytes)d bytes saved.") % { 603 " with %(bytes)d bytes saved.") % {
591 'target': target, 'filename': filename, 'bytes': bytes} 604 'target': target, 'filename': filename, 'bytes': bytes}
592 except AttachmentAlreadyExists: 605 except AttachmentAlreadyExists:
593 msg = _("Attachment '%(target)s' (remote name '%(filename)s') already exists.") % { 606 msg = _("Attachment '%(target)s' (remote name '%(filename)s') already exists.") % {
625 if err.errno != errno.ENOENT: # no such file 638 if err.errno != errno.ENOENT: # no such file
626 raise 639 raise
627 else: 640 else:
628 stream = open(savepath, 'wb') 641 stream = open(savepath, 'wb')
629 try: 642 try:
630 stream.write(filecontent) 643 _write_stream(filecontent, stream)
631 finally: 644 finally:
632 stream.close() 645 stream.close()
633 646
634 # touch attachment directory to invalidate cache if new map is saved 647 # touch attachment directory to invalidate cache if new map is saved
635 if ext == '.map': 648 if ext == '.map':