-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.go
More file actions
146 lines (127 loc) · 3.64 KB
/
utils.go
File metadata and controls
146 lines (127 loc) · 3.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package main
import (
"errors"
"fmt"
"log"
"net/url"
"os"
"path/filepath"
"regexp"
"strings"
srvConfig "github.com/CHESSComputing/golib/config"
services "github.com/CHESSComputing/golib/services"
)
// FileEntry represents a directory entry
type FileEntry struct {
Did string `json:"did"`
EscDid string `json:"esc_did"`
Name string `json:"name"`
IsDir bool `json:"is_dir"`
Path string `json:"path"` // path here correspond to sub-path within raw location area
}
// getFileList returns a list of files and directories in the given path
func getFileList(did, path, spath string) ([]FileEntry, error) {
var entries []FileEntry
files, err := os.ReadDir(path)
if err != nil {
return nil, fmt.Errorf("[DataManagement.main.getFileList] os.ReadDir error: %w", err)
}
for _, file := range files {
entry := FileEntry{
Did: did,
EscDid: url.QueryEscape(did),
Name: file.Name(),
IsDir: file.IsDir(),
Path: filepath.Join(spath, file.Name()),
// Path: filepath.Join(path, file.Name()),
}
entries = append(entries, entry)
}
return entries, nil
}
// helper function to find meta-data record for given did
func findMetaDataRecord(did string) (map[string]any, error) {
var rec map[string]any
query := fmt.Sprintf("{\"did\":\"%s\"}", did)
var skeys []string
var sorder, idx int
limit := 1
records, err := services.MetaDataRecords(query, skeys, sorder, idx, limit)
if err != nil {
return rec, fmt.Errorf("[DataManagement.main.findMetaDataRecord] services.MetaDataRecords error: %w", err)
}
if len(records) != 1 {
msg := fmt.Sprintf("multiple records found for did=%s, records=%v", did, records)
return rec, errors.New(msg)
}
return records[0], nil
}
// findFiles recursively finds all files in idir matching the given pattern pat.
func findFiles(idir string, pat string) ([]string, error) {
if !strings.HasSuffix(idir, "/") {
idir += "/"
}
var files []string
// Compile the regex pattern
re, err := regexp.Compile(pat)
if err != nil {
return nil, fmt.Errorf("invalid regex pattern: %v", err)
}
// Walk through the directory
err = filepath.Walk(idir, func(path string, info os.FileInfo, err error) error {
if err != nil {
log.Println("WARNING:", err)
}
if pat == "all" {
// get all files
if !info.IsDir() {
files = append(files, path)
}
} else {
// Check if it's a regular file and matches the pattern
if !info.IsDir() && re.MatchString(info.Name()) {
files = append(files, path)
}
}
return nil
})
if err != nil {
return files, fmt.Errorf("[DataManagement.main.findFiles] filepath.Walk error: %w", err)
}
return files, nil
}
// fileExtensions finds all unique file extensions in the given directory and subdirectories.
func fileExtensions(idir string) []string {
if !strings.HasSuffix(idir, "/") {
idir += "/"
}
extMap := make(map[string]bool)
// Walk through the directory
err := filepath.Walk(idir, func(path string, info os.FileInfo, err error) error {
if err != nil {
log.Println("WARNING: filepath.Walk", err.Error())
}
// Check if it's a file
if !info.IsDir() {
ext := filepath.Ext(info.Name()) // Extract file extension
if ext != "" {
extMap[ext] = true // Store unique extensions
}
}
return nil
})
if err != nil {
log.Println("WARNING: filepath.Walk", err.Error())
// return default list of file extensions
if len(srvConfig.Config.DataManagement.FileExtensions) > 0 {
return srvConfig.Config.DataManagement.FileExtensions
}
return []string{"png", "jpg", "tiff", "mcs"}
}
// Convert map keys to a slice
var extensions []string
for ext := range extMap {
extensions = append(extensions, ext)
}
return extensions
}