Skip to content
This repository has been archived by the owner on Feb 21, 2019. It is now read-only.

Commit

Permalink
replace pyramid file request code by new hapic file output feature
Browse files Browse the repository at this point in the history
  • Loading branch information
inkhey authored and buxx committed Sep 17, 2018
1 parent 1537117 commit f006937
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 59 deletions.
2 changes: 1 addition & 1 deletion backend/setup.py
Expand Up @@ -24,7 +24,7 @@
'zope.sqlalchemy',
'alembic',
# API
'hapic>=0.41',
'hapic>=0.52',
'marshmallow <3.0.0a1,>2.0.0',
# CLI
'cliff',
Expand Down
12 changes: 0 additions & 12 deletions backend/tracim_backend/lib/utils/request.py
Expand Up @@ -65,12 +65,8 @@ def __init__(
# user
self._candidate_user = None # type: User

# TODO BS 2018-09-12: Must be deleted, see:
# https://github.com/tracim/tracim/issues/903
self.response_content_disposition = None
# INFO - G.M - 18-05-2018 - Close db at the end of the request
self.add_finished_callback(self._cleanup)
self.add_response_callback(self._set_responses_headers)

@property
def current_workspace(self) -> Workspace:
Expand Down Expand Up @@ -183,10 +179,6 @@ def candidate_workspace(self) -> Workspace:
)
return self._candidate_workspace

def response_download_mode(self, force_download: bool = False) -> None:
if force_download:
self.response_content_disposition = 'attachment'

def _cleanup(self, request: 'TracimRequest') -> None:
"""
Close dbsession at the end of the request in order to avoid exception
Expand All @@ -200,10 +192,6 @@ def _cleanup(self, request: 'TracimRequest') -> None:
self._current_workspace = None
self.dbsession.close()

def _set_responses_headers(self, request: 'TracimRequest', response: Response): # nopep8
if request.response_content_disposition:
response.content_disposition = request.response_content_disposition

@candidate_user.setter
def candidate_user(self, user: User) -> None:
if self._candidate_user is not None:
Expand Down
16 changes: 8 additions & 8 deletions backend/tracim_backend/tests/functional/test_contents.py
Expand Up @@ -1960,7 +1960,7 @@ def test_api__get_file_raw__ok_200__force_download_case(self) -> None:
status=200,
params=params
)
assert res.headers['Content-Disposition'] == 'attachment'
assert res.headers['Content-Disposition'] == 'attachment; filename="Test_file.txt"' # nopep8
assert res.body == b'Test file'
assert res.content_type == 'text/plain'
assert res.content_length == len(b'Test file')
Expand Down Expand Up @@ -2190,7 +2190,7 @@ def test_api__get_jpeg_preview__ok__200__force_download_case(self) -> None:
status=200,
params=params
)
assert res.headers['Content-Disposition'] == 'attachment'
assert res.headers['Content-Disposition'] == 'attachment; filename="Test file_page_1.jpg"' # nopep8
assert res.body != image.getvalue()
assert res.content_type == 'image/jpeg'

Expand Down Expand Up @@ -2304,7 +2304,7 @@ def test_api__get_sized_jpeg_preview__ok__200__force_download_case(self) -> None
params=params,
)
assert res.body != image.getvalue()
assert res.headers['Content-Disposition'] == 'attachment'
assert res.headers['Content-Disposition'] == 'attachment; filename="Test file_page_1_256x256.jpg"' # nopep8
assert res.content_type == 'image/jpeg'
new_image = Image.open(io.BytesIO(res.body))
assert 256, 256 == new_image.size
Expand Down Expand Up @@ -2505,7 +2505,7 @@ def test_api__get_sized_jpeg_revision_preview__ok__200__force_download_case(self
status=200,
params=params,
)
assert res.headers['Content-Disposition'] == 'attachment'
assert res.headers['Content-Disposition'] == 'attachment; filename="Test file_page_1_256x256.jpg"' # nopep8
assert res.body != image.getvalue()
assert res.content_type == 'image/jpeg'
new_image = Image.open(io.BytesIO(res.body))
Expand Down Expand Up @@ -2639,7 +2639,7 @@ def test_api__get_full_pdf_preview__ok__200__force_download_case(self) -> None:
status=200,
params=params
)
assert res.headers['Content-Disposition'] == 'attachment'
assert res.headers['Content-Disposition'] == 'attachment; filename="Test_file.pdf"' # nopep8
assert res.content_type == 'application/pdf'

def test_api__get_full_pdf_preview__err__400__png_UnavailablePreviewType(self) -> None: # nopep8
Expand Down Expand Up @@ -2822,7 +2822,7 @@ def test_api__get_pdf_preview__ok__200__force_download_case(self) -> None:
params=params,
)
assert res.content_type == 'application/pdf'
assert res.headers['Content-Disposition'] == 'attachment'
assert res.headers['Content-Disposition'] == 'attachment; filename="Test_file_page_1.pdf"' # nopep8

def test_api__get_pdf_preview__ok__err__400_page_of_preview_not_found(self) -> None: # nopep8
"""
Expand Down Expand Up @@ -3100,7 +3100,7 @@ def test_api__get_full_pdf_revision_preview__ok__200__force_download_case(self)
status=200,
params=params,
)
assert res.headers['Content-Disposition'] == 'attachment'
assert res.headers['Content-Disposition'] == 'attachment; filename="Test file.pdf"' # nopep8
assert res.content_type == 'application/pdf'

def test_api__get_pdf_revision_preview__ok__200__force_download_case(self) -> None:
Expand Down Expand Up @@ -3173,7 +3173,7 @@ def test_api__get_pdf_revision_preview__ok__200__force_download_case(self) -> No
status=200,
params=params,
)
assert res.headers['Content-Disposition'] == 'attachment'
assert res.headers['Content-Disposition'] == 'attachment; filename="Test file_page_1.pdf"' # nopep8
assert res.content_type == 'application/pdf'


Expand Down
109 changes: 71 additions & 38 deletions backend/tracim_backend/views/contents_api/file_controller.py
Expand Up @@ -3,6 +3,7 @@

import transaction
from depot.manager import DepotManager
from hapic.data import HapicFile
from preview_generator.exception import UnavailablePreviewType
from pyramid.config import Configurator
from pyramid.response import FileIter
Expand Down Expand Up @@ -115,14 +116,12 @@ def download_file(self, context, request: TracimRequest, hapic_data=None):
content_type=CONTENT_TYPES.Any_SLUG
)
file = DepotManager.get().get(content.depot_file)
request.response_download_mode(force_download=hapic_data.query.force_download) # nopep8
response = request.response
response.content_type = file.content_type
response.app_iter = FileIter(file)
# INFO - G.M - 2018-09-05 - Support for force download param
if hapic_data.query.force_download:
response.content_disposition = 'attachment'
return response
return HapicFile(
file_object=file,
mimetype=file.content_type,
filename=content.file_name,
as_attachment=hapic_data.query.force_download
)

@hapic.with_api_doc(tags=[SWAGGER_TAG__FILE_ENDPOINTS])
@require_workspace_role(UserRoleInWorkspace.READER)
Expand Down Expand Up @@ -151,14 +150,12 @@ def download_revisions_file(self, context, request: TracimRequest, hapic_data=No
content=content
)
file = DepotManager.get().get(revision.depot_file)
request.response_download_mode(force_download=hapic_data.query.force_download) # nopep8
response = request.response
response.content_type = file.content_type
response.app_iter = FileIter(file)
# INFO - G.M - 2018-09-05 - Support for force download param
if hapic_data.query.force_download:
response.content_disposition = 'attachment'
return response
return HapicFile(
file_object=file,
mimetype=file.content_type,
filename=revision.file_name,
as_attachment=hapic_data.query.force_download
)

# preview
# pdf
Expand Down Expand Up @@ -191,9 +188,12 @@ def preview_pdf(self, context, request: TracimRequest, hapic_data=None):
content.revision_id,
page_number=hapic_data.query.page
)
request.response_download_mode(force_download=hapic_data.query.force_download) # nopep8
response = FileResponse(pdf_preview_path)
return response
filename = "{}_page_{}.pdf".format(content.label, hapic_data.query.page)
return HapicFile(
file_path=pdf_preview_path,
filename=filename,
as_attachment=hapic_data.query.force_download
)

@hapic.with_api_doc(tags=[SWAGGER_TAG__FILE_ENDPOINTS])
@require_workspace_role(UserRoleInWorkspace.READER)
Expand All @@ -219,9 +219,12 @@ def preview_pdf_full(self, context, request: TracimRequest, hapic_data=None): #
content_type=CONTENT_TYPES.Any_SLUG
)
pdf_preview_path = api.get_full_pdf_preview_path(content.revision_id)
request.response_download_mode(force_download=hapic_data.query.force_download) # nopep8
response = FileResponse(pdf_preview_path)
return response
filename = "{label}.pdf".format(label=content.label)
return HapicFile(
file_path=pdf_preview_path,
filename=filename,
as_attachment=hapic_data.query.force_download
)

@hapic.with_api_doc(tags=[SWAGGER_TAG__FILE_ENDPOINTS])
@require_workspace_role(UserRoleInWorkspace.READER)
Expand Down Expand Up @@ -253,9 +256,12 @@ def preview_pdf_full_revision(self, context, request: TracimRequest, hapic_data=
pdf_preview_path = api.get_full_pdf_preview_path(
revision.revision_id,
)
request.response_download_mode(force_download=hapic_data.query.force_download) # nopep8
response = FileResponse(pdf_preview_path)
return response
filename = "{label}.pdf".format(label=revision.label)
return HapicFile(
file_path=pdf_preview_path,
filename=filename,
as_attachment=hapic_data.query.force_download
)

@hapic.with_api_doc(tags=[SWAGGER_TAG__FILE_ENDPOINTS])
@require_workspace_role(UserRoleInWorkspace.READER)
Expand Down Expand Up @@ -289,9 +295,15 @@ def preview_pdf_revision(self, context, request: TracimRequest, hapic_data=None)
revision.revision_id,
page_number=hapic_data.query.page
)
request.response_download_mode(force_download=hapic_data.query.force_download) # nopep8
response = FileResponse(pdf_preview_path)
return response
filename = "{label}_page_{page_number}.pdf".format(
label=content.label,
page_number=hapic_data.query.page
)
return HapicFile(
file_path=pdf_preview_path,
filename=filename,
as_attachment=hapic_data.query.force_download
)

# jpg
@hapic.with_api_doc(tags=[SWAGGER_TAG__FILE_ENDPOINTS])
Expand Down Expand Up @@ -325,9 +337,15 @@ def preview_jpg(self, context, request: TracimRequest, hapic_data=None):
width=allowed_dim.dimensions[0].width,
height=allowed_dim.dimensions[0].height,
)
request.response_download_mode(force_download=hapic_data.query.force_download) # nopep8
response = FileResponse(jpg_preview_path)
return response
filename = "{label}_page_{page_number}.jpg".format(
label=content.label,
page_number=hapic_data.query.page
)
return HapicFile(
file_path=jpg_preview_path,
filename=filename,
as_attachment=hapic_data.query.force_download
)

@hapic.with_api_doc(tags=[SWAGGER_TAG__FILE_ENDPOINTS])
@require_workspace_role(UserRoleInWorkspace.READER)
Expand Down Expand Up @@ -360,9 +378,17 @@ def sized_preview_jpg(self, context, request: TracimRequest, hapic_data=None):
height=hapic_data.path.height,
width=hapic_data.path.width,
)
request.response_download_mode(force_download=hapic_data.query.force_download) # nopep8
response = FileResponse(jpg_preview_path)
return response
filename = "{label}_page_{page_number}_{width}x{height}.jpg".format(
label=content.label,
page_number=hapic_data.query.page,
width=hapic_data.path.width,
height=hapic_data.path.height
)
return HapicFile(
file_path=jpg_preview_path,
filename=filename,
as_attachment=hapic_data.query.force_download
)

@hapic.with_api_doc(tags=[SWAGGER_TAG__FILE_ENDPOINTS])
@require_workspace_role(UserRoleInWorkspace.READER)
Expand Down Expand Up @@ -399,10 +425,17 @@ def sized_preview_jpg_revision(self, context, request: TracimRequest, hapic_data
height=hapic_data.path.height,
width=hapic_data.path.width,
)

request.response_download_mode(force_download=hapic_data.query.force_download) # nopep8
response = FileResponse(jpg_preview_path)
return response
filename = "{label}_page_{page_number}_{width}x{height}.jpg".format(
label=revision.label,
page_number=hapic_data.query.page,
width=hapic_data.path.width,
height=hapic_data.path.height
)
return HapicFile(
file_path=jpg_preview_path,
filename=filename,
as_attachment=hapic_data.query.force_download
)

@hapic.with_api_doc(tags=[SWAGGER_TAG__FILE_ENDPOINTS])
@require_workspace_role(UserRoleInWorkspace.READER)
Expand Down

0 comments on commit f006937

Please sign in to comment.