-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidate_files.go
129 lines (106 loc) · 2.63 KB
/
validate_files.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
package activities
import (
"context"
"errors"
"fmt"
"io/fs"
"path/filepath"
"slices"
"go.artefactual.dev/tools/temporal"
"github.com/artefactual-sdps/preprocessing-sfa/internal/fformat"
"github.com/artefactual-sdps/preprocessing-sfa/internal/fvalidate"
"github.com/artefactual-sdps/preprocessing-sfa/internal/sip"
)
const ValidateFilesName = "validate-files"
type (
ValidateFiles struct {
identifier fformat.Identifier
validators []fvalidate.Validator
}
ValidateFilesParams struct {
SIP sip.SIP
}
ValidateFilesResult struct {
Failures []string
}
)
type fileFormats map[string]*fformat.FileFormat
func NewValidateFiles(idr fformat.Identifier, vdrs ...fvalidate.Validator) *ValidateFiles {
return &ValidateFiles{
identifier: idr,
validators: vdrs,
}
}
// Execute validates SIP files against a file format specification. The
// only format validator currently implemented verapdf for PDF/A.
func (a *ValidateFiles) Execute(ctx context.Context, params *ValidateFilesParams) (*ValidateFilesResult, error) {
formats, err := a.identifyFormats(ctx, params.SIP)
if err != nil {
return nil, fmt.Errorf("identifyFormats: %v", err)
}
failures, err := a.validateFiles(params.SIP, formats)
if err != nil {
return nil, fmt.Errorf("validateFiles: %v", err)
}
return &ValidateFilesResult{Failures: failures}, nil
}
func (a *ValidateFiles) identifyFormats(ctx context.Context, sip sip.SIP) (fileFormats, error) {
logger := temporal.GetLogger(ctx)
formats := make(fileFormats)
err := filepath.WalkDir(sip.ContentPath, func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if ctx.Err() != nil {
return errors.New("context cancelled")
}
if d.IsDir() {
return nil
}
ff, err := a.identifier.Identify(path)
if err != nil {
logger.Info("format identication failed", "path", path)
} else {
formats[path] = ff
}
return nil
})
if err != nil {
return nil, err
}
return formats, nil
}
func (a *ValidateFiles) validateFiles(
sip sip.SIP,
files fileFormats,
) ([]string, error) {
var failures []string
for _, v := range a.validators {
out, err := validate(v, sip.ContentPath, files)
if err != nil {
return nil, err
}
if out != "" {
failures = append(failures, out)
}
}
return failures, nil
}
func validate(v fvalidate.Validator, path string, files fileFormats) (string, error) {
var canValidate bool
allowedIds := v.FormatIDs()
for _, f := range files {
if slices.Contains(allowedIds, f.ID) {
canValidate = true
break
}
}
if !canValidate {
return "", nil
}
out, err := v.Validate(path)
if err != nil {
return "", err
}
return out, nil
}