import { spawn } from "node:child_process"; import { existsSync } from "node:fs"; import log from "electron-log"; import path from "node:path"; import FormData from "form-data"; import { readFile } from "fs/promises"; import axios from "axios"; import { PatientInfo, SeriesInfo, StudyInfo } from "./pacs.type"; export const OrthancServerRoot = "http://localhost:8042"; export const getPacsPath = ( platform: "macos" | "windows", isDevelopment: boolean ): string => { const orthancExecFile = { macos: "orthanc-mac-24.8.1/Orthanc", windows: "orthanc-win64-1.12.4/Orthanc.exe", }; const basePath = isDevelopment ? path.join(process.env.VITE_PUBLIC, "../extraResources") : path.join(process.resourcesPath, "lib"); return path.join(basePath, orthancExecFile[platform]); }; export const runOrthancServer = (pacsPath: string) => { if (existsSync(pacsPath)) { const configPath = path.join(path.dirname(pacsPath), "config.json"); const child_process = spawn(pacsPath, [configPath]); child_process.stdout.on("data", (data) => log.info(data)); child_process.stderr.on("data", (data) => log.error(data)); } else { 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 => { 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( `${orthancUrl}/patients/${patientId}` ); const patientDetails = patientDetailsResponse.data; const studyIds = patientDetails.Studies; const studies: StudyInfo[] = []; for (const studyId of studyIds) { const studyDetailsResponse = await axios.get( `${orthancUrl}/studies/${studyId}` ); const studyDetails = studyDetailsResponse.data; const seriesIds = studyDetails.Series; const series: SeriesInfo[] = []; for (const seriesId of seriesIds) { const seriesDetailsResponse = await axios.get( `${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 } };