pluginsdk/bnp/screenshots.go
Alex Dunmow 3b13dccdbb Pack codeless .bnp artifacts in the SDK
Moves the codeless build, validation and pack path from cli/internal/bnp
into a public bnp package so the CLI and the orchestrator share it.
BuildCodelessFS packs from an fs.FS and returns the artifact bytes; the
directory form calls it through os.DirFS. Output is byte-identical to the
old CLI build. ADR 0009.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-19 23:28:35 +08:00

122 lines
4.0 KiB
Go

package bnp
import (
"errors"
"fmt"
"io/fs"
"path"
"sort"
"strings"
)
// Presentation members: a root preview.png plus an optional screenshots/ dir.
// Neither ships behavior — the registry ingests them at publish into the
// plugin's managed gallery (spec 2026-07-05-registry-presentation-reviews).
// The .so-era source archives carried preview.png implicitly; the .bnp
// packers dropped it, which silently wiped previews on republish — these
// entries restore that and add the multi-screenshot surface.
const (
FilePreview = "preview.png"
DirScreenshots = "screenshots"
// maxScreenshotBytes caps one presentation image (registry gallery cap).
maxScreenshotBytes = 8 << 20
// maxScreenshotFiles caps the screenshots/ dir (registry gallery cap).
maxScreenshotFiles = 12
)
// ScreenshotExtOK reports whether a filename carries an accepted gallery
// image extension.
func ScreenshotExtOK(name string) bool {
switch strings.ToLower(path.Ext(name)) {
case ".png", ".jpg", ".jpeg", ".webp":
return true
}
return false
}
// validateScreenshotsDir checks the optional screenshots/ dir: accepted
// extensions only, ≤ maxScreenshotBytes per file, ≤ maxScreenshotFiles total,
// no subdirectories or irregular files. Returns the file count (0 when the
// dir is absent).
func validateScreenshotsDir(fsys fs.FS) (int, error) {
root := DirScreenshots
info, err := fs.Stat(fsys, root)
if errors.Is(err, fs.ErrNotExist) {
return 0, nil
}
if err != nil {
return 0, fmt.Errorf("stat %s/: %w", DirScreenshots, err)
}
if !info.IsDir() {
return 0, fmt.Errorf("%s exists but is not a directory", DirScreenshots)
}
entries, err := fs.ReadDir(fsys, root)
if err != nil {
return 0, fmt.Errorf("read %s/: %w", DirScreenshots, err)
}
count := 0
for _, e := range entries {
if e.IsDir() {
return 0, fmt.Errorf("%s/%s: subdirectories are not allowed in screenshots/", DirScreenshots, e.Name())
}
fi, err := e.Info()
if err != nil {
return 0, fmt.Errorf("%s/%s: %w", DirScreenshots, e.Name(), err)
}
if !fi.Mode().IsRegular() {
return 0, fmt.Errorf("%s/%s: not a regular file", DirScreenshots, e.Name())
}
if !ScreenshotExtOK(e.Name()) {
return 0, fmt.Errorf("%s/%s: unsupported image type (allowed: .png .jpg .jpeg .webp)", DirScreenshots, e.Name())
}
if fi.Size() > maxScreenshotBytes {
return 0, fmt.Errorf("%s/%s: %d bytes exceeds the %d MB per-image cap", DirScreenshots, e.Name(), fi.Size(), maxScreenshotBytes>>20)
}
count++
}
if count > maxScreenshotFiles {
return 0, fmt.Errorf("%s/ holds %d images; the gallery cap is %d", DirScreenshots, count, maxScreenshotFiles)
}
return count, nil
}
// PresentationEntries validates and returns pack entries for the optional
// root preview.png and screenshots/ dir. Both are optional; absence is not an
// error.
func PresentationEntries(fsys fs.FS) (entries []Entry, previewIncluded bool, screenshotCount int, err error) {
if fi, statErr := fs.Stat(fsys, FilePreview); statErr == nil {
if !fi.Mode().IsRegular() {
return nil, false, 0, fmt.Errorf("%s is not a regular file", FilePreview)
}
if fi.Size() > maxScreenshotBytes {
return nil, false, 0, fmt.Errorf("%s: %d bytes exceeds the %d MB per-image cap", FilePreview, fi.Size(), maxScreenshotBytes>>20)
}
entries = append(entries, Entry{ArtifactPath: FilePreview, Source: FilePreview})
previewIncluded = true
}
screenshotCount, err = validateScreenshotsDir(fsys)
if err != nil {
return nil, false, 0, err
}
if screenshotCount > 0 {
root := DirScreenshots
files, readErr := fs.ReadDir(fsys, root)
if readErr != nil {
return nil, false, 0, fmt.Errorf("read %s/: %w", DirScreenshots, readErr)
}
names := make([]string, 0, len(files))
for _, f := range files {
names = append(names, f.Name())
}
sort.Strings(names)
for _, name := range names {
entries = append(entries, Entry{
ArtifactPath: DirScreenshots + "/" + name,
Source: path.Join(root, name),
})
}
}
return entries, previewIncluded, screenshotCount, nil
}