diff MoinMoin/action/AttachFile.py @ 1115:a349adcabffe

304 response support for attached files
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Tue, 01 Aug 2006 10:01:44 +0200
parents ecece5db5288
children 76a76def8687
line wrap: on
line diff
--- a/MoinMoin/action/AttachFile.py	Tue Aug 01 00:12:17 2006 +0200
+++ b/MoinMoin/action/AttachFile.py	Tue Aug 01 10:01:44 2006 +0200
@@ -29,7 +29,7 @@
 import os, time, zipfile
 from MoinMoin import config, user, util, wikiutil, packages
 from MoinMoin.Page import Page
-from MoinMoin.util import filesys
+from MoinMoin.util import filesys, timefuncs
 
 action_name = __name__.split('.')[-1]
 
@@ -651,18 +651,22 @@
     if not filename:
         return # error msg already sent in _access_file
 
-    mt = wikiutil.MimeType(filename=filename)
+    timestamp = timefuncs.formathttpdate(int(os.path.getmtime(fpath)))
+    if request.if_modified_since == timestamp:
+        request.emit_http_headers(["Status: 304 Not modified"])
+    else:
+        mt = wikiutil.MimeType(filename=filename)
+        request.emit_http_headers([
+            "Content-Type: %s" % mt.content_type(),
+            "Last-Modified: %s" % timestamp, # TODO maybe add a short Expires: header here?
+            "Content-Length: %d" % os.path.getsize(fpath),
+            # TODO: fix the encoding here, plain 8 bit is not allowed according to the RFCs
+            # There is no solution that is compatible to IE except stripping non-ascii chars
+            "Content-Disposition: attachment; filename=\"%s\"" % filename.encode(config.charset),
+        ])
 
-    request.emit_http_headers([
-        "Content-Type: %s" % mt.content_type(),
-        "Content-Length: %d" % os.path.getsize(fpath),
-        # TODO: fix the encoding here, plain 8 bit is not allowed according to the RFCs
-        # There is no solution that is compatible to IE except stripping non-ascii chars
-        "Content-Disposition: attachment; filename=\"%s\"" % filename.encode(config.charset),
-    ])
-
-    # send data
-    shutil.copyfileobj(open(fpath, 'rb'), request, 8192)
+        # send data
+        shutil.copyfileobj(open(fpath, 'rb'), request, 8192)
 
 def install_package(pagename, request):
     _ = request.getText