Allow to user browser with WebDAV

This commit is contained in:
Cédric Krier committed 2008-02-14 15:19:08 +01:00
1 parent a437768c9a
commit 336479ecb2
2 files changed
+56 -9

No files matched your search

+21 -6
View File
@@ -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,
+35 -3
View File
@@ -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 = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">'
res += '<html>'
res += '<head>'
res += '<meta http-equiv="Content-Type" content="text/html; charset=utf-8">'
res += '<title>%s - WebDAV - %s</title>' \
% (PACKAGE, dbname or 'root')
res += '</head>'
res += '<body>'
res += '<h2>Directory: %s</h2>' % (get_urifilename(uri) or '/')
res += '<ul>'
if dbname:
res += '<li><a href="%s">..</a></li>' \
% (get_uriparentpath(uri) or '/')
for child in self.get_childs(uri):
res += '<li><a href="%s">%s</a></li>' \
% (child, get_urifilename(child))
res += '</ul>'
res += '<hr noshade>'
res += '<em>Powered by <a href="%s">%s</a> version %s</em>' \
% (WEBSITE, PACKAGE, VERSION)
res += '</body>'
res += '</html>'
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)