Skip to content

Commit

Permalink
It ignores expiration if STORAGE_EXPIRATION_SECONDS is None
Browse files Browse the repository at this point in the history
  • Loading branch information
guilhermef committed Jul 16, 2015
1 parent eaa4642 commit 3c24ff4
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 13 deletions.
32 changes: 20 additions & 12 deletions thumbor/storages/file_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,15 @@ def put_detector_data(self, path, data):

@return_future
def get(self, path, callback):
file_abspath = self.path_on_filesystem(path)
abs_path = self.path_on_filesystem(path)

if not exists(file_abspath) or self.__is_expired(file_abspath):
callback(None)
else:
callback(open(file_abspath, 'r').read())
def file_exists(resource_available):
if not resource_available:
callback(None)
else:
callback(open(self.path_on_filesystem(path), 'r').read())

self.exists(None, file_exists, path_on_filesystem=abs_path)

@return_future
def get_crypto(self, path, callback):
Expand All @@ -97,10 +100,12 @@ def get_detector_data(self, path, callback):
file_abspath = self.path_on_filesystem(path)
path = '%s.detectors.txt' % splitext(file_abspath)[0]

if not exists(path) or self.__is_expired(path):
callback(None)
else:
callback(loads(open(path, 'r').read()))
def file_exists(resource_available):
if not resource_available:
callback(None)
else:
callback(loads(open(path, 'r').read()))
self.exists(None, file_exists, path_on_filesystem=path)

def path_on_filesystem(self, path):
digest = hashlib.sha1(path.encode('utf-8')).hexdigest()
Expand All @@ -111,14 +116,17 @@ def path_on_filesystem(self, path):
)

@return_future
def exists(self, path, callback):
n_path = self.path_on_filesystem(path)
callback(os.path.exists(n_path))
def exists(self, path, callback, path_on_filesystem=None):
if path_on_filesystem is None:
path_on_filesystem = self.path_on_filesystem(path)
callback(os.path.exists(path_on_filesystem) and not self.__is_expired(path_on_filesystem))

def remove(self, path):
n_path = self.path_on_filesystem(path)
return os.remove(n_path)

def __is_expired(self, path):
if self.context.config.STORAGE_EXPIRATION_SECONDS is None:
return False
timediff = datetime.now() - datetime.fromtimestamp(getmtime(path))
return timediff.seconds > self.context.config.STORAGE_EXPIRATION_SECONDS
25 changes: 24 additions & 1 deletion vows/file_storage_vows.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
from thumbor.context import Context
from thumbor.config import Config
from fixtures.storage_fixture import IMAGE_URL, SAME_IMAGE_URL, IMAGE_BYTES, get_server
import tornado.concurrent


@Vows.batch
Expand Down Expand Up @@ -83,6 +82,30 @@ def should_not_be_null(self, topic):
def should_have_proper_bytes(self, topic):
expect(topic.result()).to_equal(IMAGE_BYTES)

class CannotGetExpiredImage(Vows.Context):
def topic(self):
config = Config(FILE_STORAGE_ROOT_PATH="/tmp/thumbor/file_storage/", STORAGE_EXPIRATION_SECONDS=-1)
storage = FileStorage(Context(config=config, server=get_server('ACME-SEC')))

storage.put(IMAGE_URL % 2, IMAGE_BYTES)
return storage.get(IMAGE_URL % 2)

def should_be_null(self, topic):
expect(topic.result()).to_be_null()
expect(topic.exception()).not_to_be_an_error()

class CanGetIfExpireSetToNone(Vows.Context):
def topic(self):
config = Config(FILE_STORAGE_ROOT_PATH="/tmp/thumbor/file_storage/", STORAGE_EXPIRATION_SECONDS=None)
storage = FileStorage(Context(config=config, server=get_server('ACME-SEC')))

storage.put(IMAGE_URL % 2, IMAGE_BYTES)
return storage.get(IMAGE_URL % 2)

def should_be_null(self, topic):
expect(topic.result()).not_to_be_null()
expect(topic.exception()).not_to_be_an_error()

class CryptoVows(Vows.Context):
class RaisesIfInvalidConfig(Vows.Context):

Expand Down

0 comments on commit 3c24ff4

Please sign in to comment.