merge mimetypes guessing patch from trunk twisty
authorMark Hammond <mhammond@skippinet.com.au>
Tue, 10 Mar 2009 17:28:22 +1100
branchtwisty
changeset 68 c03e660107daac3a5c4fbad2680daef697e702c5
parent 67 cc4bcb411ef90f67ec3479256c94858892f5e646
child 69 057aae263c27cf40b957c1652b663e6f2d6f8d51
push id1
push userroot
push dateWed, 08 Apr 2009 01:46:05 +0000
merge mimetypes guessing patch from trunk
server/python/junius/bootstrap.py
--- a/server/python/junius/bootstrap.py
+++ b/server/python/junius/bootstrap.py
@@ -1,13 +1,14 @@
 #!/usr/bin/env python
 
 '''
 Setup the CouchDB server so that it is fully usable and what not.
 '''
+import sys
 from twisted.internet import reactor, defer
 import twisted.web.error
 import os, os.path, mimetypes, base64, pprint
 import model
 
 import logging
 logger = logging.getLogger(__name__)
 
@@ -79,25 +80,36 @@ def install_client_files(whateva):
     def _update_doc(design_doc):
         attachments = design_doc['_attachments'] = {}
         # we cannot go in a zipped egg...
         junius_root_dir = path_part_nuke(model.__file__, 4)
         client_dir = os.path.join(junius_root_dir, 'client')
         logger.info("listing contents of '%s'", client_dir)
         
         for filename in os.listdir(client_dir):
-            logger.debug("filename '%s'", filename)
             path = os.path.join(client_dir, filename)
             if os.path.isfile(path):
                 f = open(path, 'rb')
+                ct = mimetypes.guess_type(filename)[0]
+                if ct is None and sys.platform=="win32":
+                    # A very simplistic check in the windows registry.
+                    import _winreg
+                    try:
+                        k = _winreg.OpenKey(_winreg.HKEY_CLASSES_ROOT,
+                                            os.path.splitext(filename)[1])
+                        ct = _winreg.QueryValueEx(k, "Content Type")[0]
+                    except EnvironmentError:
+                        pass
+                assert ct, "can't guess the content type for '%s'" % filename
                 attachments[filename] = {
-                    'content_type': mimetypes.guess_type(filename)[0],
+                    'content_type': ct,
                     'data': base64.b64encode(f.read())
                 }
                 f.close()
+            logger.debug("filename '%s' (%s)", filename, ct)
         return d.saveDoc('raindrop', design_doc, FILES_DOC)
 
     defrd = d.openDoc('raindrop', FILES_DOC) # XXX - why the db name??
     defrd.addCallbacks(_opened_ok, _open_not_exists)
     defrd.addCallback(_update_doc)
     return defrd