Bound external result file reads
This commit is contained in:
@@ -4,12 +4,17 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"math"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/fileops"
|
||||
)
|
||||
|
||||
const (
|
||||
// MaxSessionBoundsFileBytes bounds one Scriptorium bounds JSON result.
|
||||
MaxSessionBoundsFileBytes int64 = 1 * 1024 * 1024
|
||||
// MaxTranscriptFileBytes bounds a transcript JSON handoff used for bounds validation.
|
||||
MaxTranscriptFileBytes int64 = 64 * 1024 * 1024
|
||||
// BoundsTrimActionTrim keeps only a selected segment range.
|
||||
BoundsTrimActionTrim = "trim"
|
||||
// BoundsTrimActionNone indicates no trimming should be applied.
|
||||
@@ -36,9 +41,9 @@ type SessionBounds struct {
|
||||
|
||||
// ParseSessionBoundsFile reads and parses one Scriptorium bounds output JSON file.
|
||||
func ParseSessionBoundsFile(path string) (SessionBounds, error) {
|
||||
data, err := os.ReadFile(path)
|
||||
data, err := readContractResult(path, MaxSessionBoundsFileBytes, "scriptorium bounds result")
|
||||
if err != nil {
|
||||
return SessionBounds{}, fmt.Errorf("read bounds file: %w", err)
|
||||
return SessionBounds{}, err
|
||||
}
|
||||
var bounds SessionBounds
|
||||
if err := json.Unmarshal(data, &bounds); err != nil {
|
||||
@@ -119,9 +124,9 @@ func normalizeBoundsTrimAction(raw string) (string, error) {
|
||||
}
|
||||
|
||||
func loadTranscriptSegmentIDs(path string) (map[int]struct{}, error) {
|
||||
data, err := os.ReadFile(path)
|
||||
data, err := readContractResult(path, MaxTranscriptFileBytes, "transcript result")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("read transcript file: %w", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var payload map[string]any
|
||||
@@ -158,6 +163,14 @@ func loadTranscriptSegmentIDs(path string) (map[int]struct{}, error) {
|
||||
return ids, nil
|
||||
}
|
||||
|
||||
func readContractResult(path string, limit int64, category string) ([]byte, error) {
|
||||
data, err := fileops.ReadRegularFile(path, limit)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%s exceeds or cannot be read within %d-byte limit: %w", category, limit, err)
|
||||
}
|
||||
return data, nil
|
||||
}
|
||||
|
||||
func parseJSONSegmentID(v any) (int, error) {
|
||||
n, ok := v.(float64)
|
||||
if !ok {
|
||||
|
||||
Reference in New Issue
Block a user