comparison MoinMoin/action/AttachFile.py @ 3572:870cc4c47705

AttachFile: fix saving of drawing files
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Sat, 10 May 2008 17:28:44 +0200
parents 6fe1ea4d9d1a
children 35366465d4fe
comparison
equal deleted inserted replaced
3571:35cd758720e8 3572:870cc4c47705
622 basepath, basename = os.path.split(filename) 622 basepath, basename = os.path.split(filename)
623 basename, ext = os.path.splitext(basename) 623 basename, ext = os.path.splitext(basename)
624 624
625 # get directory, and possibly create it 625 # get directory, and possibly create it
626 attach_dir = getAttachDir(request, pagename, create=1) 626 attach_dir = getAttachDir(request, pagename, create=1)
627 savepath = os.path.join(attach_dir, basename + ext)
628
629 # XXX would be better if it worked without this (at least for the png),
630 # XXX but it does not:
631 filecontent = filecontent.read() # read file completely into memory
627 632
628 if ext == '.draw': 633 if ext == '.draw':
629 _addLogEntry(request, 'ATTDRW', pagename, basename + ext) 634 _addLogEntry(request, 'ATTDRW', pagename, basename + ext)
630 filecontent = filecontent.replace("\r", "") 635 filecontent = filecontent.replace("\r", "")
631 636 elif ext == '.map':
632 savepath = os.path.join(attach_dir, basename + ext) 637 filecontent = filecontent.strip()
633 if ext == '.map' and not filecontent.strip(): 638
634 # delete map file if it is empty 639 if filecontent:
640 # filecontent is either a file or a non-empty string
641 stream = open(savepath, 'wb')
642 try:
643 _write_stream(filecontent, stream)
644 finally:
645 stream.close()
646 else:
647 # filecontent is empty string (e.g. empty map file), delete the target file
635 try: 648 try:
636 os.unlink(savepath) 649 os.unlink(savepath)
637 except OSError, err: 650 except OSError, err:
638 if err.errno != errno.ENOENT: # no such file 651 if err.errno != errno.ENOENT: # no such file
639 raise 652 raise
640 else:
641 stream = open(savepath, 'wb')
642 try:
643 _write_stream(filecontent, stream)
644 finally:
645 stream.close()
646 653
647 # touch attachment directory to invalidate cache if new map is saved 654 # touch attachment directory to invalidate cache if new map is saved
648 if ext == '.map': 655 if ext == '.map':
649 os.utime(getAttachDir(request, pagename), None) 656 os.utime(attach_dir, None)
650 657
651 request.emit_http_headers() 658 request.emit_http_headers()
652 request.write("OK") 659 request.write("OK")
653 660
654 661