Exemplo n.º 1
0
 def __init__(self, data):
     streams = [StringStream('{')]
     for key, value in data.items():
         if not isinstance(value, asyncio.StreamReader):
             value = StringStream(value)
         streams.extend([
             StringStream('"{}":"'.format(key)), value,
             StringStream('",')
         ])
     super().__init__(*(streams[:-1] + [StringStream('"}')]))
Exemplo n.º 2
0
    def __init__(self, file_tuple):
        filename, stream = file_tuple
        filename = filename.strip('/')
        # Build a ZipInfo instance to use for the file's header and footer
        self.zinfo = zipfile.ZipInfo(
            filename=filename,
            date_time=time.localtime(time.time())[:6],
        )
        self.zinfo.compress_type = zipfile.ZIP_DEFLATED
        self.zinfo.external_attr = 0o600 << 16
        self.zinfo.header_offset = 0
        self.zinfo.flag_bits |= 0x08
        # Initial CRC: value will be updated as file is streamed
        self.zinfo.CRC = 0

        # define a compressor
        self.compressor = zlib.compressobj(
            zlib.Z_DEFAULT_COMPRESSION,
            zlib.DEFLATED,
            -15,
        )

        # meta information - needed to build the footer
        self.original_size = 0
        self.compressed_size = 0

        super().__init__(
            StringStream(self.local_header),
            ZipLocalFileData(self, stream),
            ZipLocalFileDescriptor(self),
        )
Exemplo n.º 3
0
    def add_file(self,
                 field_name,
                 file_stream,
                 file_name=None,
                 mime='application/octet-stream',
                 disposition='file',
                 transcoding='binary'):
        assert self.can_add_more, 'Cannot add more fields after calling finalize or read'

        header = self.make_header(field_name,
                                  disposition=disposition,
                                  filename=file_name,
                                  additional_headers={
                                      'Content-Type': mime,
                                      'Content-Transfer-Encoding': transcoding
                                  })

        self.add_streams(self._make_boundary_stream(), StringStream(header),
                         file_stream, StringStream('\r\n'))
Exemplo n.º 4
0
    def __init__(self, file_tuple):
        filename, stream = file_tuple
        # Build a ZipInfo instance to use for the file's header and footer
        self.zinfo = zipfile.ZipInfo(
            filename=filename,
            date_time=time.localtime(time.time())[:6],
        )
        # If the file is a directory, set the directory flag, turn off compression
        if self.zinfo.filename[-1] == '/':
            self.zinfo.external_attr = 0o40775 << 16  # drwxrwxr-x
            self.zinfo.external_attr |= 0x10  # Directory flag
            self.zinfo.compress_type = zipfile.ZIP_STORED
            self.compressor = None
        else:
            self.zinfo.external_attr = 0o600 << 16  # rw-------
            # define a compressor
            self.zinfo.compress_type = zipfile.ZIP_DEFLATED
            self.compressor = zlib.compressobj(
                zlib.Z_DEFAULT_COMPRESSION,
                zlib.DEFLATED,
                -15,
            )

        self.zinfo.header_offset = 0
        self.zinfo.flag_bits |= 0x08
        # Initial CRC: value will be updated as file is streamed
        self.zinfo.CRC = 0

        # meta information - needed to build the footer
        self.original_size = 0
        self.compressed_size = 0
        self.need_zip64_data_descriptor = False

        super().__init__(
            StringStream(self.local_header),
            ZipLocalFileData(self, stream),
            ZipLocalFileDataDescriptor(self),
        )
Exemplo n.º 5
0
 def end_boundary(self):
     return StringStream('--{}--\r\n'.format(self.boundary))
Exemplo n.º 6
0
 def _make_boundary_stream(self):
     return StringStream('--{}\r\n'.format(self.boundary))
Exemplo n.º 7
0
    def add_field(self, key, value):
        assert self.can_add_more, 'Cannot add more fields after calling finalize or read'

        self.add_streams(self._make_boundary_stream(),
                         StringStream(self.make_header(key) + value + '\r\n'))