|
| 1 | +package activities |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "io/fs" |
| 8 | + "path/filepath" |
| 9 | + "slices" |
| 10 | + |
| 11 | + "go.artefactual.dev/tools/temporal" |
| 12 | + |
| 13 | + "github.com/artefactual-sdps/preprocessing-sfa/internal/fformat" |
| 14 | + "github.com/artefactual-sdps/preprocessing-sfa/internal/fvalidate" |
| 15 | + "github.com/artefactual-sdps/preprocessing-sfa/internal/sip" |
| 16 | +) |
| 17 | + |
| 18 | +const ValidateFilesName = "validate-files" |
| 19 | + |
| 20 | +type ( |
| 21 | + ValidateFiles struct { |
| 22 | + identifier fformat.Identifier |
| 23 | + validators []fvalidate.Validator |
| 24 | + } |
| 25 | + ValidateFilesParams struct { |
| 26 | + SIP sip.SIP |
| 27 | + } |
| 28 | + ValidateFilesResult struct { |
| 29 | + Failures []string |
| 30 | + } |
| 31 | +) |
| 32 | + |
| 33 | +type fileFormats map[string]*fformat.FileFormat |
| 34 | + |
| 35 | +func NewValidateFiles(idr fformat.Identifier, vdrs ...fvalidate.Validator) *ValidateFiles { |
| 36 | + return &ValidateFiles{ |
| 37 | + identifier: idr, |
| 38 | + validators: vdrs, |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +// Execute validates SIP files against a file format specification. The |
| 43 | +// only format validator currently implemented verapdf for PDF/A. |
| 44 | +func (a *ValidateFiles) Execute(ctx context.Context, params *ValidateFilesParams) (*ValidateFilesResult, error) { |
| 45 | + formats, err := a.identifyFormats(ctx, params.SIP) |
| 46 | + if err != nil { |
| 47 | + return nil, fmt.Errorf("identifyFormats: %v", err) |
| 48 | + } |
| 49 | + |
| 50 | + failures, err := a.validateFiles(params.SIP, formats) |
| 51 | + if err != nil { |
| 52 | + return nil, fmt.Errorf("validateFiles: %v", err) |
| 53 | + } |
| 54 | + |
| 55 | + return &ValidateFilesResult{Failures: failures}, nil |
| 56 | +} |
| 57 | + |
| 58 | +func (a *ValidateFiles) identifyFormats(ctx context.Context, sip sip.SIP) (fileFormats, error) { |
| 59 | + logger := temporal.GetLogger(ctx) |
| 60 | + formats := make(fileFormats) |
| 61 | + err := filepath.WalkDir(sip.ContentPath, func(path string, d fs.DirEntry, err error) error { |
| 62 | + if err != nil { |
| 63 | + return err |
| 64 | + } |
| 65 | + |
| 66 | + if ctx.Err() != nil { |
| 67 | + return errors.New("context cancelled") |
| 68 | + } |
| 69 | + |
| 70 | + if d.IsDir() { |
| 71 | + return nil |
| 72 | + } |
| 73 | + |
| 74 | + ff, err := a.identifier.Identify(path) |
| 75 | + if err != nil { |
| 76 | + logger.Info("format identication failed", "path", path) |
| 77 | + } else { |
| 78 | + formats[path] = ff |
| 79 | + } |
| 80 | + |
| 81 | + return nil |
| 82 | + }) |
| 83 | + if err != nil { |
| 84 | + return nil, err |
| 85 | + } |
| 86 | + |
| 87 | + return formats, nil |
| 88 | +} |
| 89 | + |
| 90 | +func (a *ValidateFiles) validateFiles( |
| 91 | + sip sip.SIP, |
| 92 | + files fileFormats, |
| 93 | +) ([]string, error) { |
| 94 | + var failures []string |
| 95 | + for _, v := range a.validators { |
| 96 | + out, err := validate(v, sip.ContentPath, files) |
| 97 | + if err != nil { |
| 98 | + return nil, err |
| 99 | + } |
| 100 | + if out != "" { |
| 101 | + failures = append(failures, out) |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + return failures, nil |
| 106 | +} |
| 107 | + |
| 108 | +func validate(v fvalidate.Validator, path string, files fileFormats) (string, error) { |
| 109 | + var canValidate bool |
| 110 | + allowedIds := v.FormatIDs() |
| 111 | + |
| 112 | + for _, f := range files { |
| 113 | + if slices.Contains(allowedIds, f.ID) { |
| 114 | + canValidate = true |
| 115 | + break |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + if !canValidate { |
| 120 | + return "", nil |
| 121 | + } |
| 122 | + |
| 123 | + out, err := v.Validate(path) |
| 124 | + if err != nil { |
| 125 | + return "", err |
| 126 | + } |
| 127 | + |
| 128 | + return out, nil |
| 129 | +} |
0 commit comments