Skip to content

Commit

Permalink
Allows to have no content type
Browse files Browse the repository at this point in the history
  • Loading branch information
sbrunner committed Jan 1, 2021
1 parent 2690c99 commit 92023cf
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 9 deletions.
2 changes: 1 addition & 1 deletion tc-copy
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def main(argv):
try:
output_tilestore = TileStore.load(args[-1])
for arg in args[:-1]:
input_tilestore = TileStore.load(arg)
input_tilestore = TileStore.load(arg, allows_no_contenttype=options.add_content_type)
if bounding_pyramid:
tilestream = BoundingPyramidTileStore(bounding_pyramid).list()
else:
Expand Down
4 changes: 2 additions & 2 deletions tilecloud/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -694,7 +694,7 @@ def put_one(tile):
raise NotImplementedError

@staticmethod
def load(name): # pragma: no cover
def load(name, allows_no_contenttype=False): # pragma: no cover
"""
Construct a :class:`TileStore` from a name.
Expand Down Expand Up @@ -745,7 +745,7 @@ def load(name): # pragma: no cover
from tilecloud.layout.template import TemplateTileLayout
from tilecloud.store.url import URLTileStore

return URLTileStore((TemplateTileLayout(name),))
return URLTileStore((TemplateTileLayout(name),), allows_no_contenttype=allows_no_contenttype)
if name.startswith("memcached://"):
from tilecloud.layout.template import TemplateTileLayout
from tilecloud.lib.memcached import MemcachedClient
Expand Down
23 changes: 17 additions & 6 deletions tilecloud/store/url.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@


class URLTileStore(TileStore):
def __init__(self, tilelayouts, headers=None, **kwargs):
def __init__(self, tilelayouts, headers=None, allows_no_contenttype=False, **kwargs):
TileStore.__init__(self, **kwargs)
self.allows_no_contenttype = allows_no_contenttype
self.tilelayouts = tuple(tilelayouts)
self.session = requests.session()
if headers is not None:
Expand All @@ -29,16 +30,26 @@ def get_one(self, tile):
logger.info("GET %s", url)
try:
response = self.session.get(url)
if response.status_code == 404:
if response.status_code == 404 or response.status_code == 204:
return None
tile.content_encoding = response.headers.get("Content-Encoding")
tile.content_type = response.headers.get("Content-Type")
if response.status_code < 300:
tile.data = response.content
if tile.content_type.startswith("image/"):
tile.data = response.content
if response.status_code != 200:
tile.error = "Unsupportetd status code {}: {}".format(
response.status_code, response.reason
)
if tile.content_type:
if tile.content_type.startswith("image/"):
tile.data = response.content
else:
tile.error = response.text
else:
tile.error = response.text
if self.allows_no_contenttype:
tile.data = response.content
else:
tile.error = "The Content-Type header is missing"

else:
tile.error = response.reason
except requests.exceptions.RequestException as e:
Expand Down

0 comments on commit 92023cf

Please sign in to comment.