Commit c2bb9c7d authored by 's avatar

Changed mime-type sniffing to use OFS.content_types

parent b6c854a3
...@@ -83,7 +83,7 @@ ...@@ -83,7 +83,7 @@
# #
############################################################################## ##############################################################################
__doc__="""Copy interface""" __doc__="""Copy interface"""
__version__='$Revision: 1.34 $'[11:-2] __version__='$Revision: 1.35 $'[11:-2]
import sys, string, Globals, Moniker, tempfile, ExtensionClass import sys, string, Globals, Moniker, tempfile, ExtensionClass
from marshal import loads, dumps from marshal import loads, dumps
...@@ -332,7 +332,7 @@ class CopyContainer(ExtensionClass.Base): ...@@ -332,7 +332,7 @@ class CopyContainer(ExtensionClass.Base):
meth=None meth=None
if hasattr(self, method_name): if hasattr(self, method_name):
meth=self._getOb(method_name) meth=getattr(self, method_name)
else: else:
# Handle strange names that come from the Product # Handle strange names that come from the Product
# machinery ;( # machinery ;(
......
...@@ -84,7 +84,7 @@ ...@@ -84,7 +84,7 @@
############################################################################## ##############################################################################
"""DTML Method objects.""" """DTML Method objects."""
__version__='$Revision: 1.21 $'[11:-2] __version__='$Revision: 1.22 $'[11:-2]
from Globals import HTML, HTMLFile, MessageDialog from Globals import HTML, HTMLFile, MessageDialog
from string import join,split,strip,rfind,atoi,lower from string import join,split,strip,rfind,atoi,lower
...@@ -172,7 +172,7 @@ class DTMLMethod(cDocument, HTML, Acquisition.Implicit, RoleManager, ...@@ -172,7 +172,7 @@ class DTMLMethod(cDocument, HTML, Acquisition.Implicit, RoleManager,
return len(self.raw) return len(self.raw)
getSize=get_size getSize=get_size
def oldvalidate(self, inst, parent, name, value, md): def validate(self, inst, parent, name, value, md):
################################################################# #################################################################
# Note that this method is not used normally. It is simply a # Note that this method is not used normally. It is simply a
# Python rendition of the validate method implemented in # Python rendition of the validate method implemented in
......
...@@ -84,9 +84,10 @@ ...@@ -84,9 +84,10 @@
############################################################################## ##############################################################################
"""Image object""" """Image object"""
__version__='$Revision: 1.69 $'[11:-2] __version__='$Revision: 1.70 $'[11:-2]
import Globals, string, struct, mimetypes, content_types import Globals, string, struct, content_types
from OFS.content_types import guess_content_type
from Globals import HTMLFile, MessageDialog from Globals import HTMLFile, MessageDialog
from PropertyManager import PropertyManager from PropertyManager import PropertyManager
from AccessControl.Role import RoleManager from AccessControl.Role import RoleManager
...@@ -146,6 +147,7 @@ class File(Persistent,Implicit,PropertyManager, ...@@ -146,6 +147,7 @@ class File(Persistent,Implicit,PropertyManager,
self.__name__=id self.__name__=id
self.title=title self.title=title
self.precondition=precondition self.precondition=precondition
filename=hasattr(file, 'filename') and file.filename or None
headers=hasattr(file, 'headers') and file.headers or None headers=hasattr(file, 'headers') and file.headers or None
if hasattr(file, 'read'): if hasattr(file, 'read'):
data=file.read() data=file.read()
...@@ -153,12 +155,8 @@ class File(Persistent,Implicit,PropertyManager, ...@@ -153,12 +155,8 @@ class File(Persistent,Implicit,PropertyManager,
if headers and headers.has_key('content-type') and (not content_type): if headers and headers.has_key('content-type') and (not content_type):
content_type=headers['content-type'] content_type=headers['content-type']
if not content_type: if not content_type:
content_type, enc=mimetypes.guess_type(id) filename=filename or id
if not content_type: content_type, enc=guess_content_type(id, data)
if content_types.find_binary(data) >= 0:
content_type='application/octet-stream'
else: content_type=content_types.text_type(data)
content_type=string.lower(content_type)
self.update_data(data, content_type) self.update_data(data, content_type)
def id(self): def id(self):
...@@ -199,7 +197,7 @@ class File(Persistent,Implicit,PropertyManager, ...@@ -199,7 +197,7 @@ class File(Persistent,Implicit,PropertyManager,
self.data=Pdata(data) self.data=Pdata(data)
self.size=len(data) self.size=len(data)
def manage_edit(self,title,content_type,precondition='',REQUEST=None): def manage_edit(self, title, content_type, precondition='', REQUEST=None):
""" """
Changes the title and content type attributes of the File or Image. Changes the title and content type attributes of the File or Image.
""" """
...@@ -218,38 +216,28 @@ class File(Persistent,Implicit,PropertyManager, ...@@ -218,38 +216,28 @@ class File(Persistent,Implicit,PropertyManager,
The file or images contents are replaced with the contents of 'file'. The file or images contents are replaced with the contents of 'file'.
""" """
filename=hasattr(file, 'filename') and file.filename or None
headers=hasattr(file, 'headers') and file.headers or None headers=hasattr(file, 'headers') and file.headers or None
data=(headers is None) and file or file.read() body=(headers is None) and file or file.read()
if headers and headers.has_key('content-type'): if headers and headers.has_key('content-type'):
content_type=headers['content-type'] content_type=headers['content-type']
else: else:
content_type='' filename=filename or self.id()
if not content_type: content_type, enc=guess_content_type(self.id(), body)
content_type, enc=mimetypes.guess_type(self.id()) self.update_data(body, content_type)
if not content_type:
if content_types.find_binary(data) >= 0:
content_type='application/octet-stream'
else: content_type=content_types.text_type(data)
content_type=string.lower(content_type)
self.update_data(data, content_type)
if REQUEST: return MessageDialog( if REQUEST: return MessageDialog(
title ='Success!', title ='Success!',
message='Your changes have been saved', message='Your changes have been saved',
action ='manage_main') action ='manage_main')
def PUT(self, REQUEST, RESPONSE): def PUT(self, REQUEST, RESPONSE):
"""Handle HTTP PUT requests""" """Handle HTTP PUT requests"""
self.dav__init(REQUEST, RESPONSE) self.dav__init(REQUEST, RESPONSE)
type=REQUEST.get_header('content-type', None) type=REQUEST.get_header('content-type', None)
body=REQUEST.get('BODY', '') body=REQUEST.get('BODY', '')
if type is None: if type is None:
type, enc=mimetypes.guess_type(self.id()) type, enc=guess_content_type(self.id(), body)
if type is None:
if content_types.find_binary(body) >= 0:
type='application/octet-stream'
else: type=content_types.text_type(body)
type=string.lower(type)
self.update_data(body, type) self.update_data(body, type)
RESPONSE.setStatus(204) RESPONSE.setStatus(204)
return RESPONSE return RESPONSE
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment