Пример #1
0
    def render_GET(self, request):
        # no auth here on purpose, to allow anyone to view, even across home
        # servers.

        # TODO: A little crude here, we could do this better.
        filename = request.path.split('/')[-1]
        # be paranoid
        filename = re.sub("[^0-9A-z.-_]", "", filename)

        file_path = self.directory + "/" + filename

        logger.debug("Searching for %s", file_path)

        if os.path.isfile(file_path):
            # filename has the content type
            base64_contentype = filename.split(".")[1]
            content_type = base64.urlsafe_b64decode(base64_contentype)
            logger.info("Sending file %s", file_path)
            f = open(file_path, 'rb')
            request.setHeader('Content-Type', content_type)
            d = FileSender().beginFileTransfer(f, request)

            # after the file has been sent, clean up and finish the request
            def cbFinished(ignored):
                f.close()
                request.finish()
            d.addCallback(cbFinished)
        else:
            respond_with_json_bytes(
                request,
                404,
                json.dumps(cs_error("Not found", code=Codes.NOT_FOUND)),
                send_cors=True)

        return server.NOT_DONE_YET
Пример #2
0
 def test_pullFileConsumer(self):
     fileToSend = StringIO(fileData)
     clock = Clock()
     consumer = FileConsumer(clock)
     d = FileSender().beginFileTransfer(fileToSend, consumer)
     finished = []
     d.addCallback(finished.append)
     while not finished:
         clock.advance(1)
     self.assertEqual(consumer.value(), fileData)
Пример #3
0
def send_open_file(openFile, request):
    '''Use FileSender to asynchronously send an open file

    [JBY] From: http://stackoverflow.com/questions/1538617/http-download-very-big-file'''
    dd = FileSender().beginFileTransfer(openFile, request)

    def cbFinished(ignored):
        openFile.close()
        request.finish()
    
    dd.addCallback(cbFinished).addErrback(err)
Пример #4
0
def download_file(request, name, path, file_is_temp=False):
    file = open(path, 'r')

    def finalize(*args, **kwargs):
        request.finish()
        file.close()
        if file_is_temp:
            os.remove(path)

    request.setHeader('Content-Disposition', 'attachment; filename="{}"'.format(name))

    file_sender = FileSender().beginFileTransfer(file, request)
    file_sender.addCallback(finalize)

    return NOT_DONE_YET
def sendOpenFile(request, openFile):
    '''Use FileSender to asynchronously send an open file

    [JBY] From: http://stackoverflow.com/questions/1538617/http-download-very-big-file'''

    contentType, junk = mimetypes.guess_type(request.path)
    request.setHeader('Content-Type', contentType if contentType else 'text/plain')
    dd = FileSender().beginFileTransfer(openFile, request)

    def cbFinished(ignored):
        openFile.close()
        request.finish()
    
    dd.addErrback(err)
    dd.addCallback(cbFinished)
    return server.NOT_DONE_YET
Пример #6
0
    def render_GET(self, request):
        # no auth here on purpose, to allow anyone to view, even across home
        # servers.

        # TODO: A little crude here, we could do this better.
        filename = request.path.split('/')[-1]
        # be paranoid
        filename = re.sub("[^0-9A-z.-_]", "", filename)

        file_path = self.directory + "/" + filename

        logger.debug("Searching for %s", file_path)

        if os.path.isfile(file_path):
            # filename has the content type
            base64_contentype = filename.split(".")[1]
            content_type = base64.urlsafe_b64decode(base64_contentype)
            logger.info("Sending file %s", file_path)
            f = open(file_path, 'rb')
            request.setHeader('Content-Type', content_type)

            # cache for at least a day.
            # XXX: we might want to turn this off for data we don't want to
            # recommend caching as it's sensitive or private - or at least
            # select private. don't bother setting Expires as all our matrix
            # clients are smart enough to be happy with Cache-Control (right?)
            request.setHeader("Cache-Control",
                              "public,max-age=86400,s-maxage=86400")

            d = FileSender().beginFileTransfer(f, request)

            # after the file has been sent, clean up and finish the request
            def cbFinished(ignored):
                f.close()
                finish_request(request)

            d.addCallback(cbFinished)
        else:
            respond_with_json_bytes(request,
                                    404,
                                    json.dumps(
                                        cs_error("Not found",
                                                 code=Codes.NOT_FOUND)),
                                    send_cors=True)

        return server.NOT_DONE_YET
Пример #7
0
    def render_GET(self, request):
        # no auth here on purpose, to allow anyone to view, even across home
        # servers.

        # TODO: A little crude here, we could do this better.
        filename = request.path.split('/')[-1]
        # be paranoid
        filename = re.sub("[^0-9A-z.-_]", "", filename)

        file_path = self.directory + "/" + filename

        logger.debug("Searching for %s", file_path)

        if os.path.isfile(file_path):
            # filename has the content type
            base64_contentype = filename.split(".")[1]
            content_type = base64.urlsafe_b64decode(base64_contentype)
            logger.info("Sending file %s", file_path)
            f = open(file_path, 'rb')
            request.setHeader('Content-Type', content_type)

            # cache for at least a day.
            # XXX: we might want to turn this off for data we don't want to
            # recommend caching as it's sensitive or private - or at least
            # select private. don't bother setting Expires as all our matrix
            # clients are smart enough to be happy with Cache-Control (right?)
            request.setHeader(
                "Cache-Control", "public,max-age=86400,s-maxage=86400"
            )

            d = FileSender().beginFileTransfer(f, request)

            # after the file has been sent, clean up and finish the request
            def cbFinished(ignored):
                f.close()
                finish_request(request)
            d.addCallback(cbFinished)
        else:
            respond_with_json_bytes(
                request,
                404,
                json.dumps(cs_error("Not found", code=Codes.NOT_FOUND)),
                send_cors=True)

        return server.NOT_DONE_YET
Пример #8
0
    def render_GET(self, request):
        # no auth here on purpose, to allow anyone to view, even across home
        # servers.

        # TODO: A little crude here, we could do this better.
        filename = request.path.split('/')[-1]
        # be paranoid
        filename = re.sub("[^0-9A-z.-_]", "", filename)

        file_path = self.directory + "/" + filename

        logger.debug("Searching for %s", file_path)

        if os.path.isfile(file_path):
            # filename has the content type
            base64_contentype = filename.split(".")[1]
            content_type = base64.urlsafe_b64decode(base64_contentype)
            logger.info("Sending file %s", file_path)
            f = open(file_path, 'rb')
            request.setHeader('Content-Type', content_type)
            d = FileSender().beginFileTransfer(f, request)

            # after the file has been sent, clean up and finish the request
            def cbFinished(ignored):
                f.close()
                request.finish()

            d.addCallback(cbFinished)
        else:
            respond_with_json_bytes(request,
                                    404,
                                    json.dumps(
                                        cs_error("Not found",
                                                 code=Codes.NOT_FOUND)),
                                    send_cors=True)

        return server.NOT_DONE_YET
Пример #9
0
    def get(self, **kwargs):
        """
        Get the contents of the specified task log.  The log will be
        returned in CSV format with the following fields:

            * ISO8601 timestamp
            * Stream number (0 == stdout, 1 == stderr)
            * Line number
            * Parent process ID
            * Message the process produced

        The log file identifier is configurable and relies on the
        `jobtype_task_log_filename` configuration option.  See the
        configuration documentation for more information about the
        default value.

        .. http:get:: /api/v1/tasklogs/<identifier> HTTP/1.1

            **Request**

            .. sourcecode:: http

                GET /api/v1/tasklogs/<identifier> HTTP/1.1

            **Response**

            .. sourcecode:: http

                HTTP/1.1 200 OK
                Content-Type: text/csv

                2015-05-07T23:42:53.730975,0,15,42,Hello world

        :statuscode 200: The log file was found, it's content will be returned.
        """
        request = kwargs["request"]

        if request_from_master(request):
            config.master_contacted()

        if len(request.postpath) != 1:
            return (
                dumps({"error": "Did not specify a log identifier"}),
                BAD_REQUEST,
                {"Content-Type": ["application/json"]}
            )

        log_identifier = request.postpath[0]
        if "/" in log_identifier or "\\" in log_identifier:
            return (
                dumps({"error": "log_identifier must not contain "
                                "directory separators"}),
                BAD_REQUEST,
                {"Content-Type": ["application/json"]}
            )

        path = join(config["jobtype_task_logs"], log_identifier)

        try:
            logfile = self._open_file(path, "rb")
        except Exception as error:
            request.responseHeaders.setRawHeaders(
                "Content-Type", ["application/json"])

            if getattr(error, "errno", None) == ENOENT:
                return dumps({"error": "%s does not exist" % path}), NOT_FOUND

            logger.error("GET %s failed: %s", request.postpath, error)
            return dumps({"error": str(error)}), INTERNAL_SERVER_ERROR

        # TODO: deferToThread for open? (and possibly send)
        request.setResponseCode(OK)
        request.responseHeaders.setRawHeaders("Content-Type", ["text/csv"])
        deferred = FileSender().beginFileTransfer(logfile, request)
        deferred.addCallback(lambda *_: request.finish())
        deferred.addCallback(lambda *_: logfile.close())

        return NOT_DONE_YET