Skip to content

Open-source manufacturing ERP, MES and QMS. Quoting, MRP, inventory, shop floor, quality and lot/serial traceability on one Postgres schema, with a REST API and MCP server. Self-host or use Carbon Cloud.

1.4 KB
Raw Blame
// SPDX-License-Identifier: AGPL-3.0-only
// Carbon (github.com/crbnos/carbon). Modified or adapted versions of this file,
// including ports, remain AGPLv3; serving them over a network requires releasing their source.

import { downloadBlob } from "@carbon/files";
import { path } from "~/utils/path";

export type ModelDownloadResult = "ok" | "unavailable" | "error";

/**
 * Download a model's ORIGINAL file bytes via the api/model/download route
 * (which resolves `originalPath` and redirects through the auth-checked
 * /file/preview proxy — `.zst` raws are decompressed server-side).
 *
 * Returns a result instead of toasting so callers keep their own (localized)
 * error copy. "unavailable" = no original exists for this model (legacy rows
 * compacted before originals were retained); "error" = network/server failure.
 * Never saves a non-OK response body to disk — that is how corrupt "STEP"
 * downloads were born.
 */
export async function downloadModelFile(model: {
  modelId: string | null;
  modelName: string | null;
}): Promise<ModelDownloadResult> {
  if (!model.modelId || !model.modelName) return "error";
  try {
    const response = await fetch(path.to.api.modelDownload(model.modelId));
    if (!response.ok) {
      return response.status === 404 ? "unavailable" : "error";
    }
    downloadBlob(await response.blob(), model.modelName);
    return "ok";
  } catch {
    return "error";
  }
}