cmis: add endpoint to get file metadata (#67612)
This commit is contained in:
parent
99d45cd364
commit
3e22994c1d
|
@ -186,6 +186,33 @@ class CmisConnector(BaseResource):
|
|||
bytes_io = doc.getContentStream()
|
||||
return HttpResponse(bytes_io, content_type=mime_type)
|
||||
|
||||
@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}
|
||||
|
||||
|
||||
def wrap_cmis_error(f):
|
||||
@functools.wraps(f)
|
||||
|
|
|
@ -77,6 +77,9 @@
|
|||
<cmis:propertyDateTime propertyDefinitionId="cmis:creationDate">
|
||||
<cmis:value>2020-02-08T19:17:10Z</cmis:value>
|
||||
</cmis:propertyDateTime>
|
||||
<cmis:propertyString propertyDefinitionId="rsj:idInsertis">
|
||||
<cmis:value>21N284563</cmis:value>
|
||||
</cmis:propertyString>
|
||||
</cmis:properties>
|
||||
</cmisra:object>
|
||||
<atom:link rel="service" href="http://example.com/cmisatom/test?repositoryId=test" type="application/atomsvc+xml"/>
|
||||
|
|
|
@ -641,3 +641,42 @@ def test_get_file(mocked_request, app, setup):
|
|||
response = app.get(url, params={'object_id': '/test/file'})
|
||||
assert response.content_type == 'application/octet-stream'
|
||||
assert response.content == b'hello world'
|
||||
|
||||
|
||||
@mock.patch('httplib2.Http.request')
|
||||
def test_get_metadata(mocked_request, app, setup):
|
||||
url = reverse(
|
||||
'generic-endpoint', kwargs={'connector': 'cmis', 'endpoint': 'getmetadata', 'slug': setup.slug}
|
||||
)
|
||||
|
||||
def cmis_mocked_request(uri, method="GET", body=None, **kwargs):
|
||||
"""simulate the HTTP queries involved"""
|
||||
response = {'status': '200'}
|
||||
if method == 'GET' and uri == 'http://example.com/cmisatom':
|
||||
with open('%s/tests/data/cmis/cmis1.out.xml' % os.getcwd(), 'rb') as fd:
|
||||
content = fd.read()
|
||||
elif method == 'GET' and (
|
||||
uri.startswith('http://example.com/cmisatom/test/path?path=/test/file')
|
||||
or uri.startswith(
|
||||
'http://example.com/cmisatom/test/id?id=c4bc9d00-5bf0-404d-8f0a-a6260f6d21ae;1.0'
|
||||
)
|
||||
):
|
||||
with open('%s/tests/data/cmis/cmis3.out.xml' % os.getcwd(), 'rb') as fd:
|
||||
content = fd.read()
|
||||
elif (
|
||||
method == 'GET'
|
||||
and uri == 'http://example.com/cmisatom/test/content/test2?id=L3Rlc3QtZW8vdGVzdDI%3D'
|
||||
):
|
||||
content = b'hello world'
|
||||
else:
|
||||
raise Exception('url is not yet mocked: %s' % uri)
|
||||
return (response, content)
|
||||
|
||||
mocked_request.side_effect = cmis_mocked_request
|
||||
response = app.get(url, params={'object_id': 'c4bc9d00-5bf0-404d-8f0a-a6260f6d21ae;1.0'})
|
||||
assert response.json['data']['cmis']['contentStreamFileName'] == 'test2'
|
||||
assert response.json['data']['rsj']['idInsertis'] == '21N284563'
|
||||
|
||||
response = app.get(url, params={'object_id': '/test/file'})
|
||||
assert response.json['data']['cmis']['contentStreamFileName'] == 'test2'
|
||||
assert response.json['data']['rsj']['idInsertis'] == '21N284563'
|
||||
|
|
Loading…
Reference in New Issue