fix: findscu go mkiso

This commit is contained in:
2026-06-05 08:33:09 +07:00
parent 983667a76a
commit 1142c86e70
2 changed files with 135 additions and 41 deletions

View File

@@ -7,6 +7,7 @@ import (
"log/slog"
"os"
"os/exec"
"regexp"
"strconv"
"strings"
"sync"
@@ -101,17 +102,72 @@ func StartStoresCP(ctx context.Context, bin, aeTitle string, port int, outputDir
return ch, stop, nil
}
// RunMoveSCU executes movescu for the given accession number.
// RunFindSCUStudyUIDs queries PACS by accession number and returns matching StudyInstanceUID values.
func RunFindSCUStudyUIDs(ctx context.Context, bin, ourAE, pacsAE, pacsHost string, pacsPort int, accessionNumber string, timeoutSec int) (studyUIDs []string, stdout, stderr string, err error) {
args := []string{
"-v",
"-aet", ourAE,
"-aec", pacsAE,
pacsHost, strconv.Itoa(pacsPort),
"-S",
"-k", "0008,0052=STUDY",
"-k", fmt.Sprintf("0008,0050=%s", accessionNumber),
"-k", "0020,000D",
}
if timeoutSec > 0 {
args = append(args, "-to", strconv.Itoa(timeoutSec))
}
slog.Info("running findscu",
"bin", bin,
"accession", accessionNumber,
"pacs", fmt.Sprintf("%s@%s:%d", pacsAE, pacsHost, pacsPort),
)
cmd := exec.CommandContext(ctx, bin, args...)
var outBuf, errBuf bytes.Buffer
cmd.Stdout = &outBuf
cmd.Stderr = &errBuf
err = cmd.Run()
stdout = outBuf.String()
stderr = errBuf.String()
combined := stdout + "\n" + stderr
if err != nil {
return nil, stdout, stderr, fmt.Errorf("findscu failed: %w", err)
}
uidRe := regexp.MustCompile(`\(0020,000d\) UI \[([^\]]+)\]`)
seen := make(map[string]bool)
for _, match := range uidRe.FindAllStringSubmatch(combined, -1) {
uid := strings.TrimSpace(match[1])
if uid == "" || seen[uid] {
continue
}
seen[uid] = true
studyUIDs = append(studyUIDs, uid)
}
if len(studyUIDs) == 0 {
return nil, stdout, stderr, fmt.Errorf("no StudyInstanceUID found for accession %q", accessionNumber)
}
return studyUIDs, stdout, stderr, nil
}
// RunMoveSCUByStudyUID executes movescu for the given StudyInstanceUID.
// It uses Study Root query/retrieve model (via -S flag).
func RunMoveSCU(ctx context.Context, bin, ourAE, pacsAE, pacsHost string, pacsPort, moveDestPort int, accessionNumber string, timeoutSec int) (exitCode int, stdout, stderr string, err error) {
func RunMoveSCUByStudyUID(ctx context.Context, bin, ourAE, pacsAE, pacsHost string, pacsPort, moveDestPort int, studyUID string, timeoutSec int) (exitCode int, stdout, stderr string, err error) {
args := []string{
"-aet", ourAE,
"-aec", pacsAE,
"-aem", ourAE,
pacsHost, strconv.Itoa(pacsPort),
"-S", // Study Root query/retrieve
"-k", fmt.Sprintf("0008,0050=%s", accessionNumber),
"-k", "0010,0020=", // Patient ID wildcard (required for Study Root)
"-k", "0008,0052=STUDY",
"-k", fmt.Sprintf("0020,000D=%s", studyUID),
"--no-port",
}
@@ -121,7 +177,7 @@ func RunMoveSCU(ctx context.Context, bin, ourAE, pacsAE, pacsHost string, pacsPo
slog.Info("running movescu",
"bin", bin,
"accession", accessionNumber,
"study_uid", studyUID,
"pacs", fmt.Sprintf("%s@%s:%d", pacsAE, pacsHost, pacsPort),
"dest", fmt.Sprintf("%s:%d", ourAE, moveDestPort),
)