MCPcopy
hub / github.com/daptin/daptin / readFileWithLimit

Function readFileWithLimit

server/file_serving_utils.go:94–116  ·  view source on GitHub ↗

readFileWithLimit reads a file with size limit and buffer pool for memory efficiency

(file io.Reader, maxSize int64)

Source from the content-addressed store, hash-verified

92
93// readFileWithLimit reads a file with size limit and buffer pool for memory efficiency
94func readFileWithLimit(file io.Reader, maxSize int64) ([]byte, error) {
95 // Get buffer from pool
96 buf := bufferPool.Get().([]byte)
97 defer bufferPool.Put(buf[:0]) // Reset and return to pool
98
99 // Read with limit
100 limitedReader := io.LimitReader(file, maxSize+1)
101 data, err := io.ReadAll(limitedReader)
102 if err != nil {
103 return nil, err
104 }
105
106 // Check if file was too large
107 if int64(len(data)) > maxSize {
108 return nil, fmt.Errorf("file too large: %d bytes > %d limit", len(data), maxSize)
109 }
110
111 // Return a copy since we're returning the buffer to the pool
112 result := make([]byte, len(data))
113 copy(result, data)
114
115 return result, nil
116}
117
118// serveFileWithMemoryCache serves a file with in-memory caching for small files
119func serveFileWithMemoryCache(c *gin.Context, file io.Reader, fullPath string, fileInfo os.FileInfo, config FileServingConfig) error {

Callers 4

AssetRouteHandlerFunction · 0.85
SetupNoRouteRouterFunction · 0.85
CreateFaviconEndpointFunction · 0.85
serveFileWithMemoryCacheFunction · 0.85

Calls 3

makeFunction · 0.85
ReadAllMethod · 0.80
GetMethod · 0.65

Tested by

no test coverage detected