Beispiel #1
0
    async def forward_chunk_request(self,
                                    request: web.Request) -> web.Response:
        """Redirects a precomp chunk request to the original URL"""
        encoded_original_url = request.match_info.get("encoded_original_url")
        if not encoded_original_url:
            return web.Response(status=400, text="Missing parameter: url")
        decoded_url = b64decode(encoded_original_url,
                                altchars=b'-_').decode('utf8')
        url = Url.parse(decoded_url)
        if url is None:
            return web.Response(status=400, text=f"Bad url: {decoded_url}")
        rest = request.match_info.get("rest", "").lstrip("/")
        tile_url = url.joinpath(rest)

        if tile_url.hostname != "data-proxy.ebrains.eu":
            raise web.HTTPFound(location=tile_url.schemeless_raw)

        token = UserToken.get_global_login_token()
        if isinstance(token, UsageError):
            return web.Response(status=403, text="Token has expired")  # FIXME

        async with self.http_client_session.get(
                tile_url.schemeless_raw,
                ssl=self.ssl_context,
                headers=token.as_auth_header(),
        ) as response:
            cscs_url = (await response.json())["url"]
            raise web.HTTPFound(location=cscs_url)
Beispiel #2
0
    async def stripped_precomputed_info(self,
                                        request: web.Request) -> web.Response:
        """Serves a precomp info stripped of all but one scales"""
        resolution_x = request.match_info.get("resolution_x")
        resolution_y = request.match_info.get("resolution_y")
        resolution_z = request.match_info.get("resolution_z")
        if resolution_x is None or resolution_y is None or resolution_z is None:
            return web.Response(
                status=400,
                text=
                f"Bad resolution: {resolution_x}_{resolution_y}_{resolution_z}"
            )
        try:
            resolution = (int(resolution_x), int(resolution_y),
                          int(resolution_z))
        except Exception:
            return web.Response(
                status=400,
                text=
                f"Bad resolution: {resolution_x}_{resolution_y}_{resolution_z}"
            )

        encoded_original_url = request.match_info.get("encoded_original_url")
        if not encoded_original_url:
            return web.Response(status=400, text="Missing parameter: url")

        decoded_url = b64decode(encoded_original_url,
                                altchars=b'-_').decode('utf8')
        base_url = Url.parse(decoded_url)
        if base_url is None:
            return web.Response(status=400, text=f"Bad url: {decoded_url}")
        info_url = base_url.joinpath("info")
        logger.debug(f"Will request this info: {info_url.schemeless_raw}")

        token = UserToken.get_global_login_token()
        if isinstance(token, UsageError):
            return web.Response(status=403, text="Token has expired")  # FIXME

        async with self.http_client_session.get(
                info_url.schemeless_raw,
                ssl=self.ssl_context,
                headers=token.as_auth_header()
                if info_url.hostname == "data-proxy.ebrains.eu" else {},
                params={"redirect": "true"}
                if info_url.hostname == "data-proxy.ebrains.eu" else {},
        ) as response:
            response_text = await response.text()
            if response.status // 100 != 2:
                return web.Response(status=response.status, text=response_text)
            info = PrecomputedChunksInfo.from_json_value(
                json.loads(response_text))

        stripped_info = info.stripped(resolution=resolution)
        return web.json_response(stripped_info.to_json_value())
Beispiel #3
0
 def try_create(
     cls,
     bucket_name: str,
     prefix: PurePosixPath,
     ebrains_user_token: "UserToken | None" = None
 ) -> "BucketFs | UsageError":
     token_result = ebrains_user_token or UserToken.get_global_login_token()
     if isinstance(token_result, UsageError):
         return token_result
     return BucketFs(bucket_name=bucket_name,
                     prefix=prefix,
                     ebrains_user_token=token_result)
Beispiel #4
0
 def from_json_value(cls, value: JsonValue) -> "BucketFs":
     value_obj = ensureJsonObject(value)
     raw_token = value_obj.get("ebrains_user_token")
     if raw_token is not None:
         token = UserToken.from_json_value(raw_token)
     else:
         token_resut = UserToken.get_global_login_token()
         if isinstance(token_resut, UsageError):
             raise token_resut
         token = token_resut
     return BucketFs(
         bucket_name=ensureJsonString(value_obj.get("bucket_name")),
         prefix=PurePosixPath(ensureJsonString(value_obj.get("prefix"))),
         ebrains_user_token=token)
Beispiel #5
0
    def __setstate__(self, value_obj: Dict[str, Any]):
        raw_token = value_obj.get("ebrains_user_token")
        if raw_token is not None:
            token = UserToken.from_json_value(raw_token)
        else:
            token_resut = UserToken.get_global_login_token()
            if isinstance(token_resut, UsageError):
                raise token_resut
            token = token_resut

        self.__init__(
            bucket_name=ensureJsonString(value_obj.get("bucket_name")),
            prefix=PurePosixPath(ensureJsonString(value_obj.get("prefix"))),
            ebrains_user_token=token,
        )
Beispiel #6
0
 def try_from_url(cls,
                  url: Url,
                  ebrains_user_token: "UserToken | None" = None
                  ) -> "BucketFs | Exception":
     if not url.raw.startswith(cls.API_URL.raw):
         return Exception(
             f"Url must be inside the data-proxy ({cls.API_URL}. Got {url}")
     bucket_name_part_index = len(cls.API_URL.path.parts)
     if len(url.path.parts) <= bucket_name_part_index:
         return Exception(f"Bad bucket url: {url}")
     token_result = ebrains_user_token or UserToken.get_global_login_token()
     if isinstance(token_result, UsageError):
         return token_result
     return BucketFs(
         bucket_name=url.path.parts[bucket_name_part_index],
         prefix=PurePosixPath("/".join(
             url.path.parts[bucket_name_part_index + 1:])),
         ebrains_user_token=token_result,
     )