summaryrefslogtreecommitdiff
path: root/service/pixelated/support
diff options
context:
space:
mode:
authorDuda Dornelles <ddornell@thoughtworks.com>2014-11-11 10:34:35 -0200
committerDuda Dornelles <ddornell@thoughtworks.com>2014-11-11 10:34:35 -0200
commitbe5230368f08fd6b77fcafbe2b5b0dd491b2d68b (patch)
treef632649af6334306695e2ab83aef48122cdebffa /service/pixelated/support
parentd2d01c13f202b6c51d01e48bf9cfef53e879f38a (diff)
Adding a file length cache for the EncryptedFileStorage so we don't have to open the files and decrypt them to find the length
Diffstat (limited to 'service/pixelated/support')
-rw-r--r--service/pixelated/support/encrypted_file_storage.py7
1 files changed, 3 insertions, 4 deletions
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)