29 |
29 |
30 def lookup_path (self, url): |
30 def lookup_path (self, url): |
31 """ |
31 """ |
32 Lookup image by request path. |
32 Lookup image by request path. |
33 |
33 |
34 Returns name, path. |
34 Returns name, path, type. For dirs, type will be None. |
35 """ |
35 """ |
36 |
36 |
37 if not os.path.isdir(self.image_root): |
37 if not os.path.isdir(self.image_root): |
38 raise exceptions.InternalServerError("Server image_root has gone missing") |
38 raise exceptions.InternalServerError("Server image_root has gone missing") |
39 |
39 |
47 if not path.startswith(self.image_root): |
47 if not path.startswith(self.image_root): |
48 raise exceptions.NotFound(name) |
48 raise exceptions.NotFound(name) |
49 |
49 |
50 if not os.path.exists(path): |
50 if not os.path.exists(path): |
51 raise exceptions.NotFound(name) |
51 raise exceptions.NotFound(name) |
52 |
52 |
53 return name, path |
53 # determine time |
|
54 if os.path.isdir(path): |
|
55 return name, path, None |
|
56 else: |
|
57 basename, type = path.rsplit('.', 1) |
|
58 |
|
59 return name, path, type |
54 |
60 |
55 def get_image (self, url): |
61 def get_image (self, url): |
56 """ |
62 """ |
57 Return Image object. |
63 Return Image object. |
58 """ |
64 """ |
59 |
65 |
60 name, path = self.lookup_path(url) |
66 name, path, type = self.lookup_path(url) |
61 |
67 |
62 if os.path.isdir(path): |
68 if type not in self.IMAGE_TYPES: |
63 raise exceptions.BadRequest("Is a directory: {name}".format(name=name)) |
69 raise exceptions.BadRequest("Not a supported image: {name}: {type}".format(name=name, type=type)) |
64 |
|
65 basename, file_type = path.rsplit('.', 1) |
|
66 |
|
67 if file_type not in self.IMAGE_TYPES: |
|
68 raise exceptions.BadRequest("Not a supported image: {name}: {type}".format(name=name, type=file_type)) |
|
69 |
70 |
70 # get Image object |
71 # get Image object |
71 image = self.image_cache.get(path) |
72 image = self.image_cache.get(path) |
72 |
73 |
73 if not image: |
74 if not image: |