MCPcopy
hub / github.com/daptin/daptin / serveFileWithMemoryCache

Function serveFileWithMemoryCache

server/file_serving_utils.go:119–145  ·  view source on GitHub ↗

serveFileWithMemoryCache serves a file with in-memory caching for small files

(c *gin.Context, file io.Reader, fullPath string, fileInfo os.FileInfo, config FileServingConfig)

Source from the content-addressed store, hash-verified

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 {
120 // Check client cache first
121 if checkClientCache(c, fileInfo) {
122 return nil
123 }
124
125 // Read file with size limit
126 data, err := readFileWithLimit(file, config.MaxMemoryReadSize)
127 if err != nil {
128 return err
129 }
130
131 // Set optimal headers
132 setOptimalCacheHeaders(c, fileInfo, config)
133
134 // Set content type
135 contentType := mime.TypeByExtension(filepath.Ext(fullPath))
136 if contentType == "" {
137 contentType = http.DetectContentType(data)
138 }
139 c.Header("Content-Type", contentType)
140
141 // Serve the data
142 c.Data(http.StatusOK, contentType, data)
143
144 return nil
145}
146
147// determineServingStrategy decides how to serve a file based on size and type
148func determineServingStrategy(fileInfo os.FileInfo, config FileServingConfig) string {

Callers 1

ServeFileOptimallyFunction · 0.85

Calls 3

checkClientCacheFunction · 0.85
readFileWithLimitFunction · 0.85
setOptimalCacheHeadersFunction · 0.85

Tested by

no test coverage detected