Skip to content

Commit

Permalink
Create the specified temporary directory when Loris starts (#447)
Browse files Browse the repository at this point in the history
* Add a test to show that Loris doesn't create the temp directory

* Ensure Loris creates the temp directory on startup

* Add a test for directory creation in InfoCache

* Remove a missing import from webapp_t.py
  • Loading branch information
alexwlchan authored and bcail committed Mar 6, 2019
1 parent 81c2249 commit f7fbe29
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
8 changes: 7 additions & 1 deletion loris/webapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
SyntaxException,
TransformException,
)

from loris.utils import mkdir_p


getcontext().prec = 25 # Decimal precision. This should be plenty.
Expand Down Expand Up @@ -328,6 +328,12 @@ def __init__(self, app_configs={}):
# make the loris.Loris configs attrs for easier access
_loris_config = self.app_configs['loris.Loris']
self.tmp_dp = _loris_config['tmp_dp']

try:
mkdir_p(self.tmp_dp)
except Exception as exc:
raise ConfigError("Error creating tmp_dp %s: %r" % (self.tmp_dp, exc))

self.www_dp = _loris_config['www_dp']
self.enable_caching = _loris_config['enable_caching']
self.redirect_canonical_image_request = _loris_config['redirect_canonical_image_request']
Expand Down
17 changes: 17 additions & 0 deletions tests/img_info_t.py
Original file line number Diff line number Diff line change
Expand Up @@ -581,3 +581,20 @@ def test_looking_up_missing_item_is_keyerror(self):

with pytest.raises(KeyError):
cache[req]

def test_creates_cache_dir(self):
root = os.path.join(tempfile.mkdtemp(), "doesnotexist")
assert not os.path.exists(root)
cache = img_info.InfoCache(root=root)
path = self.test_jpeg_fp
req = webapp_t._get_werkzeug_request(path=path)

info = img_info.ImageInfo(
app=self.app,
ident=self.test_jpeg_uri,
src_img_fp=self.test_jpeg_fp,
src_format=self.test_jpeg_fmt
)

cache[req] = info
assert cache[req][0] == info
16 changes: 16 additions & 0 deletions tests/webapp_t.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from loris.authorizer import NullAuthorizer
from loris.loris_exception import ConfigError
from loris.transforms import KakaduJP2Transformer, OPJ_JP2Transformer
from loris.webapp import get_debug_config, Loris
from tests import loris_t


Expand Down Expand Up @@ -585,6 +586,21 @@ def test_cleans_up_when_not_caching(self):
pass
self._assert_tmp_has_no_files()

def test_can_use_tmp_dir_for_transforms(self):
config = get_debug_config('kdu')
config["loris.Loris"]["tmp_dp"] = "/tmp/doesnotexist"
self.build_client_from_config(config)

assert self.app.tmp_dp == "/tmp/doesnotexist"

self.client.get("/%s/full/full/0/default.jpg" % self.test_jpeg_id)

def test_invalid_tmp_dir_is_configerror(self):
config = get_debug_config('kdu')
config["loris.Loris"]["tmp_dp"] = "/dev/null/tmp"
with pytest.raises(ConfigError, match="Error creating tmp_dp /dev/null/tmp:"):
Loris(config)

def _assert_tmp_has_no_files(self):
# callback should delete the image before the test ends, so the tmp dir
# should not contain any files (there may be dirs)
Expand Down

0 comments on commit f7fbe29

Please sign in to comment.