serveFileWithMemoryCache serves a file with in-memory caching for small files
(c *gin.Context, file io.Reader, fullPath string, fileInfo os.FileInfo, config FileServingConfig)
| 117 | |
| 118 | // serveFileWithMemoryCache serves a file with in-memory caching for small files |
| 119 | func 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 |
| 148 | func determineServingStrategy(fileInfo os.FileInfo, config FileServingConfig) string { |
no test coverage detected