optimize: Limit file load size and clean up server logging
This commit is contained in:
@@ -15,7 +15,9 @@ type PageVFS struct {
|
|||||||
commitID string
|
commitID string
|
||||||
}
|
}
|
||||||
|
|
||||||
// todo: 限制最大文件加载大小
|
// MaxFileLoadSize limits the maximum size of file loaded into memory (10MB)
|
||||||
|
const MaxFileLoadSize = 10 * 1024 * 1024
|
||||||
|
|
||||||
func NewPageVFS(
|
func NewPageVFS(
|
||||||
backend Backend,
|
backend Backend,
|
||||||
org string,
|
org string,
|
||||||
@@ -69,7 +71,19 @@ func (p *PageVFS) Read(ctx context.Context, path string) ([]byte, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
defer open.Close()
|
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) {
|
func (p *PageVFS) ReadString(ctx context.Context, path string) (string, error) {
|
||||||
|
|||||||
@@ -218,6 +218,7 @@ func (s *Server) Serve(writer *utils.WrittenResponseWriter, request *http.Reques
|
|||||||
if !ok {
|
if !ok {
|
||||||
value, err = glob.Compile(filter.Path)
|
value, err = glob.Compile(filter.Path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
zap.L().Warn("invalid glob pattern", zap.String("pattern", filter.Path), zap.Error(err))
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
s.globCache.Add(filter.Path, value)
|
s.globCache.Add(filter.Path, value)
|
||||||
@@ -239,9 +240,12 @@ func (s *Server) Serve(writer *utils.WrittenResponseWriter, request *http.Reques
|
|||||||
slices.Reverse(activeFiltersCall)
|
slices.Reverse(activeFiltersCall)
|
||||||
slices.Reverse(activeFilters)
|
slices.Reverse(activeFilters)
|
||||||
|
|
||||||
|
// Build the visual call stack for logging (e.g., A -> B -> C -> B -> A)
|
||||||
l := len(filtersRoute)
|
l := len(filtersRoute)
|
||||||
for i := l - 2; i >= 0; i-- {
|
if l > 1 {
|
||||||
filtersRoute = append(filtersRoute, filtersRoute[i])
|
for i := l - 2; i >= 0; i-- {
|
||||||
|
filtersRoute = append(filtersRoute, filtersRoute[i])
|
||||||
|
}
|
||||||
}
|
}
|
||||||
zap.L().Debug("active filters", zap.String("filters", strings.Join(filtersRoute, " -> ")))
|
zap.L().Debug("active filters", zap.String("filters", strings.Join(filtersRoute, " -> ")))
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user