Unverified Commit 21e7a222 authored by Marius van den Beek's avatar Marius van den Beek Committed by GitHub
Browse files

Merge pull request #18494 from mvdbeek/fix_invalid_id_error_message

[24.1] Increase API robustness to invalid requests, improve compressed data serving
parents 545ef625 92bfaa63
Loading
Loading
Loading
Loading
+22 −20
Original line number Diff line number Diff line
@@ -484,11 +484,12 @@ class Data(metaclass=DataMeta):

    def _serve_binary_file_contents_as_text(self, trans, data, headers, file_size, max_peek_size):
        headers["content-type"] = "text/html"
        with open(data.get_file_name(), "rb") as fh:
            return (
                trans.fill_template_mako(
                    "/dataset/binary_file.mako",
                    data=data,
                file_contents=open(data.get_file_name(), "rb").read(max_peek_size),
                    file_contents=fh.read(max_peek_size),
                    file_size=util.nice_size(file_size),
                    truncated=file_size > max_peek_size,
                ),
@@ -502,12 +503,13 @@ class Data(metaclass=DataMeta):
        if not preview or isinstance(data.datatype, images.Image) or file_size < max_peek_size:
            return self._yield_user_file_content(trans, data, data.get_file_name(), headers), headers

        with compression_utils.get_fileobj(data.get_file_name(), "rb") as fh:
            # preview large text file
            headers["content-type"] = "text/html"
            return (
                trans.fill_template_mako(
                    "/dataset/large_file.mako",
                truncated_data=open(data.get_file_name(), "rb").read(max_peek_size),
                    truncated_data=fh.read(max_peek_size),
                    data=data,
                ),
                headers,
+9 −8
Original line number Diff line number Diff line
@@ -193,10 +193,11 @@ class TabularData(Text):
                return open(dataset.get_file_name(), mode="rb"), headers
            else:
                headers["content-type"] = "text/html"
                with compression_utils.get_fileobj(dataset.get_file_name(), "rb") as fh:
                    return (
                        trans.fill_template_mako(
                            "/dataset/large_file.mako",
                        truncated_data=open(dataset.get_file_name()).read(max_peek_size),
                            truncated_data=fh.read(max_peek_size),
                            data=dataset,
                        ),
                        headers,
+4 −1
Original line number Diff line number Diff line
@@ -311,7 +311,10 @@ class HDAManager(

        truncated = preview and os.stat(file_path).st_size > MAX_PEEK_SIZE
        with get_fileobj(file_path) as fh:
            try:
                hda_data = fh.read(MAX_PEEK_SIZE)
            except UnicodeDecodeError:
                raise exceptions.RequestParameterInvalidException("Cannot generate text preview for dataset.")
        return truncated, hda_data

    # .... annotatable
+2 −1
Original line number Diff line number Diff line
@@ -2053,7 +2053,8 @@ def src_id_to_item(
        item = sa_session.get(src_to_class[value["src"]], decoded_id)
    except KeyError:
        raise ValueError(f"Unknown input source {value['src']} passed to job submission API.")
    assert item
    if not item:
        raise ValueError("Invalid input id passed to job submission API.")
    item.extra_params = {k: v for k, v in value.items() if k not in ("src", "id")}
    return item

+7 −2
Original line number Diff line number Diff line
@@ -28,8 +28,13 @@ class StatsdMiddleware:
        start_time = time.time()
        req = self.application(environ, start_response)
        dt = int((time.time() - start_time) * 1000)
        page = environ.get("controller_action_key", None) or environ.get("PATH_INFO", "NOPATH").strip("/").replace(
            "/", "."
        page = (
            environ.get("controller_action_key", None)
            or environ.get("PATH_INFO", "NOPATH")
            .strip("/")
            .replace("/", ".")
            .encode("ascii", errors="replace")
            .decode()
        )
        self.galaxy_stasd_client.timing(page, dt)
        try:
Loading