2017-09-27 14:46:53 +03:00
|
|
|
import gzip
|
2017-09-27 22:23:59 +03:00
|
|
|
import struct
|
2017-09-27 14:46:53 +03:00
|
|
|
|
2020-07-04 13:18:39 +03:00
|
|
|
from .. import TLObject
|
2017-09-27 14:46:53 +03:00
|
|
|
|
|
|
|
|
|
|
|
class GzipPacked(TLObject):
|
2017-09-29 14:11:33 +03:00
|
|
|
CONSTRUCTOR_ID = 0x3072cfa1
|
2017-09-27 14:46:53 +03:00
|
|
|
|
|
|
|
def __init__(self, data):
|
|
|
|
self.data = data
|
|
|
|
|
|
|
|
@staticmethod
|
2018-10-04 17:15:51 +03:00
|
|
|
def gzip_if_smaller(content_related, data):
|
2017-10-17 20:54:24 +03:00
|
|
|
"""Calls bytes(request), and based on a certain threshold,
|
2017-09-27 14:46:53 +03:00
|
|
|
optionally gzips the resulting data. If the gzipped data is
|
|
|
|
smaller than the original byte array, this is returned instead.
|
|
|
|
|
|
|
|
Note that this only applies to content related requests.
|
|
|
|
"""
|
2018-10-04 17:15:51 +03:00
|
|
|
if content_related and len(data) > 512:
|
2017-10-17 20:54:24 +03:00
|
|
|
gzipped = bytes(GzipPacked(data))
|
2017-09-27 14:46:53 +03:00
|
|
|
return gzipped if len(gzipped) < len(data) else data
|
|
|
|
else:
|
|
|
|
return data
|
|
|
|
|
2017-10-17 20:54:24 +03:00
|
|
|
def __bytes__(self):
|
2017-09-29 14:11:33 +03:00
|
|
|
return struct.pack('<I', GzipPacked.CONSTRUCTOR_ID) + \
|
2017-09-27 22:23:59 +03:00
|
|
|
TLObject.serialize_bytes(gzip.compress(self.data))
|
2017-09-27 14:46:53 +03:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def read(reader):
|
2018-08-14 20:14:13 +03:00
|
|
|
constructor = reader.read_int(signed=False)
|
|
|
|
assert constructor == GzipPacked.CONSTRUCTOR_ID
|
2017-09-27 14:46:53 +03:00
|
|
|
return gzip.decompress(reader.tgread_bytes())
|
2018-06-09 14:11:49 +03:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def from_reader(cls, reader):
|
|
|
|
return GzipPacked(gzip.decompress(reader.tgread_bytes()))
|
2018-06-21 10:52:47 +03:00
|
|
|
|
|
|
|
def to_dict(self):
|
|
|
|
return {
|
|
|
|
'_': 'GzipPacked',
|
|
|
|
'data': self.data
|
|
|
|
}
|