cmis: add endpoint to get file (#67602)
gitea-wip/passerelle/pipeline/head Build started... Details
gitea/passerelle/pipeline/head Something is wrong with the build of this commit Details

This commit is contained in:
Frédéric Péters 2022-11-03 08:12:25 +01:00
parent e5275f8a1d
commit 99d45cd364
2 changed files with 68 additions and 0 deletions

View File

@ -32,6 +32,7 @@ from cmislib.exceptions import (
UpdateConflictException,
)
from django.db import models
from django.http import HttpResponse
from django.utils.functional import cached_property
from django.utils.translation import gettext_lazy as _
@ -163,6 +164,28 @@ class CmisConnector(BaseResource):
return False, '', data
@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)
def wrap_cmis_error(f):
@functools.wraps(f)
@ -226,3 +249,11 @@ class CMISGateway:
return folder.createDocument(
file_name, contentFile=BytesIO(file_byte_content), contentType=content_type, properties=properties
)
@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)

View File

@ -604,3 +604,40 @@ def test_cmis_check_status(app, setup, monkeypatch):
with pytest.raises(CmisException):
setup.check_status()
@mock.patch('httplib2.Http.request')
def test_get_file(mocked_request, app, setup):
url = reverse('generic-endpoint', kwargs={'connector': 'cmis', 'endpoint': 'getfile', '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.content_type == 'application/octet-stream'
assert response.content == b'hello world'
response = app.get(url, params={'object_id': '/test/file'})
assert response.content_type == 'application/octet-stream'
assert response.content == b'hello world'