1
0
mirror of https://github.com/dkmstr/openuds.git synced 2024-12-22 13:34:04 +03:00

Fixing up encoders

This commit is contained in:
Adolfo Gómez García 2017-11-15 14:21:45 +01:00
parent a43d6af237
commit 4cf62de3fd
4 changed files with 18 additions and 7 deletions

View File

@ -96,9 +96,9 @@ class AutoAttributes(Serializable):
return
# We keep original data (maybe incomplete)
try:
data = encoders.decode_bz2(data)
data = encoders.decode(data, 'bzip2')
except Exception: # With old zip encoding
data = encoders.decode_zip(data)
data = encoders.decode(data, 'zip')
for pair in data.split('\2'):
k, v = pair.split('\1')
self.dict[k] = pickle.loads(str(v))

View File

@ -50,6 +50,7 @@ logger = logging.getLogger(__name__)
class FileStorage(Storage):
def __init__(self, *args, **kwargs):
self._base_url = getattr(settings, 'FILE_STORAGE', '/files')
if self._base_url[-1] != '/':
@ -60,6 +61,7 @@ class FileStorage(Storage):
try:
cache = caches[cacheName]
except:
logger.info('No cache for FileStorage configured.')
cache = None
self.cache = cache
@ -73,7 +75,6 @@ class FileStorage(Storage):
Storage.__init__(self, *args, **kwargs)
def get_valid_name(self, name):
return name.replace('\\', os.path.sep)
@ -119,7 +120,6 @@ class FileStorage(Storage):
return
self.cache.delete(self._getKey(name))
def _open(self, name, mode='rb'):
f = six.BytesIO(self._dbFileForReadOnly(name).data)
f.name = name
@ -173,7 +173,9 @@ class FileStorage(Storage):
except DBFile.DoesNotExist:
return None
class CompressorFileStorage(FileStorage):
def __init__(self, *args, **kwargs):
FileStorage.__init__(self, *args, **dict(kwargs, owner='compressor'))

View File

@ -41,14 +41,14 @@ def __toBinary(data):
return data
def encode(data, encoder, asText):
def encode(data, encoder, asText=False):
res = codecs.encode(__toBinary(data), encoder)
if asText:
return res.decode('utf8')
return res
def decode(data, encoder, asText):
def decode(data, encoder, asText=False):
res = codecs.decode(__toBinary(data), encoder)
if asText:
return res.decode('utf8')

View File

@ -58,7 +58,16 @@ class DBFile(UUIDModel):
@property
def data(self):
return encoders.decode(encoders.decode(self.content, 'base64'), 'zip')
try:
return encoders.decode(encoders.decode(self.content, 'base64'), 'zip')
except Exception:
logger.error('DBFile {} has errors and cannot be used'.format(self.name))
try:
self.delete() # Autodelete, invalid...
except:
logger.error('Could not even delete {}!!'.format(self.name))
return ''
@data.setter
def data(self, value):