Skip to content

Commit eedf786

Browse files
committed
Add support for multi-file NPE decrypt
1 parent ae49b17 commit eedf786

1 file changed

Lines changed: 55 additions & 0 deletions

File tree

main.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,61 @@ func DecryptFile(inputFilePath string, outputFilePath string, config OpentdfConf
538538
return outputFilePath, nil
539539
}
540540

541+
/*
542+
DecryptFilesInDirNPE decrypts all files in the specified directory
543+
Work is performed as an NPE (Non-Person Entity). Decrypted files are placed
544+
in the same directory as the input files, with the .tdf extension removed from the file name.
545+
*/
546+
func DecryptFilesInDirNPE(dirPath string, config OpentdfConfig) ([]string, error) {
547+
files, err := os.ReadDir(dirPath)
548+
if err != nil {
549+
return nil, err
550+
}
551+
552+
var outputPaths []string
553+
for _, file := range files {
554+
if !file.IsDir() && strings.HasSuffix(file.Name(), ".tdf") {
555+
inputFilePath := path.Join(dirPath, file.Name())
556+
outputFilePath := strings.TrimSuffix(inputFilePath, ".tdf")
557+
got, err := DecryptFile(inputFilePath, outputFilePath, config)
558+
if err != nil {
559+
log.Printf("Failed to decrypt file %s: %v", inputFilePath, err)
560+
return nil, err
561+
} else {
562+
outputPaths = append(outputPaths, got)
563+
}
564+
}
565+
}
566+
return outputPaths, nil
567+
}
568+
569+
/*
570+
DecryptFilesGlobNPE decrypts all files matching the specified glob pattern.
571+
Work is performed as an NPE (Non-Person Entity). Decrypted files are placed
572+
in the same directory as the input files, with the .tdf extension removed from the file name.
573+
*/
574+
func DecryptFilesGlobNPE(pattern string, config OpentdfConfig) ([]string, error) {
575+
files, err := filepath.Glob(pattern)
576+
if err != nil {
577+
return nil, err
578+
}
579+
580+
var outputPaths []string
581+
for _, inputFilePath := range files {
582+
if strings.HasSuffix(inputFilePath, ".tdf") {
583+
outputFilePath := strings.TrimSuffix(inputFilePath, ".tdf")
584+
got, err := DecryptFile(inputFilePath, outputFilePath, config)
585+
if err != nil {
586+
log.Printf("Failed to decrypt file %s: %v", inputFilePath, err)
587+
return nil, err
588+
} else {
589+
outputPaths = append(outputPaths, got)
590+
}
591+
}
592+
}
593+
return outputPaths, nil
594+
}
595+
541596
func DecryptFilePE(inputFilePath string, outputFilePath string, config OpentdfConfig, token TokenAuth) (string, error) {
542597
bytes, err := readBytesFromFile(inputFilePath)
543598
if err != nil {

0 commit comments

Comments
 (0)