forked from xml-director/xmldirector.plonecore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
connector.py
130 lines (103 loc) · 4.29 KB
/
connector.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# -*- coding: utf-8 -*-
################################################################
# xmldirector.plonecore
# (C) 2014, Andreas Jung, www.zopyx.com, Tuebingen, Germany
################################################################
import fs
import fs.errors
import urllib
import zExceptions
from zope import schema
from zope.interface import implements
from zope.component import getUtility
from plone.dexterity.content import Item
from plone.supermodel import model
from plone.registry.interfaces import IRegistry
from xmldirector.plonecore.i18n import MessageFactory as _
from xmldirector.plonecore.interfaces import IWebdavSettings
from xmldirector.plonecore.fswrapper import get_fs_wrapper
from xmldirector.plonecore.logger import LOG
class IConnector(model.Schema):
webdav_url = schema.TextLine(
title=_(u'(optional) connection URL of storage'),
description=_(u'WebDAV: http://host:port/path/to/webdav, Local filesystem: file://path/to/directory, AWS S3: s3://bucketname, SFTP sftp://host/path, FTP: ftp://host/path'),
required=False
)
webdav_username = schema.TextLine(
title=_(u'(optional) username overriding the system settings'),
required=False
)
webdav_password = schema.Password(
title=_(u'(optional) password overriding the system settings'),
required=False
)
webdav_subpath = schema.TextLine(
title=_(u'Subdirectory relative to the global connection URL'),
description=_(u'Use this value for configuring a more specific subpath'),
required=False
)
api_enabled = schema.Bool(
title=_(u'Public web API enabled'),
default=False,
required=False
)
default_view_anonymous = schema.TextLine(
title=_(u'Default view (anonymous)'),
description=_(
u'Name of a default view for site visitors without edit permission'),
required=False,
default=None,
)
default_view_authenticated = schema.TextLine(
title=_(u'Default view (authenticated)'),
description=_(u'Name of a default view for anonymous site visitors'),
required=False,
default=u'@@view',
)
class Connector(Item):
implements(IConnector)
webdav_url = None
webdav_username = None
webdav_password = None
webdav_subpath = None
def webdav_handle(self, subpath=None):
""" Return WebDAV handle to root of configured connector object
including configured webdav_subpath.
"""
registry = getUtility(IRegistry)
settings = registry.forInterface(IWebdavSettings)
adapted = IConnector(self)
url = adapted.webdav_url or settings.webdav_url
if adapted.webdav_subpath:
url += '/{}'.format(adapted.webdav_subpath)
if subpath:
url += '/{}'.format(urllib.quote(subpath))
# system-wide credentials
username = settings.webdav_username
password = settings.webdav_password or ''
# local credentials override the system credentials
if adapted.webdav_url:
username = adapted.webdav_username or ''
password = adapted.webdav_password or ''
try:
return get_fs_wrapper(url, credentials=dict(username=username, password=password))
except fs.errors.ResourceNotFoundError:
LOG.error(u'Error accessing {}::{}::{}'.format(
self.absolute_url(), url, self.REQUEST.get('HTTP_USER_AGENT')), exc_info=True)
raise zExceptions.NotFound(url)
except fs.errors.ResourceInvalidError:
parts = url.rsplit('/', 1)
wrapper = get_fs_wrapper(parts[0], credentials=dict(username=username, password=password))
wrapper.__leaf__ = True
wrapper.__leaf_filename__ = parts[1]
return wrapper
except fs.errors.RemoteConnectionError as e:
# LOG.error(u'Error accessing {}::{}::{}'.format(self.absolute_url(), url, self.REQUEST.get('HTTP_USER_AGENT')), exc_info=True)
exc = RuntimeError(url)
exc.url = url
raise exc
except Exception as e:
LOG.error(u'Error accessing {}::{}::{}'.format(
self.absolute_url(), url, self.REQUEST.get('HTTP_USER_AGENT')), exc_info=True)
e.url = url
raise e