Esempio n. 1
0
 def _on_add_failed(failure):
     failure.trap(ValueError, DuplicateTorrentFileError,
                  SchemeNotSupported)
     self._logger.exception(failure.value)
     request.write(
         BaseChannelsEndpoint.return_500(self, request, failure.value))
     request.finish()
    def render_PUT(self, request):
        """
        .. http:put:: /channels/discovered/(string: channelid)/torrents

        Add a torrent file to your own channel. Returns error 500 if something is wrong with the torrent file
        and DuplicateTorrentFileError if already added to your channel. The torrent data is passed as base-64 encoded
        string. The description is optional.

            **Example request**:

            .. sourcecode:: none

                curl -X PUT http://localhost:8085/channels/discovered/abcd/torrents
                --data "torrent=...&description=funny video"

            **Example response**:

            .. sourcecode:: javascript

                {
                    "added": True
                }

            :statuscode 404: if your channel does not exist.
            :statuscode 500: if the passed torrent data is corrupt.
        """
        channel = self.get_channel_from_db(self.cid)
        if channel is None:
            return ChannelsTorrentsEndpoint.return_404(request)

        parameters = http.parse_qs(request.content.read(), 1)

        if 'torrent' not in parameters or len(parameters['torrent']) == 0:
            request.setResponseCode(http.BAD_REQUEST)
            return json.dumps({"error": "torrent parameter missing"})

        if 'description' not in parameters or len(
                parameters['description']) == 0:
            extra_info = {}
        else:
            extra_info = {'description': parameters['description'][0]}

        try:
            torrent = base64.b64decode(parameters['torrent'][0])
            torrent_def = TorrentDef.load_from_memory(torrent)
            self.session.add_torrent_def_to_channel(channel[0],
                                                    torrent_def,
                                                    extra_info,
                                                    forward=True)

        except (DuplicateTorrentFileError, ValueError) as ex:
            return BaseChannelsEndpoint.return_500(self, request, ex)

        return json.dumps({"added": True})
Esempio n. 3
0
    def render_PUT(self, request):
        """
        .. http:put:: /channels/discovered

        Create your own new channel. The passed mode and descriptions are optional.
        Valid modes include: 'open', 'semi-open' or 'closed'. By default, the mode of the new channel is 'closed'.

            **Example request**:

            .. sourcecode:: none

                curl -X PUT http://localhost:8085/channels/discovered
                --data "name=fancy name&description=fancy description&mode=open"

            **Example response**:

            .. sourcecode:: javascript

                {
                    "added": 23
                }

            :statuscode 500: if a channel with the specified name already exists.
        """
        parameters = http.parse_qs(request.content.read(), 1)

        if 'name' not in parameters or len(parameters['name']) == 0 or len(
                parameters['name'][0]) == 0:
            request.setResponseCode(http.BAD_REQUEST)
            return json.dumps({"error": "channel name cannot be empty"})

        if 'description' not in parameters or len(
                parameters['description']) == 0:
            description = u''
        else:
            description = unicode(parameters['description'][0], 'utf-8')

        if 'mode' not in parameters or len(parameters['mode']) == 0:
            # By default, the mode of the new channel is closed.
            mode = u'closed'
        else:
            mode = unicode(parameters['mode'][0], 'utf-8')

        try:
            channel_id = self.session.create_channel(
                unicode(parameters['name'][0], 'utf-8'), description, mode)
        except DuplicateChannelNameError as ex:
            return BaseChannelsEndpoint.return_500(self, request, ex)

        return json.dumps({"added": channel_id})
Esempio n. 4
0
 def _on_timeout(_):
     request.write(
         BaseChannelsEndpoint.return_500(
             self, request, RuntimeError("Metainfo timeout")))
     request.finish()
Esempio n. 5
0
    def render_PUT(self, request):
        """
        .. http:put:: /channels/discovered/(string: channelid)/torrents

        Add a torrent file to your own channel. Returns error 500 if something is wrong with the torrent file
        and DuplicateTorrentFileError if already added to your channel. The torrent data is passed as base-64 encoded
        string. The description is optional.

            **Example request**:

            .. sourcecode:: none

                curl -X PUT http://localhost:8085/channels/discovered/abcd/torrents
                --data "torrent=...&description=funny video"

            **Example response**:

            .. sourcecode:: javascript

                {
                    "added": True
                }

            :statuscode 404: if your channel does not exist.
            :statuscode 500: if the passed torrent data is corrupt.
        """
        key = self.session.trustchain_keypair
        my_channel_id = key.pub().key_to_bin()

        # First check whether the channel actually exists
        if self.is_chant_channel:
            if my_channel_id != self.cid:
                request.setResponseCode(http.NOT_ALLOWED)
                return json.dumps({
                    "error":
                    "you can only add torrents to your own chant channel"
                })

            channel = self.session.lm.mds.ChannelMetadata.get_channel_with_id(
                my_channel_id)
            if not channel:
                return ChannelsTorrentsEndpoint.return_404(request)
        else:
            channel = self.get_channel_from_db(self.cid)
            if channel is None:
                return ChannelsTorrentsEndpoint.return_404(request)

        parameters = http.parse_qs(request.content.read(), 1)

        if 'torrent' not in parameters or len(parameters['torrent']) == 0:
            request.setResponseCode(http.BAD_REQUEST)
            return json.dumps({"error": "torrent parameter missing"})

        if 'description' not in parameters or len(
                parameters['description']) == 0:
            extra_info = {}
        else:
            extra_info = {'description': parameters['description'][0]}

        # Try to parse the torrent data
        try:
            torrent = base64.b64decode(parameters['torrent'][0])
            torrent_def = TorrentDef.load_from_memory(torrent)
        except ValueError as exc:
            return BaseChannelsEndpoint.return_500(self, request, exc)

        if self.is_chant_channel:
            try:
                channel.add_torrent_to_channel(torrent_def, extra_info)
            except DuplicateTorrentFileError as exc:
                return BaseChannelsEndpoint.return_500(self, request, exc)
        else:
            try:
                self.session.add_torrent_def_to_channel(channel[0],
                                                        torrent_def,
                                                        extra_info,
                                                        forward=True)
            except (DuplicateTorrentFileError, HttpError) as ex:
                return BaseChannelsEndpoint.return_500(self, request, ex)

        return json.dumps({"added": True})
 def _on_timeout(_):
     request.write(BaseChannelsEndpoint.return_500(self, request, RuntimeError("Metainfo timeout")))
     request.finish()
 def _on_add_failed(failure):
     failure.trap(ValueError, DuplicateTorrentFileError, SchemeNotSupported)
     self._logger.exception(failure.value)
     request.write(BaseChannelsEndpoint.return_500(self, request, failure.value))
     request.finish()
    def render_PUT(self, request):
        """
        .. http:put:: /channels/discovered/(string: channelid)/torrents

        Add a torrent file to your own channel. Returns error 500 if something is wrong with the torrent file
        and DuplicateTorrentFileError if already added to your channel. The torrent data is passed as base-64 encoded
        string. The description is optional.

            **Example request**:

            .. sourcecode:: none

                curl -X PUT http://localhost:8085/channels/discovered/abcd/torrents
                --data "torrent=...&description=funny video"

            **Example response**:

            .. sourcecode:: javascript

                {
                    "added": True
                }

            :statuscode 404: if your channel does not exist.
            :statuscode 500: if the passed torrent data is corrupt.
        """
        key = self.session.trustchain_keypair
        my_channel_id = key.pub().key_to_bin()

        # First check whether the channel actually exists
        if self.is_chant_channel:
            if my_channel_id != self.cid:
                request.setResponseCode(http.NOT_ALLOWED)
                return json.dumps({"error": "you can only add torrents to your own chant channel"})

            channel = self.session.lm.mds.ChannelMetadata.get_channel_with_id(my_channel_id)
            if not channel:
                return ChannelsTorrentsEndpoint.return_404(request)
        else:
            channel = self.get_channel_from_db(self.cid)
            if channel is None:
                return ChannelsTorrentsEndpoint.return_404(request)

        parameters = http.parse_qs(request.content.read(), 1)

        if 'torrent' not in parameters or len(parameters['torrent']) == 0:
            request.setResponseCode(http.BAD_REQUEST)
            return json.dumps({"error": "torrent parameter missing"})

        if 'description' not in parameters or len(parameters['description']) == 0:
            extra_info = {}
        else:
            extra_info = {'description': parameters['description'][0]}

        # Try to parse the torrent data
        try:
            torrent = base64.b64decode(parameters['torrent'][0])
            torrent_def = TorrentDef.load_from_memory(torrent)
        except ValueError as exc:
            return BaseChannelsEndpoint.return_500(self, request, exc)

        if self.is_chant_channel:
            try:
                channel.add_torrent_to_channel(torrent_def, extra_info)
            except DuplicateTorrentFileError as exc:
                return BaseChannelsEndpoint.return_500(self, request, exc)
        else:
            try:
                self.session.add_torrent_def_to_channel(channel[0], torrent_def, extra_info, forward=True)
            except (DuplicateTorrentFileError, HttpError) as ex:
                return BaseChannelsEndpoint.return_500(self, request, ex)

        return json.dumps({"added": True})
 def on_refresh_error(failure):
     self._logger.exception(failure.value)
     request.write(BaseChannelsEndpoint.return_500(self, request, failure.value))
     request.finish()
    def render_PUT(self, request):
        """
        .. http:put:: /channels/discovered

        Create your own new channel. The passed mode and descriptions are optional.
        Valid modes include: 'open', 'semi-open' or 'closed'. By default, the mode of the new channel is 'closed'.

            **Example request**:

            .. sourcecode:: none

                curl -X PUT http://localhost:8085/channels/discovered
                --data "name=fancy name&description=fancy description&mode=open"

            **Example response**:

            .. sourcecode:: javascript

                {
                    "added": 23
                }

            :statuscode 500: if a channel with the specified name already exists.
        """
        parameters = http.parse_qs(request.content.read(), 1)

        if 'name' not in parameters or len(parameters['name']) == 0 or len(parameters['name'][0]) == 0:
            request.setResponseCode(http.BAD_REQUEST)
            return json.dumps({"error": "channel name cannot be empty"})

        if 'description' not in parameters or len(parameters['description']) == 0:
            description = u''
        else:
            description = unicode(parameters['description'][0], 'utf-8')

        if self.session.config.get_chant_channel_edit():
            my_key = self.session.trustchain_keypair
            my_channel_id = my_key.pub().key_to_bin()

            # Do not allow to add a channel twice
            if self.session.lm.mds.get_my_channel():
                request.setResponseCode(http.INTERNAL_SERVER_ERROR)
                return json.dumps({"error": "channel already exists"})

            title = unicode(parameters['name'][0], 'utf-8')
            self.session.lm.mds.ChannelMetadata.create_channel(title, description)
            return json.dumps({
                "added": str(my_channel_id).encode("hex"),
            })

        if 'mode' not in parameters or len(parameters['mode']) == 0:
            # By default, the mode of the new channel is closed.
            mode = u'closed'
        else:
            mode = unicode(parameters['mode'][0], 'utf-8')

        try:
            channel_id = self.session.create_channel(unicode(parameters['name'][0], 'utf-8'), description, mode)
        except DuplicateChannelNameError as ex:
            return BaseChannelsEndpoint.return_500(self, request, ex)

        return json.dumps({"added": channel_id})
Esempio n. 11
0
    def render_PUT(self, request):
        """
        .. http:put:: /channels/discovered

        Create your own new channel. The passed mode and descriptions are optional.
        Valid modes include: 'open', 'semi-open' or 'closed'. By default, the mode of the new channel is 'closed'.

            **Example request**:

            .. sourcecode:: none

                curl -X PUT http://localhost:8085/channels/discovered
                --data "name=fancy name&description=fancy description&mode=open"

            **Example response**:

            .. sourcecode:: javascript

                {
                    "added": 23
                }

            :statuscode 500: if a channel with the specified name already exists.
        """
        parameters = http.parse_qs(request.content.read(), 1)

        if 'name' not in parameters or len(parameters['name']) == 0 or len(
                parameters['name'][0]) == 0:
            request.setResponseCode(http.BAD_REQUEST)
            return json.dumps({"error": "channel name cannot be empty"})

        if 'description' not in parameters or len(
                parameters['description']) == 0:
            description = u''
        else:
            description = unicode(parameters['description'][0], 'utf-8')

        if self.session.config.get_chant_channel_edit():
            my_key = self.session.trustchain_keypair
            my_channel_id = my_key.pub().key_to_bin()

            # Do not allow to add a channel twice
            if self.session.lm.mds.get_my_channel():
                request.setResponseCode(http.INTERNAL_SERVER_ERROR)
                return json.dumps({"error": "channel already exists"})

            title = unicode(parameters['name'][0], 'utf-8')
            self.session.lm.mds.ChannelMetadata.create_channel(
                title, description)
            return json.dumps({
                "added": str(my_channel_id).encode("hex"),
            })

        if 'mode' not in parameters or len(parameters['mode']) == 0:
            # By default, the mode of the new channel is closed.
            mode = u'closed'
        else:
            mode = unicode(parameters['mode'][0], 'utf-8')

        try:
            channel_id = self.session.create_channel(
                unicode(parameters['name'][0], 'utf-8'), description, mode)
        except DuplicateChannelNameError as ex:
            return BaseChannelsEndpoint.return_500(self, request, ex)

        return json.dumps({"added": channel_id})