Skip to content
This repository has been archived by the owner on Feb 13, 2020. It is now read-only.

Commit

Permalink
Fix directory listing to display a proper MIME Type string that is me…
Browse files Browse the repository at this point in the history
…aningful.

git-svn-id: https://svn.calendarserver.org/repository/calendarserver/CalendarServer/trunk@5447 e27351fd-9f3e-4f54-a53b-843176b1656c
  • Loading branch information
cyrusdaboo committed Apr 8, 2010
1 parent e96d05d commit 2a1c086
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 28 deletions.
42 changes: 23 additions & 19 deletions twistedcaldav/extensions.py
Expand Up @@ -519,7 +519,7 @@ def resourceType(self, request):
if self.deadProperties().contains((dav_namespace, "resourcetype")):
return succeed(self.deadProperties().get((dav_namespace, "resourcetype")))
if self.isCollection():
return succeed(davxml.ResourceType(davxml.Collection(), davxml.Principal()))
return succeed(davxml.ResourceType(davxml.Principal(), davxml.Collection()))
else:
return succeed(davxml.ResourceType(davxml.Principal()))

Expand Down Expand Up @@ -640,7 +640,7 @@ def gotBody(body, output=output):
d.addCallback(gotBody)
return d

@printTracebacks
@inlineCallbacks
def renderDirectoryBody(self, request):
"""
Generate a directory listing table in HTML.
Expand All @@ -656,7 +656,7 @@ def renderDirectoryBody(self, request):
for name in sorted(self.listChildren()):
child = self.getChild(name)

url, name, size, lastModified, contentType = self.getChildDirectoryEntry(child, name)
url, name, size, lastModified, contentType = (yield self.getChildDirectoryEntry(child, name, request))

# FIXME: gray out resources that are not readable
output.append(
Expand Down Expand Up @@ -761,11 +761,12 @@ def gotValues(items):
d.addCallback(gotValues)
return d

d = self.listProperties(request)
d.addCallback(gotProperties)
return d
qnames = (yield self.listProperties(request))
result = (yield gotProperties(qnames))
returnValue(result)

def getChildDirectoryEntry(self, child, name):
@inlineCallbacks
def getChildDirectoryEntry(self, child, name, request):
def orNone(value, default="?", f=None):
if value is None:
return default
Expand All @@ -782,22 +783,25 @@ def orNone(value, default="?", f=None):
if isinstance(child, MetaDataMixin):
size = child.contentLength()
lastModified = child.lastModified()
contentType = child.contentType()
rtypes = []
fullrtype = (yield child.resourceType(request))
for rtype in fullrtype.children:
rtypes.append(rtype.name)
if rtypes:
rtypes = "(%s)" % (", ".join(rtypes),)
if child.isCollection():
contentType = rtypes
else:
mimeType = child.contentType()
contentType = "%s/%s" % (mimeType.mediaType, mimeType.mediaSubtype)
if rtypes:
contentType += " %s" % (rtypes,)
else:
size = None
lastModified = None
contentType = None

if self.fp.isdir():
contentType = "(collection)"
else:
contentType = self._orNone(
contentType,
default="-",
f=lambda m: "%s/%s %s" % (m.mediaType, m.mediaSubtype, m.params)
)

return (
returnValue((
url,
name,
orNone(size),
Expand All @@ -807,7 +811,7 @@ def orNone(value, default="?", f=None):
f=lambda t: time.strftime("%Y-%b-%d %H:%M", time.localtime(t))
),
contentType,
)
))



Expand Down
9 changes: 9 additions & 0 deletions twistedcaldav/static.py
Expand Up @@ -775,6 +775,15 @@ def provisionFile(self):

return True

def _initTypeAndEncoding(self):

# Handle cases not covered by getTypeAndEncoding()
if self.isCollection():
self._type = "httpd/unix-directory"
else:
super(AutoProvisioningFileMixIn, self)._initTypeAndEncoding()


class CalendarHomeProvisioningFile (AutoProvisioningFileMixIn, DirectoryCalendarHomeProvisioningResource, DAVFile):
"""
Resource which provisions calendar home collections as needed.
Expand Down
20 changes: 11 additions & 9 deletions twistedcaldav/test/test_extensions.py
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
##
# Copyright (c) 2009 Apple Inc. All rights reserved.
# Copyright (c) 2009-2010 Apple Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -15,19 +15,19 @@
# limitations under the License.
##

from xml.etree.cElementTree import XML# , tostring

from twisted.trial.unittest import TestCase

from twisted.internet.defer import inlineCallbacks
from twext.python.filepath import CachingFilePath as FilePath
from twext.web2.dav import davxml
from twext.web2.dav.element.base import WebDAVElement
from twext.web2.http_headers import MimeType
from twext.web2.static import MetaDataMixin

from twisted.internet.defer import inlineCallbacks, succeed
from twisted.trial.unittest import TestCase
from twisted.web.microdom import parseString
from twext.web2.static import MetaDataMixin

from twistedcaldav.extensions import DAVFile

from twext.web2.dav.element.base import WebDAVElement
from xml.etree.cElementTree import XML

class UnicodeProperty(WebDAVElement):
"""
Expand Down Expand Up @@ -163,7 +163,9 @@ def test_nonASCIIListMixedChildren(self):
unicodeChildName = "test"
def addUnicodeChild(davFile):
m = MetaDataMixin()
m.contentType = lambda: u'text/plain'
m.contentType = lambda: MimeType.fromString('text/plain')
m.resourceType = lambda r: succeed(davxml.ResourceType())
m.isCollection = lambda: False
davFile.putChild(unicodeChildName, m)
yield self.doDirectoryTest([nonASCIIFilename], addUnicodeChild,
[nonASCIIFilename.encode("utf-8"), unicodeChildName])
Expand Down

0 comments on commit 2a1c086

Please sign in to comment.