diff --git a/trytond/webdav/webdav.py b/trytond/webdav/webdav.py index a63e0397b8..c637d02076 100644 --- a/trytond/webdav/webdav.py +++ b/trytond/webdav/webdav.py @@ -2,6 +2,7 @@ import os import base64 from trytond.osv import fields, OSV +from trytond.version import PACKAGE, VERSION, WEBSITE class Directory(OSV): @@ -22,6 +23,9 @@ class Directory(OSV): ('name_parent_uniq', 'UNIQUE (name, parent)', 'The directory name must be unique inside a directory!'), ] + ext2mime = { + '.png': 'image/png', + } def _uri2object(self, cursor, user, uri, object_name=_name, object_id=False, context=None): @@ -117,16 +121,27 @@ class Directory(OSV): return str(len(attachment.datas)) return '0' - def get_data(self, cursor, user, uri, context=None): - from DAV.errors import DAV_NotFound + def get_contenttype(self, cursor, user, uri, context=None): object_name, object_id = self._uri2object(cursor, user, uri, context=context) if object_name == 'ir.attachment': - attachment_obj = self.pool.get('ir.attachment') - attachment = attachment_obj.browse(cursor, user, object_id, + ext = os.path.splitext(uri)[1] + if not ext: + return "application/octet-stream" + return self.ext2mime.get(ext, 'application/octet-stream') + return "application/octet-stream" + + def get_data(self, cursor, user, uri, context=None): + from DAV.errors import DAV_NotFound + if uri: + object_name, object_id = self._uri2object(cursor, user, uri, context=context) - return base64.decodestring(attachment.datas) - raise DAV_NotFound + if object_name == 'ir.attachment': + attachment_obj = self.pool.get('ir.attachment') + attachment = attachment_obj.browse(cursor, user, object_id, + context=context) + return base64.decodestring(attachment.datas) + return DAV_NotFound def exists(self, cursor, user, uri, context=None): object_name, object_id = self._uri2object(cursor, user, uri, diff --git a/trytond/webdavsvc.py b/trytond/webdavsvc.py index aa9263999f..c86d7eb555 100644 --- a/trytond/webdavsvc.py +++ b/trytond/webdavsvc.py @@ -4,9 +4,11 @@ import base64 from DAV import AuthServer, WebDAVServer, iface from DAV.errors import * from DAV.constants import COLLECTION, OBJECT +from DAV.utils import get_uriparentpath, get_urifilename from netsvc import LocalService import security import pooler +from version import PACKAGE, VERSION, WEBSITE # This work because there is only one thread USER_ID = 0 @@ -33,14 +35,38 @@ class TrytonDAVInterface(iface.dav_interface): pool = pooler.get_pool(dbname) cursor = pooler.get_db(dbname).cursor() directory_obj = pool.get('webdav.directory') + if uri[:-1] != '/': + uri += '/' for child in directory_obj.get_childs(cursor, USER_ID, dburi): res.append(urlparse.urljoin(self.baseuri, uri + child)) return res def get_data(self, uri): dbname, dburi = self._get_dburi(uri) - if not dbname or not dburi: - raise DAV_NotFound + if not dbname or self.is_collection(uri): + res = '' + res += '' + res += '' + res += '' + res += '%s - WebDAV - %s' \ + % (PACKAGE, dbname or 'root') + res += '' + res += '' + res += '

Directory: %s

' % (get_urifilename(uri) or '/') + res += '' + res += '
' + res += 'Powered by %s version %s' \ + % (WEBSITE, PACKAGE, VERSION) + res += '' + res += '' + return res pool = pooler.get_pool(dbname) cursor = pooler.get_db(dbname).cursor() directory_obj = pool.get('webdav.directory') @@ -68,7 +94,13 @@ class TrytonDAVInterface(iface.dav_interface): return directory_obj.get_contentlength(cursor, USER_ID, dburi) def _get_dav_getcontenttype(self, uri): - return "application/octet-stream" + dbname, dburi = self._get_dburi(uri) + if not dbname or self.is_collection(uri): + return "text/html" + pool = pooler.get_pool(dbname) + cursor = pooler.get_db(dbname).cursor() + directory_obj = pool.get('webdav.directory') + return directory_obj.get_contenttype(cursor, USER_ID, dburi) def exists(self, uri): dbname, dburi = self._get_dburi(uri)