def _decode_tile(data: bytes, tile: Tile) -> None:
    """Decode a tile."""
    image_len = struct.unpack("q", data[:8])[0]
    tile.data = data[8:(image_len + 8)]
    other = json.loads((data[(8 + image_len):]).decode("utf-8"))
    tile.content_encoding = other["content_encoding"]
    tile.content_type = other["content_type"]
Exemplo n.º 2
0
 def __call__(self, tile: Tile) -> Tile:
     assert tile.data is not None
     assert tile.content_encoding is None
     bytes_io = BytesIO()
     gzip_file = GzipFile(compresslevel=self.compresslevel,
                          fileobj=bytes_io,
                          mode="w")
     gzip_file.write(tile.data)
     gzip_file.close()
     tile.content_encoding = "gzip"
     tile.data = bytes_io.getvalue()
     return tile
Exemplo n.º 3
0
 def get_one(self, tile: Tile) -> Optional[Tile]:
     key_name = self.tilelayout.filename(tile.tilecoord, tile.metadata)
     try:
         response = self.client.get_object(Bucket=self.bucket, Key=key_name)
         tile.data = response["Body"].read()
         tile.content_encoding = response.get("ContentEncoding")
         tile.content_type = response.get("ContentType")
     except botocore.exceptions.ClientError as exc:
         if _get_status(exc) == 404:
             return None
         else:
             tile.error = exc
     return tile
Exemplo n.º 4
0
 def get_one(self, tile: Tile) -> Optional[Tile]:
     key_name = self.tilelayout.filename(tile.tilecoord, tile.metadata)
     try:
         blob = self.container_client.get_blob_client(blob=key_name)
         tile.data = blob.download_blob().readall()
         properties = blob.get_blob_properties()
         tile.content_encoding = properties.content_settings.content_encoding
         tile.content_type = properties.content_settings.content_type
     except ResourceNotFoundError:
         return None
     except Exception as exc:
         LOGGER.exception(exc)
         tile.error = exc
     return tile
Exemplo n.º 5
0
    def get_one(self, tile: Tile) -> Optional[Tile]:
        if tile is None:
            return None
        if self.bounding_pyramid is not None:
            if tile.tilecoord not in self.bounding_pyramid:
                return None
        tilelayout = self.tilelayouts[hash(tile.tilecoord) % len(self.tilelayouts)]
        try:
            url = tilelayout.filename(tile.tilecoord, tile.metadata)
        except Exception as e:
            tile.error = e
            return tile

        logger.info("GET %s", url)
        try:
            response = self.session.get(url)
            if response.status_code in (404, 204):
                return None
            tile.content_encoding = response.headers.get("Content-Encoding")
            tile.content_type = response.headers.get("Content-Type")
            if response.status_code < 300:
                if response.status_code != 200:
                    tile.error = f"Unsupported status code {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:
                    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:
            tile.error = e
        return tile
Exemplo n.º 6
0
 def __call__(self, tile: Tile) -> Tile:
     assert tile.data is not None
     if tile.content_encoding == "gzip":
         tile.content_encoding = None
         tile.data = GzipFile(fileobj=BytesIO(tile.data)).read()
     return tile