From 58a018169dfec5899b1dca0df9cbf8ebf89a8b72 Mon Sep 17 00:00:00 2001 From: Duda Dornelles Date: Tue, 11 Nov 2014 10:05:32 -0200 Subject: Removing duplication of onclose method for EncryptedFileStorage --- .../pixelated/support/encrypted_file_storage.py | 32 +++++++++------------- 1 file changed, 13 insertions(+), 19 deletions(-) (limited to 'service/pixelated/support/encrypted_file_storage.py') diff --git a/service/pixelated/support/encrypted_file_storage.py b/service/pixelated/support/encrypted_file_storage.py index 4720fe91..1776c62e 100644 --- a/service/pixelated/support/encrypted_file_storage.py +++ b/service/pixelated/support/encrypted_file_storage.py @@ -16,24 +16,10 @@ class EncryptedFileStorage(FileStorage): FileStorage.__init__(self, path, supports_mmap=False) def open_file(self, name, **kwargs): - def onclose(file_struct): - file_struct.seek(0) - content = file_struct.file.read() - encrypted_content = self.f.encrypt(content) - with open(self._fpath(name), 'w+b') as _f: - _f.write(encrypted_content) - - return self._open_file(name, onclose=onclose) + return self._open_encrypted_file(name, onclose=self._encrypt_index_on_close(name)) def create_file(self, name, excl=False, mode="w+b", **kwargs): - def onclose(file_struct): - file_struct.seek(0) - content = file_struct.file.read() - encrypted_content = self.f.encrypt(content) - with open(self._fpath(name), 'w+b') as _f: - _f.write(encrypted_content) - - f = StructFile(io.BytesIO(), name=name, onclose=onclose) + f = StructFile(io.BytesIO(), name=name, onclose=self._encrypt_index_on_close(name)) f.is_real = False return f @@ -41,19 +27,27 @@ class EncryptedFileStorage(FileStorage): name = name or "%s.tmp" % random_name() path = os.path.join(self.folder, name) tempstore = EncryptedFileStorage(path, self.masterkey) - # import pdb;pdb.set_trace() return tempstore.create() def file_length(self, name): - f = self._open_file(name) + f = self._open_encrypted_file(name) length = len(f.file.read()) f.close() return length + def _encrypt_index_on_close(self, name): + def wrapper(file_struct): + file_struct.seek(0) + content = file_struct.file.read() + encrypted_content = self.f.encrypt(content) + with open(self._fpath(name), 'w+b') as _f: + _f.write(encrypted_content) + return wrapper + def _decrypt(self, file_content): return self.f.decrypt(file_content) if len(file_content) else file_content - def _open_file(self, name, onclose=lambda x: None): + def _open_encrypted_file(self, name, onclose=lambda x: None): file_content = open(self._fpath(name), "rb").read() decrypted = self._decrypt(file_content) f = BufferFile(buffer(decrypted), name=name, onclose=onclose) -- cgit v1.2.3