From be5230368f08fd6b77fcafbe2b5b0dd491b2d68b Mon Sep 17 00:00:00 2001 From: Duda Dornelles Date: Tue, 11 Nov 2014 10:34:35 -0200 Subject: Adding a file length cache for the EncryptedFileStorage so we don't have to open the files and decrypt them to find the length --- service/pixelated/support/encrypted_file_storage.py | 7 +++---- 1 file changed, 3 insertions(+), 4 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 6d94def7..95785baa 100644 --- a/service/pixelated/support/encrypted_file_storage.py +++ b/service/pixelated/support/encrypted_file_storage.py @@ -13,6 +13,7 @@ class EncryptedFileStorage(FileStorage): self.masterkey = masterkey self.f = Fernet(masterkey) self._tmp_storage = self.temp_storage + self.length_cache = {} FileStorage.__init__(self, path, supports_mmap=False) def open_file(self, name, **kwargs): @@ -29,15 +30,13 @@ class EncryptedFileStorage(FileStorage): return EncryptedFileStorage(path, self.masterkey).create() def file_length(self, name): - f = self._open_encrypted_file(name) - length = len(f.file.read()) - f.close() - return length + return self.length_cache[name] def _encrypt_index_on_close(self, name): def wrapper(struct_file): struct_file.seek(0) content = struct_file.file.read() + self.length_cache[name] = len(content) encrypted_content = self.f.encrypt(content) with open(self._fpath(name), 'w+b') as f: f.write(encrypted_content) -- cgit v1.2.3