示例#1
0
    def stream(self, stream, chunk_size=DEFAULT_CHUNK_SIZE, request=None):
        """
        if a controller function::

            return response.stream(file, 100)

        the file content will be streamed at 100 bytes at the time
        """

        if isinstance(stream, (str, unicode)):
            stream_file_or_304_or_206(stream, chunk_size=chunk_size, request=request, headers=self.headers)

        # ## the following is for backward compatibility

        if hasattr(stream, "name"):
            filename = stream.name
        else:
            filename = None
        keys = [item.lower() for item in self.headers]
        if filename and not "content-type" in keys:
            self.headers["Content-Type"] = contenttype(filename)
        if filename and not "content-length" in keys:
            try:
                self.headers["Content-Length"] = os.path.getsize(filename)
            except OSError:
                pass
        if request and request.env.web2py_use_wsgi_file_wrapper:
            wrapped = request.env.wsgi_file_wrapper(stream, chunk_size)
        else:
            wrapped = streamer(stream, chunk_size=chunk_size)
        return wrapped
示例#2
0
    def stream(
        self,
        stream,
        chunk_size=10 ** 6,
        request=None,
        ):
        """
        if a controller function
        > return response.stream(file,100)
        the file content will be streamed at 100 bytes at the time
        """

        if isinstance(stream, str):
            stream_file_or_304_or_206(stream, request=request,
                    chunk_size=chunk_size, headers=self.headers)

        # ## the following is for backward compatibility

        if hasattr(stream, 'name'):
            filename = stream.name
        else:
            filename = None
        keys = [item.lower() for item in self.headers.keys()]
        if filename and not 'content-type' in keys:
            self.headers['Content-Type'] = contenttype(filename)
        if filename and not 'content-length' in keys:
            self.headers['Content-Length'] = \
                os.stat(filename)[stat.ST_SIZE]
        self.body = streamer(stream, chunk_size)
        return self.body
示例#3
0
    def stream(self, stream, chunk_size=DEFAULT_CHUNK_SIZE, request=None, attachment=False, filename=None):
        """
        if a controller function::

            return response.stream(file, 100)

        the file content will be streamed at 100 bytes at the time

        Optional kwargs:
            (for custom stream calls)
            attachment=True # Send as attachment. Usually creates a
                            # pop-up download window on browsers
            filename=None # The name for the attachment

        Note: for using the stream name (filename) with attachments
        the option must be explicitly set as function parameter(will
        default to the last request argument otherwise)
        """

        # for attachment settings and backward compatibility
        keys = [item.lower() for item in self.headers]
        if attachment:
            if filename is None:
                attname = ""
            else:
                attname = filename
            self.headers["Content-Disposition"] = "attachment;filename=%s" % attname

        if not request:
            request = current.request
        if isinstance(stream, (str, unicode)):
            stream_file_or_304_or_206(stream, chunk_size=chunk_size, request=request, headers=self.headers)

        # ## the following is for backward compatibility
        if hasattr(stream, "name"):
            filename = stream.name

        if filename and not "content-type" in keys:
            self.headers["Content-Type"] = contenttype(filename)
        if filename and not "content-length" in keys:
            try:
                self.headers["Content-Length"] = os.path.getsize(filename)
            except OSError:
                pass

        # Internet Explorer < 9.0 will not allow downloads over SSL unless caching is enabled
        if (
            request.is_https
            and isinstance(request.env.http_user_agent, str)
            and not re.search(r"Opera", request.env.http_user_agent)
            and re.search(r"MSIE [5-8][^0-9]", request.env.http_user_agent)
        ):
            self.headers["Pragma"] = "cache"
            self.headers["Cache-Control"] = "private"

        if request and request.env.web2py_use_wsgi_file_wrapper:
            wrapped = request.env.wsgi_file_wrapper(stream, chunk_size)
        else:
            wrapped = streamer(stream, chunk_size=chunk_size)
        return wrapped
示例#4
0
    def stream(
        self,
        stream,
        chunk_size=10**6,
        request=None,
    ):
        """
        if a controller function
        > return response.stream(file,100)
        the file content will be streamed at 100 bytes at the time
        """

        if isinstance(stream, str):
            stream_file_or_304_or_206(stream,
                                      request=request,
                                      chunk_size=chunk_size,
                                      headers=self.headers)

        # ## the following is for backward compatibility

        if hasattr(stream, 'name'):
            filename = stream.name
        else:
            filename = None
        keys = [item.lower() for item in self.headers.keys()]
        if filename and not 'content-type' in keys:
            self.headers['Content-Type'] = contenttype(filename)
        if filename and not 'content-length' in keys:
            self.headers['Content-Length'] = \
                os.stat(filename)[stat.ST_SIZE]
        self.body = streamer(stream, chunk_size)
        return self.body
示例#5
0
文件: globals.py 项目: LOSC/LOSCHome
    def stream(
        self,
        stream,
        chunk_size = DEFAULT_CHUNK_SIZE,
        request=None,
        ):
        """
        if a controller function::

            return response.stream(file, 100)

        the file content will be streamed at 100 bytes at the time
        """
        if not request:
            request = current.request
        if isinstance(stream, (str, unicode)):
            stream_file_or_304_or_206(stream,
                                      chunk_size=chunk_size,
                                      request=request,
                                      headers=self.headers)

        # ## the following is for backward compatibility

        if hasattr(stream, 'name'):
            filename = stream.name
        else:
            filename = None
        keys = [item.lower() for item in self.headers]
        if filename and not 'content-type' in keys:
            self.headers['Content-Type'] = contenttype(filename)
        if filename and not 'content-length' in keys:
            try:
                self.headers['Content-Length'] = \
                    os.path.getsize(filename)
            except OSError:
                pass

        # Internet Explorer < 9.0 will not allow downloads over SSL unless caching is enabled
        if request.is_https and isinstance(request.env.http_user_agent,str) and \
                not re.search(r'Opera', request.env.http_user_agent) and \
                re.search(r'MSIE [5-8][^0-9]', request.env.http_user_agent):
            self.headers['Pragma'] = 'cache'
            self.headers['Cache-Control'] = 'private'

        if request and request.env.web2py_use_wsgi_file_wrapper:
            wrapped = request.env.wsgi_file_wrapper(stream, chunk_size)
        else:
            wrapped = streamer(stream, chunk_size=chunk_size)
        return wrapped
示例#6
0
    def stream(
        self,
        stream,
        chunk_size=DEFAULT_CHUNK_SIZE,
        request=None,
    ):
        """
        if a controller function::

            return response.stream(file, 100)

        the file content will be streamed at 100 bytes at the time
        """
        if not request:
            request = current.request
        if isinstance(stream, (str, unicode)):
            stream_file_or_304_or_206(stream,
                                      chunk_size=chunk_size,
                                      request=request,
                                      headers=self.headers)

        # ## the following is for backward compatibility

        if hasattr(stream, 'name'):
            filename = stream.name
        else:
            filename = None
        keys = [item.lower() for item in self.headers]
        if filename and not 'content-type' in keys:
            self.headers['Content-Type'] = contenttype(filename)
        if filename and not 'content-length' in keys:
            try:
                self.headers['Content-Length'] = \
                    os.path.getsize(filename)
            except OSError:
                pass

        # Internet Explorer < 9.0 will not allow downloads over SSL unless caching is enabled
        if request.is_https and isinstance(request.env.http_user_agent,str) and \
                not re.search(r'Opera', request.env.http_user_agent) and \
                re.search(r'MSIE [5-8][^0-9]', request.env.http_user_agent):
            self.headers['Pragma'] = 'cache'
            self.headers['Cache-Control'] = 'private'

        if request and request.env.web2py_use_wsgi_file_wrapper:
            wrapped = request.env.wsgi_file_wrapper(stream, chunk_size)
        else:
            wrapped = streamer(stream, chunk_size=chunk_size)
        return wrapped
示例#7
0
    def stream(
        self,
        stream,
        chunk_size=DEFAULT_CHUNK_SIZE,
        request=None,
    ):
        """
        if a controller function::

            return response.stream(file, 100)

        the file content will be streamed at 100 bytes at the time
        """

        if isinstance(stream, (str, unicode)):
            stream_file_or_304_or_206(stream,
                                      chunk_size=chunk_size,
                                      request=request,
                                      headers=self.headers)

        # ## the following is for backward compatibility

        if hasattr(stream, 'name'):
            filename = stream.name
        else:
            filename = None
        keys = [item.lower() for item in self.headers]
        if filename and not 'content-type' in keys:
            self.headers['Content-Type'] = contenttype(filename)
        if filename and not 'content-length' in keys:
            try:
                self.headers['Content-Length'] = \
                    os.path.getsize(filename)
            except OSError:
                pass
        if request and request.env.web2py_use_wsgi_file_wrapper:
            wrapped = request.env.wsgi_file_wrapper(stream, chunk_size)
        else:
            wrapped = streamer(stream, chunk_size=chunk_size)
        return wrapped
示例#8
0
    def stream(self,
               stream,
               chunk_size=DEFAULT_CHUNK_SIZE,
               request=None,
               attachment=False,
               filename=None):
        """
        if a controller function::

            return response.stream(file, 100)

        the file content will be streamed at 100 bytes at the time

        Optional kwargs:
            (for custom stream calls)
            attachment=True # Send as attachment. Usually creates a
                            # pop-up download window on browsers
            filename=None # The name for the attachment

        Note: for using the stream name (filename) with attachments
        the option must be explicitly set as function parameter(will
        default to the last request argument otherwise)
        """

        headers = self.headers
        # for attachment settings and backward compatibility
        keys = [item.lower() for item in headers]
        if attachment:
            if filename is None:
                attname = ""
            else:
                attname = filename
            headers["Content-Disposition"] = \
                "attachment;filename=%s" % attname

        if not request:
            request = current.request
        if isinstance(stream, (str, unicode)):
            stream_file_or_304_or_206(stream,
                                      chunk_size=chunk_size,
                                      request=request,
                                      headers=headers,
                                      status=self.status)

        # ## the following is for backward compatibility
        if hasattr(stream, 'name'):
            filename = stream.name

        if filename and not 'content-type' in keys:
            headers['Content-Type'] = contenttype(filename)
        if filename and not 'content-length' in keys:
            try:
                headers['Content-Length'] = \
                    os.path.getsize(filename)
            except OSError:
                pass

        env = request.env
        # Internet Explorer < 9.0 will not allow downloads over SSL unless caching is enabled
        if request.is_https and isinstance(env.http_user_agent, str) and \
                not re.search(r'Opera', env.http_user_agent) and \
                re.search(r'MSIE [5-8][^0-9]', env.http_user_agent):
            headers['Pragma'] = 'cache'
            headers['Cache-Control'] = 'private'

        if request and env.web2py_use_wsgi_file_wrapper:
            wrapped = env.wsgi_file_wrapper(stream, chunk_size)
        else:
            wrapped = streamer(stream, chunk_size=chunk_size)
        return wrapped