[CalendarServer-changes] [5903] CalendarServer/branches/new-store

source_changes at macosforge.org source_changes at macosforge.org
Wed Jul 14 12:09:22 PDT 2010


Revision: 5903
          http://trac.macosforge.org/projects/calendarserver/changeset/5903
Author:   cdaboo at apple.com
Date:     2010-07-14 12:09:21 -0700 (Wed, 14 Jul 2010)
Log Message:
-----------
Make sure attachment content-type is preserved and returned.

Modified Paths:
--------------
    CalendarServer/branches/new-store/twistedcaldav/storebridge.py
    CalendarServer/branches/new-store/txcaldav/calendarstore/file.py

Modified: CalendarServer/branches/new-store/twistedcaldav/storebridge.py
===================================================================
--- CalendarServer/branches/new-store/twistedcaldav/storebridge.py	2010-07-14 19:04:07 UTC (rev 5902)
+++ CalendarServer/branches/new-store/twistedcaldav/storebridge.py	2010-07-14 19:09:21 UTC (rev 5903)
@@ -522,12 +522,16 @@
 
     @requiresPermissions(fromParent=[davxml.Bind()])
     def http_PUT(self, request):
-        # FIXME: MIME-Type from header
         # FIXME: direct test
         # FIXME: transformation?
+
+        content_type = request.headers.getHeader("content-type")
+        if content_type is None:
+            content_type = MimeType("application", "octet-stream")
+
         t = self.calendarObject.createAttachmentWithName(
             self.attachmentName,
-            "application/octet-stream"
+            content_type,
         )
         def done(ignored):
             t.loseConnection()
@@ -552,8 +556,7 @@
 
     def contentType(self):
         # FIXME: test
-        return MimeType.fromString(
-            self._newStoreAttachment.contentType())
+        return self._newStoreAttachment.contentType()
 
 
     def getChild(self, name):
@@ -563,10 +566,14 @@
     @requiresPermissions(davxml.WriteContent())
     def http_PUT(self, request):
         # FIXME: direct test
-        # FIXME: MIME-Type from header
         # FIXME: refactor with ProtoCalendarAttachment.http_PUT
         # FIXME: CDT test to make sure that permissions are enforced.
-        t = self._newStoreAttachment.store("application/octet-stream")
+
+        content_type = request.headers.getHeader("content-type")
+        if content_type is None:
+            content_type = MimeType("application", "octet-stream")
+
+        t = self._newStoreAttachment.store(content_type)
         def done(ignored):
             t.loseConnection()
             return NO_CONTENT
@@ -582,9 +589,8 @@
             def connectionLost(self, reason):
                 stream.finish()
         self._newStoreAttachment.retrieve(StreamProtocol())
-        return Response(OK, None, stream)
+        return Response(OK, {"content-type":self.contentType()}, stream)
 
-
     @requiresPermissions(fromParent=[davxml.Unbind()])
     def http_DELETE(self, request):
         self._newStoreCalendarObject.removeAttachmentWithName(

Modified: CalendarServer/branches/new-store/txcaldav/calendarstore/file.py
===================================================================
--- CalendarServer/branches/new-store/txcaldav/calendarstore/file.py	2010-07-14 19:04:07 UTC (rev 5902)
+++ CalendarServer/branches/new-store/txcaldav/calendarstore/file.py	2010-07-14 19:09:21 UTC (rev 5903)
@@ -37,8 +37,9 @@
 
 from twext.python.vcomponent import InvalidICalendarDataError
 from twext.python.vcomponent import VComponent
-# from twext.web2.dav.resource import TwistedGETContentMD5
 from twext.web2.dav.element.rfc2518 import ResourceType, GETContentType
+from twext.web2.dav.resource import TwistedGETContentMD5
+from twext.web2.http_headers import generateContentType
 
 
 from twistedcaldav import caldavxml, customxml
@@ -411,18 +412,21 @@
         )
 
 
+contentTypeKey = PropertyName.fromElement(GETContentType)
+md5key = PropertyName.fromElement(TwistedGETContentMD5)
 
 class AttachmentStorageTransport(object):
 
     implements(ITransport)
 
-    def __init__(self, attachment):
+    def __init__(self, attachment, contentType):
         """
         
         @param attachment:
         @type attachment:
         """
         self._attachment = attachment
+        self._contentType = contentType
         self._file = self._attachment._computePath().open("w")
 
 
@@ -434,13 +438,14 @@
     def loseConnection(self):
         # FIXME: do anything
         self._file.close()
-        # TwistedGETContentMD5.fromString(md5)
 
+        md5 = hashlib.md5(self._attachment._computePath().getContent()).hexdigest()
+        props = self._attachment._properties()
+        props[contentTypeKey] = GETContentType(generateContentType(self._contentType))
+        props[md5key] = TwistedGETContentMD5.fromString(md5)
+        props.flush()
 
 
-contentTypeKey = PropertyName.fromString(GETContentType.sname())
-# md5key = PropertyName.fromString(TwistedGETContentMD5.sname())
-
 class Attachment(object):
     """
     An L{Attachment} is a container for the data associated with a I{locally-
@@ -478,17 +483,12 @@
 
 
     def contentType(self):
-        return self._properties()[contentTypeKey].children[0].data
+        return self._properties()[contentTypeKey].mimeType()
 
 
     def store(self, contentType):
-        ast = AttachmentStorageTransport(self)
-        props = self._properties()
-        props[contentTypeKey] = GETContentType(contentType)
-        props.flush()
-        return ast
+        return AttachmentStorageTransport(self, contentType)
 
-
     def retrieve(self, protocol):
         # FIXME: makeConnection
         # FIXME: actually stream
@@ -499,7 +499,7 @@
 
 
     def md5(self):
-        return hashlib.md5(self._computePath().getContent()).hexdigest()
+        return self._properties()[md5key]
 
 
     def _computePath(self):
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20100714/9e453f54/attachment-0001.html>


More information about the calendarserver-changes mailing list