optimize: Limit file load size and clean up server logging

This commit is contained in:
dragon
2026-01-29 13:50:11 +08:00
parent 4fdb77c833
commit a60f123bee
2 changed files with 22 additions and 4 deletions

View File

@@ -15,7 +15,9 @@ type PageVFS struct {
commitID string
}
// todo: 限制最大文件加载大小
// MaxFileLoadSize limits the maximum size of file loaded into memory (10MB)
const MaxFileLoadSize = 10 * 1024 * 1024
func NewPageVFS(
backend Backend,
org string,
@@ -69,7 +71,19 @@ func (p *PageVFS) Read(ctx context.Context, path string) ([]byte, error) {
return nil, err
}
defer open.Close()
return io.ReadAll(open)
// Use LimitReader to prevent reading too much data
limitReader := io.LimitReader(open, MaxFileLoadSize+1)
data, err := io.ReadAll(limitReader)
if err != nil {
return nil, err
}
if len(data) > MaxFileLoadSize {
return nil, &os.PathError{Op: "read", Path: path, Err: os.ErrInvalid} // Or a specific "file too large" error
}
return data, nil
}
func (p *PageVFS) ReadString(ctx context.Context, path string) (string, error) {