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
| Name | What it is for |
|---|---|
InstanceFilePath | dir/<uid>.dcm, after checking the UID and that the result stays in dir. |
ValidateUIDForPath | Digits and dots only, at most 64 characters. |
MaxUIDLength | 64. |
PadValue, UnpadValue | Pad a value to even length, or strip the padding. |
AlignToEvenBoundary | Round a position up to even. |
DetectFromTransferSyntax | The byte order a transfer syntax implies. |
NewReaderWithBoundary | A 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
ValidateUIDForPathenforces the 64-character limit thatuid.UID.IsValiddoes not; it does not refuse components with leading zeros.TagIndex,FileMetaCache,PixelDataCache,CodecIntegration,DeferredPixelDataReader,ByteBufferPoolandByteOrderDetectorare 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