81 lines
2.8 KiB
Go
81 lines
2.8 KiB
Go
|
|
package artifacts
|
||
|
|
|
||
|
|
import (
|
||
|
|
"fmt"
|
||
|
|
"os"
|
||
|
|
"path/filepath"
|
||
|
|
|
||
|
|
"gitea.maximumdirect.net/eric/narratio/internal/artifactpolicy"
|
||
|
|
"gitea.maximumdirect.net/eric/narratio/internal/manifest"
|
||
|
|
)
|
||
|
|
|
||
|
|
// ValidateSuppliedProduct checks a recorded product against its current canonical
|
||
|
|
// location and actual local content. Configured product paths come from the
|
||
|
|
// current concrete catalog; built-in paths are fixed by the registry.
|
||
|
|
func ValidateSuppliedProduct(paths SessionPaths, source, canonicalPath string, record manifest.SuppliedProduct) error {
|
||
|
|
collection := &manifest.SuppliedProductCollection{Version: manifest.SuppliedProductVersion, Products: map[string]manifest.SuppliedProduct{source: record}}
|
||
|
|
if err := manifest.ValidateSuppliedProducts(collection); err != nil {
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
classified, _ := artifactpolicy.ClassifySource(source)
|
||
|
|
kind := contentText
|
||
|
|
if spec, ok := artifactRegistry[source]; ok {
|
||
|
|
canonicalPath = spec.CanonicalRelPath
|
||
|
|
kind = spec.ContentKind
|
||
|
|
} else if classified.Kind == artifactpolicy.SourceKindExtraction {
|
||
|
|
kind = contentJSON
|
||
|
|
}
|
||
|
|
if record.Path != canonicalPath {
|
||
|
|
return fmt.Errorf("supplied product %q has noncanonical path %q", source, record.Path)
|
||
|
|
}
|
||
|
|
path := filepath.Join(paths.Root, filepath.FromSlash(record.Path))
|
||
|
|
if err := requireSafeSessionRegularFile(paths.Root, path); err != nil {
|
||
|
|
return fmt.Errorf("supplied product %q: %w", source, err)
|
||
|
|
}
|
||
|
|
info, err := os.Stat(path)
|
||
|
|
if err != nil {
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
if info.Size() != record.Size {
|
||
|
|
return fmt.Errorf("supplied product %q: size mismatch", source)
|
||
|
|
}
|
||
|
|
if err := validateResolvedContent(path, kind); err != nil {
|
||
|
|
return fmt.Errorf("supplied product %q: %w", source, err)
|
||
|
|
}
|
||
|
|
checksum, err := SHA256File(path)
|
||
|
|
if err != nil {
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
if checksum != record.Checksum {
|
||
|
|
return fmt.Errorf("supplied product %q: checksum mismatch", source)
|
||
|
|
}
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
|
||
|
|
// SuppliedTranscriptCoverage returns transcript work satisfied by valid supplied
|
||
|
|
// products. It neither records nor creates the intermediate products.
|
||
|
|
func SuppliedTranscriptCoverage(paths SessionPaths, collection *manifest.SuppliedProductCollection) map[string]bool {
|
||
|
|
covered := map[string]bool{}
|
||
|
|
if collection == nil || manifest.ValidateSuppliedProducts(collection) != nil {
|
||
|
|
return covered
|
||
|
|
}
|
||
|
|
for _, entry := range []struct {
|
||
|
|
source string
|
||
|
|
stages []string
|
||
|
|
}{
|
||
|
|
{ArtifactTranscriptBase, []string{"transcribe", "merge"}},
|
||
|
|
{ArtifactTranscriptPolished, []string{"transcribe", "merge", "polish"}},
|
||
|
|
{ArtifactTranscriptFinal, []string{"transcribe", "merge", "polish", "normalize"}},
|
||
|
|
{ArtifactTranscriptFinalTrimmed, []string{"transcribe", "merge", "polish", "normalize", "trim"}},
|
||
|
|
} {
|
||
|
|
record, ok := collection.Products[entry.source]
|
||
|
|
if !ok || ValidateSuppliedProduct(paths, entry.source, "", record) != nil {
|
||
|
|
continue
|
||
|
|
}
|
||
|
|
for _, name := range entry.stages {
|
||
|
|
covered[name] = true
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return covered
|
||
|
|
}
|