[v2] encode json for v2, move de/compress to Paste class (#13)

This commit is contained in:
r4sas
2019-06-03 07:17:15 +00:00
parent 5b38c532a2
commit 4c124a33c0
3 changed files with 40 additions and 36 deletions

View File

@@ -1,4 +1,4 @@
import json, ntpath, os, zlib
import json, ntpath, os
from base64 import b64encode, b64decode
class PBinCLIException(Exception):
@@ -22,24 +22,5 @@ def check_writable(f):
raise PBinCLIException("Path is not writable: {}".format(f))
def decompress(s, ver = 1):
if ver == 2:
return zlib.decompress(s, -zlib.MAX_WBITS)
else:
return zlib.decompress(bytearray(map(ord, b64decode(s.encode('utf-8')).decode('utf-8'))), -zlib.MAX_WBITS)
def compress(s, ver = 1):
if ver == 2:
# using compressobj as compress doesn't let us specify wbits
# needed to get the raw stream without headers
co = zlib.compressobj(wbits=-zlib.MAX_WBITS)
return co.compress(s) + co.flush()
else:
co = zlib.compressobj(wbits=-zlib.MAX_WBITS)
b = co.compress(s) + co.flush()
return b64encode(''.join(map(chr, b)).encode('utf-8'))
def json_encode(s):
return json.dumps(s, separators=(',',':')).encode()