Skip to Content
go-dicom(3)
Packages

fileutil

Small file helpers. Two are worth using directly: InstanceFilePath and ValidateUIDForPath, which turn a SOP Instance UID from a peer into a safe file path. The rest are byte-order, padding and caching helpers left from earlier designs.

import "github.com/amrshadid/go-dicom/fileutil" · pkg.go.dev

When to use it

When you write received instances to disk and name each file after its SOP Instance UID. The UID comes from the network; joined into a path unchecked, it lets the peer choose where the file goes. dcmstore and the command line's storescp and getscu use InstanceFilePath for this.

Key types and functions

NameWhat it is for
InstanceFilePathdir/<uid>.dcm, after checking the UID and that the result stays in dir.
ValidateUIDForPathDigits and dots only, at most 64 characters.
MaxUIDLength64.
PadValue, UnpadValuePad a value to even length, or strip the padding.
AlignToEvenBoundaryRound a position up to even.
DetectFromTransferSyntaxThe byte order a transfer syntax implies.
NewReaderWithBoundaryA filebase.Reader that stops at a position.

Example

// Name a received instance's file after its SOP Instance UID, safely.
package main

import (
	"fmt"

	"github.com/amrshadid/go-dicom/fileutil"
)

func main() {
	for _, sop := range []string{"1.2.826.0.1.3680043.2.1125.1", "../../etc/cron.d/x"} {
		path, err := fileutil.InstanceFilePath("received", sop)
		if err != nil {
			fmt.Println("refused:", err)
			continue
		}
		fmt.Println("write to", path)
	}
}

It prints write to received/1.2.826.0.1.3680043.2.1125.1.dcm and refuses the second UID.

Notes

  • ValidateUIDForPath enforces the 64-character limit that uid.UID.IsValid does not; it does not refuse components with leading zeros.
  • TagIndex, FileMetaCache, PixelDataCache, CodecIntegration, DeferredPixelDataReader, ByteBufferPool and ByteOrderDetector are not used by the reader, the writer or the network code.

Note

Apart from the two path functions, this is a thin helper package. Reading and writing go through filereader and filewriter; pixel data through dataset.

Important

Never build a file name from a UID with filepath.Join alone. It cleans a path but does not confine it.

See also

dcmstore · uid · network · Network SCP · TLS and security

On this page