-
Notifications
You must be signed in to change notification settings - Fork 0
/
amtp.go
205 lines (170 loc) · 5.7 KB
/
amtp.go
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package amtp
import (
"encoding/json"
"fmt"
"io"
"log"
"os"
"path/filepath"
"strings"
"github.com/nyudlts/go-aspace"
cp "github.com/otiai10/copy"
)
var options = cp.Options{}
var params Params
type Params struct {
Partner string
ResourceCode string
Source string
StagingLoc string
TransferInfo TransferInfo
}
type DC struct {
Title string `json:"title"`
IsPartOf string `json:"is_part_of"`
}
type TransferInfo struct {
ContactName string `yaml:"Contact-Name"`
ContactPhone string `yaml:"Contact-Phone"`
ContactEmail string `yaml:"Contact-Email"`
InternalSenderIdentifier string `yaml:"Internal-Sender-Identifier"`
OrganizationAdress string `yaml:"OrganizationAddress"`
SourceOrganization string `yaml:"SourceOrganization"`
ArchivesSpaceResourceURL string `yaml:"nyu-dl-archivesspace-resource-url"`
ResourceID string `yaml:"nyu-dl-resource-id"`
ResourceTitle string `yaml:"nyu-dl-resource-title"`
ContrentType string `yaml:"nyu-dl-content-type"`
ContentClassification string `yaml:"nyu-dl-content-classification"`
ProjectName string `yaml:"nyu-dl-project-name"`
RStarCollectionID string `yaml:"nyu-dl-rstar-collection-id"`
}
func ProcessWorkOrderRows(workOrder aspace.WorkOrder, p Params, numWorkers int) ([]string, error) {
params = p
options.PreserveTimes = true
options.NumOfWorkers = int64(numWorkers)
//chunk the workorder rows
log.Println("INFO chunking work order rows")
chunks := chunkRows(workOrder.Rows, numWorkers)
resultChan := make(chan []string)
for i, chunk := range chunks {
go processChunk(chunk, resultChan, i+1)
}
results := []string{}
for range chunks {
chunkResult := <-resultChan
results = append(results, chunkResult...)
}
return results, nil
}
func chunkRows(rows []aspace.WorkOrderRow, numWorkers int) [][]aspace.WorkOrderRow {
var divided [][]aspace.WorkOrderRow
chunkSize := (len(rows) + numWorkers - 1) / numWorkers
for i := 0; i < len(rows); i += chunkSize {
end := i + chunkSize
if end > len(rows) {
end = len(rows)
}
divided = append(divided, rows[i:end])
}
log.Printf("INFO create %d workorder row chunks", len(divided))
return divided
}
func processChunk(rows []aspace.WorkOrderRow, resultChan chan []string, workerId int) {
results := []string{}
for _, row := range rows {
if err := createERPackage(row, workerId); err != nil {
results = append(results, fmt.Sprintf("WORKER %d\t%s\t%s\n", workerId, row.GetComponentID(), err))
continue
}
results = append(results, fmt.Sprintf("WORKER %d\t%s\t%s\n", workerId, row.GetComponentID(), "SUCCESS"))
}
resultChan <- results
}
func createERPackage(row aspace.WorkOrderRow, workerId int) error {
erID := row.GetComponentID()
log.Printf("INFO WORKER %d processing %s", workerId, erID)
fmt.Printf("* WORKER %d processing %s\n", workerId, erID)
//create the staging directory
log.Printf("INFO WORKER %d creating directory in staging location %s", workerId, erID)
ERDirName := fmt.Sprintf("%s_%s_%s", params.Partner, params.ResourceCode, erID)
ERLoc := filepath.Join(params.StagingLoc, ERDirName)
if err := os.Mkdir(ERLoc, 0755); err != nil {
return err
}
//create the metadata directory
log.Printf("INFO WORKER %d creating metadata directory", workerId)
ERMDDirLoc := filepath.Join(ERLoc, "metadata")
if err := os.Mkdir(ERMDDirLoc, 0755); err != nil {
return err
}
//copy the transfer-info.txt files
log.Printf("INFO WORKER %d copying transfer-info.txt", workerId)
mdSourceFile := filepath.Join(params.Source, "metadata", "transfer-info.txt")
mdTarget := filepath.Join(ERMDDirLoc, "transfer-info.txt")
_, err := copyFile(mdSourceFile, mdTarget)
if err != nil {
return (err)
}
//create the workorder
log.Printf("INFO WORKER %d creating workorder", workerId)
woOutput := fmt.Sprintf("%s\n%s\n", strings.Join(aspace.HEADER_ROW, "\t"), row)
woLocation := filepath.Join(ERMDDirLoc, fmt.Sprintf("%s_%s_%s_aspace_wo.tsv", params.Partner, params.ResourceCode, erID))
if err := os.WriteFile(woLocation, []byte(woOutput), 0755); err != nil {
return err
}
//create the DC json
log.Printf("INFO WORKER %d creating dc.json", workerId)
dc := CreateDC(params.TransferInfo, row)
dcBytes, err := json.Marshal(dc)
if err != nil {
return err
}
dcLocation := filepath.Join(ERMDDirLoc, "dc.json")
if err := os.WriteFile(dcLocation, dcBytes, 0755); err != nil {
return (err)
}
//create the ER Directory
log.Printf("INFO WORKER %d creating data directory %s", workerId, erID)
dataDir := filepath.Join(ERLoc, erID)
if err := os.Mkdir(dataDir, 0755); err != nil {
return err
}
//copy files from source to target
payloadSource := filepath.Join(params.Source, erID)
payloadTarget := (filepath.Join(dataDir))
log.Printf("INFO WORKER %d copying %s to payload", workerId, erID)
if err := cp.Copy(payloadSource, payloadTarget, options); err != nil {
return err
}
//complete
log.Printf("INFO WORKER %d %s complete", workerId, erID)
fmt.Printf("* WORKER %d completed %s\n", workerId, erID)
return nil
}
func copyFile(src, dst string) (int64, error) {
sourceFileStat, err := os.Stat(src)
if err != nil {
return 0, err
}
if !sourceFileStat.Mode().IsRegular() {
return 0, fmt.Errorf("%s is not a regular file", src)
}
source, err := os.Open(src)
if err != nil {
return 0, err
}
defer source.Close()
destination, err := os.Create(dst)
if err != nil {
return 0, err
}
defer destination.Close()
nBytes, err := io.Copy(destination, source)
return nBytes, err
}
func CreateDC(transferInfo TransferInfo, row aspace.WorkOrderRow) DC {
dc := DC{}
dc.IsPartOf = fmt.Sprintf("AIC#%s: %s", transferInfo.ResourceID, transferInfo.ResourceTitle)
dc.Title = row.GetTitle()
return dc
}