diff options
author | Duda Dornelles <ddornell@thoughtworks.com> | 2014-11-11 10:05:32 -0200 |
---|---|---|
committer | Duda Dornelles <ddornell@thoughtworks.com> | 2014-11-11 10:05:32 -0200 |
commit | 58a018169dfec5899b1dca0df9cbf8ebf89a8b72 (patch) | |
tree | b32e49da888a26e3aeb796440b8c43609782a8bd /service/pixelated/support | |
parent | 0f0d45ca5cb4e48e9e1a2e175517e6bb26826566 (diff) |
Removing duplication of onclose method for EncryptedFileStorage
Diffstat (limited to 'service/pixelated/support')
-rw-r--r-- | service/pixelated/support/encrypted_file_storage.py | 32 |
1 files changed, 13 insertions, 19 deletions
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) |