def guest_download_check(self, context, request: TracimRequest, hapic_data=None) -> None: """ Check if share token is correct and password given valid """ 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 api.check_password(content_share, password=hapic_data.body.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()
def actives_shares(self) -> int: # TODO - G.M - 2019-08-12 - handle case where share app is not enabled, by # not starting it there. see #2189 from tracim_backend.applications.share.lib import ShareLib api = ShareLib(config=self.config, session=self.dbsession, current_user=self._user) return len(api.get_content_shares(self.content))
def direct_url(self) -> str: # TODO - G.M - 2019-07-31 - import here to avoid recursive import. from tracim_backend.applications.share.lib import ShareLib api = ShareLib(config=self.config, session=self.dbsession, current_user=self._user) return api.direct_api_url(self.content_share)
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, )
def disable_content_share(self, context, request: TracimRequest, hapic_data=None) -> None: """ remove a file share """ app_config = request.registry.settings["CFG"] # type: CFG api = ShareLib(current_user=request.current_user, session=request.dbsession, config=app_config) api.disable_content_share(request.current_content, hapic_data.path.share_id) return
def get_content_shares( self, context, request: TracimRequest, hapic_data=None) -> typing.List[ContentShareInContext]: """ Get all share related to a file """ app_config = request.registry.settings["CFG"] # type: CFG api = ShareLib( current_user=request.current_user, session=request.dbsession, config=app_config, show_disabled=hapic_data.query.show_disabled, ) shares_content = api.get_content_shares(request.current_content) return api.get_content_shares_in_context(shares_content)
def add_content_share( self, context, request: TracimRequest, hapic_data=None) -> typing.List[ContentShareInContext]: """ Allow to share this file to external person """ app_config = request.registry.settings["CFG"] # type: CFG api = ShareLib(current_user=request.current_user, session=request.dbsession, config=app_config) shares_content = api.share_content( request.current_content, hapic_data.body.emails, hapic_data.body.password, do_notify=app_config.EMAIL__NOTIFICATION__ACTIVATED, ) return api.get_content_shares_in_context(shares_content)
def get(self, current_user: typing.Optional[User] = None) -> ShareLib: return ShareLib( session=self.session, config=self.app_config, current_user=current_user or self.admin_user, )