2019-05-27 20:17:46 +02:00
|
|
|
# passerelle - uniform access to multiple data sources and services
|
2016-08-19 10:36:28 +02:00
|
|
|
# Copyright (C) 2016 Entr'ouvert
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify it
|
|
|
|
# under the terms of the GNU Affero General Public License as published
|
|
|
|
# by the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Affero General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
import base64
|
2020-01-15 17:20:26 +01:00
|
|
|
import binascii
|
2017-12-22 18:10:22 +01:00
|
|
|
import functools
|
|
|
|
import re
|
2021-09-11 12:42:29 +02:00
|
|
|
from contextlib import contextmanager
|
2022-04-20 12:18:04 +02:00
|
|
|
from io import BytesIO
|
2022-03-17 17:37:33 +01:00
|
|
|
from urllib import error as urllib2
|
2016-08-19 10:36:28 +02:00
|
|
|
|
2019-10-07 22:11:42 +02:00
|
|
|
import httplib2
|
2017-12-22 18:10:22 +01:00
|
|
|
from cmislib import CmisClient
|
2019-10-07 22:11:42 +02:00
|
|
|
from cmislib.exceptions import (
|
|
|
|
CmisException,
|
|
|
|
InvalidArgumentException,
|
|
|
|
ObjectNotFoundException,
|
|
|
|
PermissionDeniedException,
|
|
|
|
UpdateConflictException,
|
2021-05-07 11:53:34 +02:00
|
|
|
)
|
2016-08-19 10:36:28 +02:00
|
|
|
from django.db import models
|
2022-11-03 08:12:25 +01:00
|
|
|
from django.http import HttpResponse
|
2021-07-07 17:34:26 +02:00
|
|
|
from django.utils.functional import cached_property
|
2023-01-24 11:48:27 +01:00
|
|
|
from django.utils.http import urlencode
|
2022-08-31 10:16:04 +02:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
2016-08-19 10:36:28 +02:00
|
|
|
|
|
|
|
from passerelle.base.models import BaseResource
|
|
|
|
from passerelle.utils.api import endpoint
|
|
|
|
from passerelle.utils.jsonresponse import APIError
|
2021-09-11 12:42:29 +02:00
|
|
|
from passerelle.utils.logging import ignore_loggers
|
2016-08-19 10:36:28 +02:00
|
|
|
|
2017-12-22 18:10:22 +01:00
|
|
|
SPECIAL_CHARS = '!#$%&+-^_`~;[]{}+=~'
|
2020-02-03 15:56:26 +01:00
|
|
|
FILE_PATH_PATTERN = r'^(/|(/[\w%s]+)+)$' % re.escape(SPECIAL_CHARS)
|
|
|
|
FILE_NAME_PATTERN = r'[\w%s\.]+$' % re.escape(SPECIAL_CHARS)
|
|
|
|
|
|
|
|
|
|
|
|
UPLOAD_SCHEMA = {
|
|
|
|
'type': 'object',
|
2021-07-08 12:28:02 +02:00
|
|
|
'title': _('CMIS file upload'),
|
2020-02-03 15:56:26 +01:00
|
|
|
'properties': {
|
|
|
|
'file': {
|
2021-07-08 12:28:02 +02:00
|
|
|
'title': _('File object'),
|
2020-02-03 15:56:26 +01:00
|
|
|
'type': 'object',
|
|
|
|
'properties': {
|
|
|
|
'filename': {
|
|
|
|
'type': 'string',
|
2021-09-23 11:59:59 +02:00
|
|
|
'description': _('Filename'),
|
2020-02-03 15:56:26 +01:00
|
|
|
'pattern': FILE_NAME_PATTERN,
|
2021-09-23 11:59:59 +02:00
|
|
|
'pattern_description': _('Numbers, letters and special caracters "%s" are allowed.')
|
|
|
|
% SPECIAL_CHARS,
|
2020-02-03 15:56:26 +01:00
|
|
|
},
|
2021-07-08 12:28:02 +02:00
|
|
|
'content': {
|
|
|
|
'type': 'string',
|
|
|
|
'description': _('Content'),
|
|
|
|
},
|
|
|
|
'content_type': {
|
|
|
|
'type': 'string',
|
|
|
|
'description': _('Content type'),
|
|
|
|
},
|
2020-02-03 15:56:26 +01:00
|
|
|
},
|
|
|
|
'required': ['content'],
|
|
|
|
},
|
|
|
|
'filename': {
|
|
|
|
'type': 'string',
|
2021-07-08 12:28:02 +02:00
|
|
|
'description': _('Filename (takes precendence over filename in "file" object)'),
|
2020-02-03 15:56:26 +01:00
|
|
|
'pattern': FILE_NAME_PATTERN,
|
2021-09-23 11:59:59 +02:00
|
|
|
'pattern_description': _('Numbers, letters and special caracters "%s" are allowed.')
|
|
|
|
% SPECIAL_CHARS,
|
2020-02-03 15:56:26 +01:00
|
|
|
},
|
|
|
|
'path': {
|
|
|
|
'type': 'string',
|
2021-09-23 11:59:59 +02:00
|
|
|
'description': _('File path'),
|
2020-02-03 15:56:26 +01:00
|
|
|
'pattern': FILE_PATH_PATTERN,
|
2021-09-23 11:59:59 +02:00
|
|
|
'pattern_description': _('Must include leading but not trailing slash.'),
|
2020-02-03 15:56:26 +01:00
|
|
|
},
|
2021-07-08 12:28:02 +02:00
|
|
|
'object_type': {
|
|
|
|
'type': 'string',
|
|
|
|
'description': _('CMIS object type'),
|
|
|
|
},
|
|
|
|
'properties': {
|
|
|
|
'type': 'object',
|
2021-08-16 09:22:37 +02:00
|
|
|
'title': _('CMIS properties (dictionary with string keys)'),
|
2021-07-08 12:28:02 +02:00
|
|
|
'additionalProperties': {'type': 'string'},
|
|
|
|
},
|
2020-02-03 15:56:26 +01:00
|
|
|
},
|
2020-02-05 16:23:04 +01:00
|
|
|
'required': ['file', 'path'],
|
|
|
|
'unflatten': True,
|
2020-02-03 15:56:26 +01:00
|
|
|
}
|
2016-08-19 10:36:28 +02:00
|
|
|
|
|
|
|
|
|
|
|
class CmisConnector(BaseResource):
|
2017-12-22 18:10:22 +01:00
|
|
|
cmis_endpoint = models.URLField(
|
|
|
|
max_length=400, verbose_name=_('CMIS Atom endpoint'), help_text=_('URL of the CMIS Atom endpoint')
|
|
|
|
)
|
2016-08-19 10:36:28 +02:00
|
|
|
username = models.CharField(max_length=128, verbose_name=_('Service username'))
|
2017-12-22 18:10:22 +01:00
|
|
|
password = models.CharField(max_length=128, verbose_name=_('Service password'))
|
2023-01-18 15:04:06 +01:00
|
|
|
category = _('File Storage')
|
2016-08-19 10:36:28 +02:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
verbose_name = _('CMIS connector')
|
|
|
|
|
2021-07-07 17:34:26 +02:00
|
|
|
def check_status(self):
|
2021-09-11 12:42:29 +02:00
|
|
|
with self.get_cmis_gateway() as cmis_gateway:
|
2022-03-18 14:35:33 +01:00
|
|
|
cmis_gateway.repo # pylint: disable=pointless-statement
|
2021-07-07 17:34:26 +02:00
|
|
|
|
2020-02-03 15:56:26 +01:00
|
|
|
@endpoint(
|
2021-07-08 11:41:51 +02:00
|
|
|
description=_('File upload'),
|
2020-02-03 15:56:26 +01:00
|
|
|
perm='can_access',
|
|
|
|
post={
|
|
|
|
'request_body': {
|
|
|
|
'schema': {
|
|
|
|
'application/json': UPLOAD_SCHEMA,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
|
|
|
def uploadfile(self, request, post_data):
|
|
|
|
error, error_msg, data = self._validate_inputs(post_data)
|
2017-12-22 18:10:22 +01:00
|
|
|
if error:
|
|
|
|
self.logger.debug("received invalid data: %s" % error_msg)
|
|
|
|
raise APIError(error_msg, http_status=400)
|
2020-02-03 14:21:46 +01:00
|
|
|
filename = data.get('filename') or data['file']['filename']
|
|
|
|
self.logger.info("received file_name: '%s', file_path: '%s'", filename, data["path"])
|
2021-09-11 12:42:29 +02:00
|
|
|
with self.get_cmis_gateway() as cmis_gateway:
|
|
|
|
doc = cmis_gateway.create_doc(
|
|
|
|
filename,
|
|
|
|
data['path'],
|
|
|
|
data['file_byte_content'],
|
|
|
|
content_type=data['file'].get('content_type'),
|
|
|
|
object_type=data.get('object_type'),
|
|
|
|
properties=data.get('properties'),
|
|
|
|
)
|
|
|
|
return {'data': {'properties': doc.properties}}
|
2017-12-22 18:10:22 +01:00
|
|
|
|
2021-09-11 12:42:29 +02:00
|
|
|
@contextmanager
|
2021-09-06 16:00:56 +02:00
|
|
|
def get_cmis_gateway(self):
|
2021-10-21 12:12:46 +02:00
|
|
|
with ignore_loggers('cmislib', 'cmislib.atompub.binding'):
|
2023-01-24 11:48:27 +01:00
|
|
|
import cmislib.atompub.binding as atompub_binding
|
|
|
|
|
|
|
|
old_Rest = atompub_binding.Rest
|
|
|
|
atompub_binding.Rest = lambda: RESTService(self)
|
|
|
|
try:
|
|
|
|
yield CMISGateway(self.cmis_endpoint, self.username, self.password, self.logger)
|
|
|
|
finally:
|
|
|
|
atompub_binding.Rest = old_Rest
|
2021-09-06 16:00:56 +02:00
|
|
|
|
2020-02-03 15:56:26 +01:00
|
|
|
def _validate_inputs(self, data):
|
|
|
|
"""process dict
|
2017-12-22 18:10:22 +01:00
|
|
|
return a tuple (error, error_msg, data)
|
|
|
|
"""
|
2018-02-13 10:44:12 +01:00
|
|
|
file_ = data['file']
|
2020-02-03 15:56:26 +01:00
|
|
|
if 'filename' not in file_ and 'filename' not in data:
|
|
|
|
return True, '"filename" or "file[\'filename\']" is required', None
|
|
|
|
|
2016-08-19 10:36:28 +02:00
|
|
|
try:
|
2018-02-13 10:44:12 +01:00
|
|
|
data['file_byte_content'] = base64.b64decode(file_['content'])
|
2020-01-15 17:20:26 +01:00
|
|
|
except (TypeError, binascii.Error):
|
2018-02-13 10:44:12 +01:00
|
|
|
return True, '"file[\'content\']" must be a valid base64 string', None
|
|
|
|
|
2017-12-22 18:10:22 +01:00
|
|
|
return False, '', data
|
2016-08-19 10:36:28 +02:00
|
|
|
|
2022-11-03 08:12:25 +01:00
|
|
|
@endpoint(
|
|
|
|
description=_('Get file'),
|
|
|
|
perm='can_access',
|
|
|
|
parameters={
|
|
|
|
'object_id': {
|
|
|
|
'description': _('Object ID of file (can also be a path)'),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
|
|
|
def getfile(self, request, object_id):
|
|
|
|
with self.get_cmis_gateway() as cmis_gateway:
|
|
|
|
if '/' in object_id:
|
|
|
|
doc = cmis_gateway.get_object_by_path(object_id)
|
|
|
|
else:
|
|
|
|
doc = cmis_gateway.get_object(object_id)
|
|
|
|
try:
|
|
|
|
mime_type = doc.properties['cmis:contentStreamMimeType']
|
|
|
|
except KeyError:
|
|
|
|
mime_type = 'application/octet-stream'
|
|
|
|
bytes_io = doc.getContentStream()
|
|
|
|
return HttpResponse(bytes_io, content_type=mime_type)
|
|
|
|
|
2022-11-03 08:22:23 +01:00
|
|
|
@endpoint(
|
|
|
|
description=_('Get file metadata'),
|
|
|
|
perm='can_access',
|
|
|
|
parameters={
|
|
|
|
'object_id': {
|
|
|
|
'description': _('Object ID of file (can also be a path)'),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
|
|
|
def getmetadata(self, request, object_id):
|
|
|
|
with self.get_cmis_gateway() as cmis_gateway:
|
|
|
|
if '/' in object_id:
|
|
|
|
doc = cmis_gateway.get_object_by_path(object_id)
|
|
|
|
else:
|
|
|
|
doc = cmis_gateway.get_object(object_id)
|
|
|
|
|
|
|
|
metadata = {}
|
|
|
|
for key, value in doc.properties.items():
|
|
|
|
sub_metadata = metadata
|
|
|
|
for subkey in key.split(':')[:-1]:
|
|
|
|
if subkey not in sub_metadata:
|
|
|
|
sub_metadata[subkey] = {}
|
|
|
|
sub_metadata = sub_metadata[subkey]
|
|
|
|
sub_metadata[key.split(':')[-1]] = value
|
|
|
|
|
|
|
|
return {'data': metadata}
|
|
|
|
|
2016-08-19 10:36:28 +02:00
|
|
|
|
2017-12-22 18:10:22 +01:00
|
|
|
def wrap_cmis_error(f):
|
|
|
|
@functools.wraps(f)
|
|
|
|
def wrapper(*args, **kwargs):
|
2016-08-19 10:36:28 +02:00
|
|
|
try:
|
2017-12-22 18:10:22 +01:00
|
|
|
return f(*args, **kwargs)
|
2019-10-07 22:11:42 +02:00
|
|
|
except (urllib2.URLError, httplib2.HttpLib2Error) as e:
|
|
|
|
# FIXME urllib2 still used for cmslib 0.5 compat
|
2017-12-22 18:10:22 +01:00
|
|
|
raise APIError("connection error: %s" % e)
|
|
|
|
except PermissionDeniedException as e:
|
|
|
|
raise APIError("permission denied: %s" % e)
|
|
|
|
except UpdateConflictException as e:
|
|
|
|
raise APIError("update conflict: %s" % e)
|
2020-02-05 16:23:04 +01:00
|
|
|
except InvalidArgumentException as e:
|
|
|
|
raise APIError("invalid property name: %s" % e)
|
2017-12-22 18:10:22 +01:00
|
|
|
except CmisException as e:
|
|
|
|
raise APIError("cmis binding error: %s" % e)
|
2021-02-20 16:26:01 +01:00
|
|
|
|
2017-12-22 18:10:22 +01:00
|
|
|
return wrapper
|
|
|
|
|
|
|
|
|
2022-03-18 14:24:29 +01:00
|
|
|
class CMISGateway:
|
2017-12-22 18:10:22 +01:00
|
|
|
def __init__(self, cmis_endpoint, username, password, logger):
|
2023-01-24 11:48:27 +01:00
|
|
|
self._cmis_client = CmisClient(cmis_endpoint, username, password)
|
2017-12-22 18:10:22 +01:00
|
|
|
self._logger = logger
|
|
|
|
|
2021-07-07 17:34:26 +02:00
|
|
|
@cached_property
|
|
|
|
def repo(self):
|
|
|
|
return self._cmis_client.defaultRepository
|
|
|
|
|
2017-12-22 18:10:22 +01:00
|
|
|
def _get_or_create_folder(self, file_path):
|
2016-08-19 10:36:28 +02:00
|
|
|
try:
|
2017-12-22 18:10:22 +01:00
|
|
|
self._logger.debug("searching '%s'" % file_path)
|
2021-07-07 17:34:26 +02:00
|
|
|
res = self.repo.getObjectByPath(file_path)
|
2017-12-22 18:10:22 +01:00
|
|
|
self._logger.debug("'%s' found" % file_path)
|
|
|
|
return res
|
2016-08-19 10:36:28 +02:00
|
|
|
except ObjectNotFoundException:
|
2017-12-22 18:10:22 +01:00
|
|
|
self._logger.debug("'%s' not found" % file_path)
|
|
|
|
basepath = ""
|
2021-07-07 17:34:26 +02:00
|
|
|
folder = self.repo.rootFolder
|
2017-12-22 18:10:22 +01:00
|
|
|
for path_part in file_path.strip('/').split('/'):
|
|
|
|
basepath += '/%s' % path_part
|
|
|
|
try:
|
|
|
|
self._logger.debug("searching '%s'" % basepath)
|
2021-07-07 17:34:26 +02:00
|
|
|
folder = self.repo.getObjectByPath(basepath)
|
2017-12-22 18:10:22 +01:00
|
|
|
self._logger.debug("'%s' found" % basepath)
|
|
|
|
except ObjectNotFoundException:
|
|
|
|
self._logger.debug("'%s' not found" % basepath)
|
|
|
|
folder = folder.createFolder(path_part)
|
|
|
|
self._logger.debug("create folder '%s'" % basepath)
|
|
|
|
return folder
|
|
|
|
|
|
|
|
@wrap_cmis_error
|
2020-02-05 16:23:04 +01:00
|
|
|
def create_doc(
|
2020-02-06 17:01:55 +01:00
|
|
|
self, file_name, file_path, file_byte_content, content_type=None, object_type=None, properties=None
|
|
|
|
):
|
2017-12-22 18:10:22 +01:00
|
|
|
folder = self._get_or_create_folder(file_path)
|
2020-02-05 16:23:04 +01:00
|
|
|
properties = properties or {}
|
|
|
|
if object_type:
|
|
|
|
properties['cmis:objectTypeId'] = object_type
|
|
|
|
return folder.createDocument(
|
|
|
|
file_name, contentFile=BytesIO(file_byte_content), contentType=content_type, properties=properties
|
2020-02-06 17:01:55 +01:00
|
|
|
)
|
2022-11-03 08:12:25 +01:00
|
|
|
|
|
|
|
@wrap_cmis_error
|
|
|
|
def get_object_by_path(self, file_path):
|
|
|
|
return self.repo.getObjectByPath(file_path)
|
|
|
|
|
|
|
|
@wrap_cmis_error
|
|
|
|
def get_object(self, object_id):
|
|
|
|
return self.repo.getObject(object_id)
|
2023-01-24 11:48:27 +01:00
|
|
|
|
|
|
|
|
|
|
|
# Mock API from cmilib.net.RESTService
|
|
|
|
class RESTService:
|
|
|
|
def __init__(self, resource):
|
|
|
|
self.resource = resource
|
|
|
|
|
|
|
|
def request(self, method, url, username, password, body=None, content_type=None, **kwargs):
|
|
|
|
if username or password:
|
|
|
|
auth = (username, password)
|
|
|
|
else:
|
|
|
|
auth = None
|
|
|
|
|
|
|
|
headers = kwargs.pop('headers', {})
|
|
|
|
|
|
|
|
if kwargs:
|
|
|
|
url = url + ('&' if '?' in url else '?') + urlencode(kwargs)
|
|
|
|
|
|
|
|
if content_type:
|
|
|
|
headers['Content-Type'] = content_type
|
|
|
|
|
|
|
|
response = self.resource.requests.request(
|
|
|
|
method=method, url=url, auth=auth, headers=headers, data=body
|
|
|
|
)
|
|
|
|
|
|
|
|
return {'status': str(response.status_code)}, response.content
|
|
|
|
|
|
|
|
def get(self, url, username=None, password=None, **kwargs):
|
|
|
|
return self.request('GET', url, username, password, **kwargs)
|
|
|
|
|
|
|
|
def delete(self, url, username=None, password=None, **kwargs):
|
|
|
|
return self.request('DELETE', url, username, password, **kwargs)
|
|
|
|
|
|
|
|
def put(self, url, payload, contentType, username=None, password=None, **kwargs):
|
|
|
|
return self.request('PUT', url, username, password, body=payload, content_type=contentType, **kwargs)
|
|
|
|
|
|
|
|
def post(self, url, payload, contentType, username=None, password=None, **kwargs):
|
|
|
|
return self.request('POST', url, username, password, body=payload, content_type=contentType, **kwargs)
|