2024-09-10 22:03:12 +08:00
|
|
|
|
import { spawn } from "node:child_process";
|
2024-09-20 15:55:17 +08:00
|
|
|
|
import { existsSync, mkdirSync, writeFileSync } from "node:fs";
|
2024-09-11 17:01:22 +08:00
|
|
|
|
import log from "electron-log";
|
2024-09-10 22:03:12 +08:00
|
|
|
|
import path from "node:path";
|
2024-09-11 17:01:22 +08:00
|
|
|
|
import FormData from "form-data";
|
|
|
|
|
import { readFile } from "fs/promises";
|
|
|
|
|
import axios from "axios";
|
2024-09-20 15:55:17 +08:00
|
|
|
|
import { InstanceInfo, PatientInfo, SeriesInfo, StudyInfo } from "./pacs.type";
|
|
|
|
|
import { app } from "electron";
|
|
|
|
|
import pLimit from "p-limit";
|
2024-09-10 22:03:12 +08:00
|
|
|
|
|
2024-09-11 17:01:22 +08:00
|
|
|
|
export const OrthancServerRoot = "http://localhost:8042";
|
|
|
|
|
|
|
|
|
|
export const getPacsPath = (
|
|
|
|
|
platform: "macos" | "windows",
|
|
|
|
|
isDevelopment: boolean
|
|
|
|
|
): string => {
|
2024-09-10 22:03:12 +08:00
|
|
|
|
const orthancExecFile = {
|
|
|
|
|
macos: "orthanc-mac-24.8.1/Orthanc",
|
2024-09-11 17:01:22 +08:00
|
|
|
|
windows: "orthanc-win64-1.12.4/Orthanc.exe",
|
|
|
|
|
};
|
2024-09-10 22:03:12 +08:00
|
|
|
|
const basePath = isDevelopment
|
|
|
|
|
? path.join(process.env.VITE_PUBLIC, "../extraResources")
|
2024-09-11 17:01:22 +08:00
|
|
|
|
: path.join(process.resourcesPath, "lib");
|
|
|
|
|
return path.join(basePath, orthancExecFile[platform]);
|
|
|
|
|
};
|
2024-09-10 22:03:12 +08:00
|
|
|
|
|
|
|
|
|
export const runOrthancServer = (pacsPath: string) => {
|
|
|
|
|
if (existsSync(pacsPath)) {
|
2024-09-12 12:55:37 +08:00
|
|
|
|
const configPath = path.join(path.dirname(pacsPath), "config.json");
|
|
|
|
|
const child_process = spawn(pacsPath, [configPath]);
|
2024-09-13 15:55:29 +08:00
|
|
|
|
child_process.stdout.on("data", (data) => log.info(data.toString()));
|
|
|
|
|
child_process.stderr.on("data", (data) => log.error(data.toString()));
|
2024-09-10 22:03:12 +08:00
|
|
|
|
} else {
|
2024-09-11 17:01:22 +08:00
|
|
|
|
console.error("pacsPath is a not exist");
|
|
|
|
|
log.error("pacsPath is a not exist");
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 上传文件到到pacs
|
|
|
|
|
* @param {string} filePath 文件地址
|
|
|
|
|
* @param {string} orthancUrl orthanc的服务地址
|
|
|
|
|
* @returns
|
|
|
|
|
*/
|
|
|
|
|
export const uploadDicomFile = async (
|
|
|
|
|
filePath: string,
|
|
|
|
|
orthancUrl: string = OrthancServerRoot
|
|
|
|
|
): Promise<boolean> => {
|
|
|
|
|
try {
|
|
|
|
|
const buffer = await readFile(filePath);
|
|
|
|
|
const fd = new FormData();
|
|
|
|
|
fd.append("files", buffer);
|
|
|
|
|
const url = `${orthancUrl}/instances`;
|
|
|
|
|
const headers = { "Content-Type": "multipart/form-data" };
|
|
|
|
|
const { status } = await axios.post(url, fd, { headers });
|
|
|
|
|
return status === 200;
|
|
|
|
|
} catch (error) {
|
|
|
|
|
log.error("Failed to upload DICOM file:", error);
|
|
|
|
|
console.error("Failed to upload DICOM file:", error);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const selectStructuredDicom = async (
|
|
|
|
|
orthancUrl: string = OrthancServerRoot
|
|
|
|
|
) => {
|
|
|
|
|
try {
|
|
|
|
|
const response = await axios.get(`${orthancUrl}/patients`);
|
|
|
|
|
const patientIds: string[] = response.data;
|
|
|
|
|
const patients: PatientInfo[] = [];
|
|
|
|
|
|
|
|
|
|
for (const patientId of patientIds) {
|
|
|
|
|
const patientDetailsResponse = await axios.get<PatientInfo>(
|
|
|
|
|
`${orthancUrl}/patients/${patientId}`
|
|
|
|
|
);
|
|
|
|
|
const patientDetails = patientDetailsResponse.data;
|
|
|
|
|
|
|
|
|
|
const studyIds = patientDetails.Studies;
|
|
|
|
|
const studies: StudyInfo[] = [];
|
|
|
|
|
|
|
|
|
|
for (const studyId of studyIds) {
|
|
|
|
|
const studyDetailsResponse = await axios.get<StudyInfo>(
|
|
|
|
|
`${orthancUrl}/studies/${studyId}`
|
|
|
|
|
);
|
|
|
|
|
const studyDetails = studyDetailsResponse.data;
|
|
|
|
|
|
|
|
|
|
const seriesIds = studyDetails.Series;
|
|
|
|
|
const series: SeriesInfo[] = [];
|
|
|
|
|
|
|
|
|
|
for (const seriesId of seriesIds) {
|
|
|
|
|
const seriesDetailsResponse = await axios.get<SeriesInfo>(
|
|
|
|
|
`${orthancUrl}/series/${seriesId}`
|
|
|
|
|
);
|
|
|
|
|
const seriesDetails = seriesDetailsResponse.data;
|
|
|
|
|
series.push({ ...seriesDetails });
|
|
|
|
|
}
|
|
|
|
|
studies.push({ ...studyDetails, children: series });
|
|
|
|
|
}
|
|
|
|
|
patients.push({ ...patientDetails, children: studies });
|
|
|
|
|
}
|
|
|
|
|
return patients;
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error("Error fetching detailed patient information:", error);
|
|
|
|
|
throw error; // or handle it accordingly
|
2024-09-10 22:03:12 +08:00
|
|
|
|
}
|
2024-09-11 17:01:22 +08:00
|
|
|
|
};
|
2024-09-20 15:55:17 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 根据 SeriesInstanceUID 下载序列的所有 DICOM 文件并返回保存的文件夹路径
|
|
|
|
|
* @param seriesInstanceUID 序列的 SeriesInstanceUID
|
|
|
|
|
* @returns 保存的文件夹路径
|
|
|
|
|
*/
|
|
|
|
|
export const downloadSeriesDicomFiles = async (
|
|
|
|
|
seriesInstanceUID: string
|
|
|
|
|
): Promise<string> => {
|
|
|
|
|
try {
|
|
|
|
|
// 使用 Orthanc 的查询接口查找匹配的序列 ID
|
|
|
|
|
const findResponse = await axios.post<string[]>(
|
|
|
|
|
`${OrthancServerRoot}/tools/find`,
|
|
|
|
|
{
|
|
|
|
|
Level: "Series",
|
|
|
|
|
Query: {
|
|
|
|
|
SeriesInstanceUID: seriesInstanceUID,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const seriesIds = findResponse.data;
|
|
|
|
|
|
|
|
|
|
if (seriesIds.length === 0) {
|
|
|
|
|
throw new Error("在 Orthanc 中未找到指定的 SeriesInstanceUID。");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const seriesId = seriesIds[0]; // 假设只有一个匹配的序列
|
|
|
|
|
|
|
|
|
|
// 获取该序列中的所有实例(DICOM 文件)
|
|
|
|
|
const instancesResponse = await axios.get<string[]>(
|
|
|
|
|
`${OrthancServerRoot}/series/${seriesId}/instances`
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const instances = instancesResponse.data as unknown as InstanceInfo[];
|
|
|
|
|
|
|
|
|
|
// 创建保存 DICOM 文件的本地目录
|
|
|
|
|
const saveDir = path.join(
|
|
|
|
|
app.getPath("userData"),
|
|
|
|
|
"dicom",
|
|
|
|
|
seriesInstanceUID
|
|
|
|
|
);
|
|
|
|
|
if (!existsSync(saveDir)) {
|
|
|
|
|
mkdirSync(saveDir, { recursive: true });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 并发限制
|
|
|
|
|
const limit = pLimit(5); // 限制同时进行的请求数为 5
|
|
|
|
|
|
|
|
|
|
// 并行下载 DICOM 文件
|
|
|
|
|
await Promise.all(
|
|
|
|
|
instances.map((instance) => {
|
|
|
|
|
const instanceId = instance.ID;
|
|
|
|
|
return limit(async () => {
|
|
|
|
|
const dicomResponse = await axios.get<ArrayBuffer>(
|
|
|
|
|
`${OrthancServerRoot}/instances/${instanceId}/file`,
|
|
|
|
|
{
|
|
|
|
|
responseType: "arraybuffer",
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const filePath = path.join(saveDir, `${instanceId}.dcm`);
|
|
|
|
|
writeFileSync(filePath, Buffer.from(dicomResponse.data), "binary");
|
|
|
|
|
});
|
|
|
|
|
})
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
console.log(`所有 DICOM 文件已保存至:${saveDir}`);
|
|
|
|
|
return saveDir;
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error("下载 DICOM 文件时出错:", error);
|
|
|
|
|
throw error;
|
|
|
|
|
}
|
|
|
|
|
};
|