Exemplo n.º 1
0
 def download_file(self, context, request: TracimRequest, hapic_data=None):
     """
     Download raw file of last revision of content.
     Good pratice for filename is filename is `{label}{file_extension}` or `{filename}`.
     Default filename value is 'raw' (without file extension) or nothing.
     """
     app_config = request.registry.settings['CFG']
     api = ContentApi(
         show_archived=True,
         show_deleted=True,
         current_user=request.current_user,
         session=request.dbsession,
         config=app_config,
     )
     content = api.get_one(
         hapic_data.path.content_id,
         content_type=content_type_list.Any_SLUG
     )
     file = DepotManager.get().get(content.depot_file)
     filename = hapic_data.path.filename
     if not filename or filename == 'raw':
         filename = content.file_name
     return HapicFile(
         file_object=file,
         mimetype=file.content_type,
         filename=filename,
         as_attachment=hapic_data.query.force_download
     )
Exemplo n.º 2
0
 def download_file(self, context, request: TracimRequest, hapic_data=None):
     """
     Download raw file of last revision of content.
     Good pratice for filename is filename is `{label}{file_extension}` or `{filename}`.
     Default filename value is 'raw' (without file extension) or nothing.
     """
     app_config = request.registry.settings["CFG"]  # type: CFG
     api = ContentApi(
         show_archived=True,
         show_deleted=True,
         current_user=request.current_user,
         session=request.dbsession,
         config=app_config,
     )
     content = api.get_one(hapic_data.path.content_id, content_type=content_type_list.Any_SLUG)
     try:
         file = DepotManager.get().get(content.depot_file)
     except IOError as exc:
         raise TracimFileNotFound(
             "file related to revision {} of content {} not found in depot.".format(
                 content.revision_id, content.content_id
             )
         ) from exc
     filename = hapic_data.path.filename
     if not filename or filename == "raw":
         filename = content.file_name
     return HapicFile(
         file_object=file,
         mimetype=file.content_type,
         filename=filename,
         as_attachment=hapic_data.query.force_download,
         content_length=file.content_length,
         last_modified=content.updated,
     )
Exemplo n.º 3
0
 def preview_pdf_full_revision(self,
                               context,
                               request: TracimRequest,
                               hapic_data=None):
     """
     Obtain full pdf preview of a specific revision of content.
     Good pratice for filename is filename is `{label}_r{revision_id}.pdf`.
     Default filename value is 'raw' (without file extension) or nothing.
     """
     app_config = request.registry.settings["CFG"]  # type: CFG
     api = ContentApi(
         show_archived=True,
         show_deleted=True,
         current_user=request.current_user,
         session=request.dbsession,
         config=app_config,
     )
     content = api.get_one(hapic_data.path.content_id,
                           content_type=content_type_list.Any_SLUG)
     revision = api.get_one_revision(
         revision_id=hapic_data.path.revision_id, content=content)
     pdf_preview_path = api.get_full_pdf_preview_path(
         revision.revision_id, file_extension=revision.file_extension)
     filename = hapic_data.path.filename
     # INFO - G.M - 2019-08-08 - use given filename in all case but none or
     # "raw", where filename returned will be a custom one.
     if not filename or filename == "raw":
         filename = "{label}_r{revision_id}.pdf".format(
             revision_id=revision.revision_id, label=revision.label)
     return HapicFile(
         file_path=pdf_preview_path,
         filename=filename,
         as_attachment=hapic_data.query.force_download,
     )
Exemplo n.º 4
0
 def preview_pdf_full(self, context, request: TracimRequest, hapic_data=None):  # nopep8
     """
     Obtain a full pdf preview (all page) of last revision of content.
     Good pratice for filename is filename is `{label}.pdf`.
     Default filename value is 'raw' (without file extension) or nothing.
     """
     app_config = request.registry.settings['CFG']
     api = ContentApi(
         show_archived=True,
         show_deleted=True,
         current_user=request.current_user,
         session=request.dbsession,
         config=app_config,
     )
     content = api.get_one(
         hapic_data.path.content_id,
         content_type=content_type_list.Any_SLUG
     )
     pdf_preview_path = api.get_full_pdf_preview_path(
         content.revision_id,
         file_extension=content.file_extension,
     )
     filename = hapic_data.path.filename
     if not filename or filename == 'raw':
         filename = "{label}.pdf".format(label=content.label)
     return HapicFile(
         file_path=pdf_preview_path,
         filename=filename,
         as_attachment=hapic_data.query.force_download
     )
Exemplo n.º 5
0
 def get_html_document_preview(
     self, context, request: TracimRequest, hapic_data=None
 ) -> HapicFile:
     """
        Download preview of html document
        Good pratice for filename is filename is `{label}{file_extension}` or `{filename}`.
        Default filename value is 'raw' (without file extension) or nothing.
        """
     app_config = request.registry.settings["CFG"]  # type: CFG
     api = ContentApi(
         show_archived=True,
         show_deleted=True,
         current_user=request.current_user,
         session=request.dbsession,
         config=app_config,
     )
     content = api.get_one(hapic_data.path.content_id, content_type=content_type_list.Any_SLUG)
     file = BytesIO()
     byte_size = file.write(content.description.encode("utf-8"))
     file.seek(0)
     filename = hapic_data.path.filename
     # INFO - G.M - 2019-08-08 - use given filename in all case but none or
     # "raw", where filename returned will be original file one.
     if not filename or filename == "raw":
         filename = content.file_name
     return HapicFile(
         file_object=file,
         mimetype=CONTENT_TYPE_TEXT_HTML,
         filename=filename,
         as_attachment=hapic_data.query.force_download,
         content_length=byte_size,
         last_modified=content.updated,
     )
Exemplo n.º 6
0
 def get_content(self,
                 context: DefaultRootFactory,
                 request: TracimRequest,
                 hapic_data: HapicData = None) -> HapicFile:
     """
     WOPI GetFile endpoint :
     https://wopi.readthedocs.io/projects/wopirest/en/latest/files/GetFile.html#getfile
     """
     try:
         file_ = DepotManager.get(
             request.app_config.UPLOADED_FILES__STORAGE__STORAGE_NAME).get(
                 request.current_content.depot_file)
     except IOError as exc:
         raise TracimFileNotFound(
             "file related to revision {} of content {} not found in depot."
             .format(request.current_content.cached_revision_id,
                     request.current_content.content_id)) from exc
     return HapicFile(
         file_object=file_,
         mimetype=file_.content_type,
         filename=request.current_content.file_name,
         as_attachment=True,
         content_length=file_.content_length,
         last_modified=request.current_content.updated,
     )
Exemplo n.º 7
0
    def get_file_response(self, file_response: HapicFile, http_code: int):
        if file_response.file_path:
            from pyramid.response import FileResponse

            # TODO - G.M - 2019-03-27 - add support for overriding parameters of
            # file_response like content_length
            # Extended support for file response:
            # https://github.com/algoo/hapic/issues/171
            response = FileResponse(
                path=file_response.file_path,
                # INFO - G.M - 2018-09-13 - If content_type is no, mimetype
                # is automatically guessed
                content_type=file_response.mimetype or None,
            )
        else:
            from pyramid.response import FileIter
            from pyramid.response import Response

            response = Response(status=http_code)
            response.content_type = file_response.mimetype
            response.app_iter = FileIter(file_response.file_object)

        if file_response.content_length:
            response.content_length = file_response.content_length
        if file_response.last_modified:
            response.last_modified = file_response.last_modified

        response.status_code = http_code
        response.content_disposition = file_response.get_content_disposition_header_value(
        )
        return response
Exemplo n.º 8
0
 def hellofile(self, context, request):
     return HapicFile(
         file_path=
         "/home/guenael/Images/Mount_Ararat_and_the_Araratian_plain_(cropped).jpg",
         mimetype="image/jpeg",
         as_attachment=False,
     )
Exemplo n.º 9
0
 def preview_jpg(self, context, request: TracimRequest, hapic_data=None):
     """
     Obtain normally sized jpg preview of last revision of content.
     Good pratice for filename is `filename is {label}_page_{page_number}.jpg`.
     Default filename value is 'raw' (without file extension) or nothing.
     """
     app_config = request.registry.settings["CFG"]  # type: CFG
     api = ContentApi(
         show_archived=True,
         show_deleted=True,
         current_user=request.current_user,
         session=request.dbsession,
         config=app_config,
     )
     content = api.get_one(hapic_data.path.content_id, content_type=content_type_list.Any_SLUG)
     allowed_dim = api.get_jpg_preview_allowed_dim()
     jpg_preview_path = api.get_jpg_preview_path(
         content_id=content.content_id,
         revision_id=content.revision_id,
         page_number=hapic_data.query.page,
         file_extension=content.file_extension,
         width=allowed_dim.dimensions[0].width,
         height=allowed_dim.dimensions[0].height,
     )
     filename = hapic_data.path.filename
     if not filename or filename == "raw":
         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,
     )
Exemplo n.º 10
0
 def preview_pdf_revision(self, context, request: TracimRequest, hapic_data=None):
     """
     Obtain a specific page pdf preview of a specific revision of content.
     Good pratice for filename is filename is `{label}_page_{page_number}.pdf`.
     Default filename value is 'raw' (without file extension) or nothing.
     """
     app_config = request.registry.settings["CFG"]  # type: CFG
     api = ContentApi(
         show_archived=True,
         show_deleted=True,
         current_user=request.current_user,
         session=request.dbsession,
         config=app_config,
     )
     content = api.get_one(hapic_data.path.content_id, content_type=content_type_list.Any_SLUG)
     revision = api.get_one_revision(revision_id=hapic_data.path.revision_id, content=content)
     pdf_preview_path = api.get_pdf_preview_path(
         revision.content_id,
         revision.revision_id,
         page_number=hapic_data.query.page,
         file_extension=revision.file_extension,
     )
     filename = hapic_data.path.filename
     if not filename or filename == "raw":
         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,
     )
Exemplo n.º 11
0
 def hellofile2(self, context, request):
     file = BytesIO()
     image = Image.new("RGBA", size=(1000, 1000), color=(0, 0, 0))
     image.save(file, "png")
     file.name = "test_image.png"
     file.seek(0)
     return HapicFile(filename="coucou.png",
                      file_object=file,
                      mimetype="image/png")
Exemplo n.º 12
0
 def test_unit_file_output_processor_err__no_path_no_object(self):
     processor = MarshmallowProcessor()
     file = BytesIO()
     image = Image.new("RGBA", size=(1000, 1000), color=(0, 0, 0))
     image.save(file, "png")
     file.name = "test_image.png"
     file.seek(0)
     tested_data = HapicFile(mimetype="image/png")
     with pytest.raises(ValidationException):
         processor.dump_output_file(tested_data)
Exemplo n.º 13
0
    def guest_download_file(self,
                            context,
                            request: TracimRequest,
                            hapic_data=None) -> HapicFile:
        app_config = request.registry.settings["CFG"]  # type: CFG
        api = ShareLib(current_user=None,
                       session=request.dbsession,
                       config=app_config)
        content_share = api.get_content_share_by_token(
            share_token=hapic_data.path.share_token)  # type: ContentShare

        # TODO - G.M - 2019-08-01 - verify in access to content share can be granted
        # we should considered do these check at decorator level
        if hapic_data.forms:
            password = hapic_data.forms.password
        else:
            password = None
        api.check_password(content_share, password=password)
        content = ContentApi(current_user=None,
                             session=request.dbsession,
                             config=app_config).get_one(
                                 content_share.content_id,
                                 content_type=content_type_list.Any_SLUG)
        workspace_api = WorkspaceApi(current_user=None,
                                     session=request.dbsession,
                                     config=app_config)
        workspace = workspace_api.get_one(content.workspace_id)
        workspace_api.check_public_download_enabled(workspace)
        if content.type not in shareables_content_type:
            raise ContentTypeNotAllowed()

        try:
            file = DepotManager.get(
                app_config.UPLOADED_FILES__STORAGE__STORAGE_NAME).get(
                    content.depot_file)
        except IOError as exc:
            raise TracimFileNotFound(
                "file related to revision {} of content {} not found in depot."
                .format(content.cached_revision_id,
                        content.content_id)) from exc
        filename = hapic_data.path.filename

        # INFO - G.M - 2019-08-08 - use given filename in all case but none or
        # "raw", when filename returned will be original file one.
        if not filename or filename == "raw":
            filename = content.file_name
        return HapicFile(
            file_object=file,
            mimetype=file.content_type,
            filename=filename,
            as_attachment=True,
            content_length=file.content_length,
            last_modified=content.updated,
        )
Exemplo n.º 14
0
 def test_unit_file_output_processor_ok__process_success_fileobject_as_stream(
         self):
     processor = MarshmallowProcessor()
     file = BytesIO()
     image = Image.new("RGBA", size=(1000, 1000), color=(0, 0, 0))
     image.save(file, "png")
     file.name = "test_image.png"
     file.seek(0)
     tested_data = HapicFile(file_object=file, mimetype="image/png")
     data = processor.dump_output_file(tested_data)
     assert data == tested_data
Exemplo n.º 15
0
 def test_unit_file_output_processor_ok__process_success_fileobject_as_sized_file(
         self):
     processor = MarshmallowProcessor()
     file = BytesIO()
     image = Image.new("RGBA", size=(1000, 1000), color=(0, 0, 0))
     image.save(file, "png")
     file.name = "test_image.png"
     file.seek(0)
     tested_data = HapicFile(
         file_object=file,
         mimetype="image/png",
         content_length=file.getbuffer().nbytes,
         last_modified=datetime.utcnow(),
     )
     data = processor.dump_output_file(tested_data)
     assert data == tested_data
Exemplo n.º 16
0
    def download_revisions_file(self,
                                context,
                                request: TracimRequest,
                                hapic_data=None):
        """
        Download raw file for specific revision of content.
        Good pratice for filename is filename is `{label}_r{revision_id}{file_extension}`.
        Default filename value is 'raw' (without file extension) or nothing.
        """
        app_config = request.registry.settings["CFG"]  # type: CFG
        api = ContentApi(
            show_archived=True,
            show_deleted=True,
            current_user=request.current_user,
            session=request.dbsession,
            config=app_config,
        )
        content = api.get_one(hapic_data.path.content_id,
                              content_type=content_type_list.Any_SLUG)
        revision = api.get_one_revision(
            revision_id=hapic_data.path.revision_id, content=content)
        try:
            file = DepotManager.get(
                app_config.UPLOADED_FILES__STORAGE__STORAGE_NAME).get(
                    revision.depot_file)
        except IOError as exc:
            raise TracimFileNotFound(
                "file related to revision {} of content {} not found in depot."
                .format(revision.revision_id, revision.content_id)) from exc

        filename = hapic_data.path.filename
        # INFO - G.M - 2019-08-08 - use given filename in all case but none or
        # "raw", where filename returned will be a custom one.
        if not filename or filename == "raw":
            filename = "{label}_r{revision_id}{file_extension}".format(
                label=revision.file_name,
                revision_id=revision.revision_id,
                file_extension=revision.file_extension,
            )
        return HapicFile(
            file_object=file,
            mimetype=file.content_type,
            filename=filename,
            as_attachment=hapic_data.query.force_download,
            content_length=file.content_length,
            last_modified=revision.updated,
        )
Exemplo n.º 17
0
 def sized_preview_jpg_revision(self,
                                context,
                                request: TracimRequest,
                                hapic_data=None):
     """
     Obtain resized jpg preview of a specific revision of content.
     Good pratice for filename is filename is `{label}_r{revision_id}_page_{page_number}_{width}x{height}.jpg`.
     Default filename value is 'raw' (without file extension) or nothing.
     """
     app_config = request.registry.settings["CFG"]  # type: CFG
     api = ContentApi(
         show_archived=True,
         show_deleted=True,
         current_user=request.current_user,
         session=request.dbsession,
         config=app_config,
     )
     content = api.get_one(hapic_data.path.content_id,
                           content_type=content_type_list.Any_SLUG)
     revision = api.get_one_revision(
         revision_id=hapic_data.path.revision_id, content=content)
     jpg_preview_path = api.get_jpg_preview_path(
         content_id=content.content_id,
         revision_id=revision.revision_id,
         page_number=hapic_data.query.page,
         height=hapic_data.path.height,
         width=hapic_data.path.width,
         file_extension=revision.file_extension,
     )
     filename = hapic_data.path.filename
     # INFO - G.M - 2019-08-08 - use given filename in all case but none or
     # "raw", where filename returned will a custom one.
     if not filename or filename == "raw":
         filename = "{label}_r{revision_id}_page_{page_number}_{width}x{height}.jpg".format(
             revision_id=revision.revision_id,
             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,
     )
Exemplo n.º 18
0
 def sized_preview_jpg(self, context, request: TracimRequest, hapic_data=None):  # nopep8
     """
     Obtain resized jpg preview of last revision of content.
     Good pratice for filename is filename is `{label}_page_{page_number}_{width}x{height}.jpg`.
     Default filename value is 'raw' (without file extension) or nothing.
     """
     app_config = request.registry.settings['CFG']
     api = ContentApi(
         show_archived=True,
         show_deleted=True,
         current_user=request.current_user,
         session=request.dbsession,
         config=app_config,
     )
     content = api.get_one(
         hapic_data.path.content_id,
         content_type=content_type_list.Any_SLUG
     )
     jpg_preview_path = api.get_jpg_preview_path(
         content_id=content.content_id,
         revision_id=content.revision_id,
         file_extension=content.file_extension,
         page_number=hapic_data.query.page,
         height=hapic_data.path.height,
         width=hapic_data.path.width,
     )
     filename = hapic_data.path.filename
     if not filename or filename == 'raw':
         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
     )
Exemplo n.º 19
0
    def download_revisions_file(self,
                                context,
                                request: TracimRequest,
                                hapic_data=None):  # nopep8
        """
        Download raw file for specific revision of content.
        Good pratice for filename is filename is `{label}_r{revision_id}{file_extension}`.
        Default filename value is 'raw' (without file extension) or nothing.
        """
        app_config = request.registry.settings['CFG']
        api = ContentApi(
            show_archived=True,
            show_deleted=True,
            current_user=request.current_user,
            session=request.dbsession,
            config=app_config,
        )
        content = api.get_one(hapic_data.path.content_id,
                              content_type=content_type_list.Any_SLUG)
        revision = api.get_one_revision(
            revision_id=hapic_data.path.revision_id, content=content)
        try:
            file = DepotManager.get().get(revision.depot_file)
        except IOError as exc:
            raise TracimFileNotFound(
                'file related to revision {} of content {} not found in depot.'
                .format(revision.revision_id, revision.content_id)) from exc

        filename = hapic_data.path.filename
        if not filename or filename == 'raw':
            filename = "{label}_r{revision_id}{file_extension}".format(
                label=revision.file_name,
                revision_id=revision.revision_id,
                file_extension=revision.file_extension)
        return HapicFile(file_object=file,
                         mimetype=file.content_type,
                         filename=filename,
                         as_attachment=hapic_data.query.force_download)
Exemplo n.º 20
0
 def get_user_avatar(self, context, request, hapic_data: HapicData):
     return HapicFile(file_path=UserLib().get_user_avatar_path(
         user_id=(int(hapic_data.path.id))))
Exemplo n.º 21
0
 def get_user_avatar(self, id, hapic_data: HapicData):
     return HapicFile(file_path=UserLib().get_user_avatar_path(
         user_id=(int(hapic_data.path["id"]))))
Exemplo n.º 22
0
    def test_unit_file_output_processor_ok__process_success_filepath(self):
        processor = MarshmallowProcessor()

        tested_data = HapicFile(file_path=os.path.abspath(__file__))
        data = processor.dump_output_file(tested_data)
        assert data == tested_data
Exemplo n.º 23
0
 def test_unit_file_output_processor_err__file_do_not_exist(self):
     processor = MarshmallowProcessor()
     tested_data = HapicFile(file_path="_____________")
     with pytest.raises(ValidationException):
         processor.dump_output_file(tested_data)