comparison MoinMoin/action/AttachFile.py @ 801:1f8976e01c3a

fix wrong import, make more use of MimeType class
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Fri, 19 May 2006 20:18:45 +0200
parents 28de47f4ff1a
children 7b2e550c9660
comparison
equal deleted inserted replaced
800:3d50c51878b7 801:1f8976e01c3a
631 631
632 def get_file(pagename, request): 632 def get_file(pagename, request):
633 import shutil 633 import shutil
634 634
635 filename, fpath = _access_file(pagename, request) 635 filename, fpath = _access_file(pagename, request)
636 if not filename: return # error msg already sent in _access_file 636 if not filename:
637 637 return # error msg already sent in _access_file
638 # get mimetype 638
639 type, enc = wikiutil.guess_type(filename) 639 mt = wikiutil.MimeType(filename=filename)
640 if not type:
641 type = "application/octet-stream"
642 640
643 # send header 641 # send header
644 request.http_headers([ 642 request.http_headers([
645 "Content-Type: %s" % type, 643 "Content-Type: %s" % mt.content_type(),
646 "Content-Length: %d" % os.path.getsize(fpath), 644 "Content-Length: %d" % os.path.getsize(fpath),
647 # TODO: fix the encoding here, plain 8 bit is not allowed according to the RFCs 645 # TODO: fix the encoding here, plain 8 bit is not allowed according to the RFCs
648 # There is no solution that is compatible to IE except stripping non-ascii chars 646 # There is no solution that is compatible to IE except stripping non-ascii chars
649 "Content-Disposition: attachment; filename=\"%s\"" % filename.encode(config.charset), 647 "Content-Disposition: attachment; filename=\"%s\"" % filename.encode(config.charset),
650 ]) 648 ])
764 filename, fpath = _access_file(pagename, request) 762 filename, fpath = _access_file(pagename, request)
765 if not filename: return 763 if not filename: return
766 764
767 request.write('<h2>' + _("Attachment '%(filename)s'") % {'filename': filename} + '</h2>') 765 request.write('<h2>' + _("Attachment '%(filename)s'") % {'filename': filename} + '</h2>')
768 766
769 type, enc = wikiutil.guess_type(filename) 767 mt = wikiutil.MimeType(filename=filename)
770 if type: 768 if mt.major == 'image':
771 if type[:5] == 'image': 769 timestamp = htdocs_access(request) and "?%s" % time.time() or ''
772 timestamp = htdocs_access(request) and "?%s" % time.time() or '' 770 request.write('<img src="%s%s" alt="%s">' % (
773 request.write('<img src="%s%s" alt="%s">' % ( 771 getAttachUrl(pagename, filename, request, escaped=1), timestamp, wikiutil.escape(filename, 1)))
774 getAttachUrl(pagename, filename, request, escaped=1), timestamp, wikiutil.escape(filename, 1))) 772 return
775 return 773 elif mt.major == 'text':
776 elif type[:4] == 'text': 774 # TODO: should use formatter here!
777 # TODO: should use formatter here! 775 request.write("<pre>")
778 request.write("<pre>") 776 # Try to decode file contents. It may return junk, but we
779 # Try to decode file contents. It may return junk, but we 777 # don't have enough information on attachments.
780 # don't have enough information on attachments. 778 content = open(fpath, 'r').read()
781 content = open(fpath, 'r').read() 779 content = wikiutil.decodeUnknownInput(content)
782 content = wikiutil.decodeUnknownInput(content) 780 content = wikiutil.escape(content)
783 content = wikiutil.escape(content) 781 request.write(content)
784 request.write(content) 782 request.write("</pre>")
785 request.write("</pre>") 783 return
786 return
787 784
788 package = packages.ZipPackage(request, fpath) 785 package = packages.ZipPackage(request, fpath)
789 if package.isPackage(): 786 if package.isPackage():
790 request.write("<pre><b>%s</b>\n%s</pre>" % (_("Package script:"),wikiutil.escape(package.getScript()))) 787 request.write("<pre><b>%s</b>\n%s</pre>" % (_("Package script:"),wikiutil.escape(package.getScript())))
791 return 788 return