MCPcopy
hub / github.com/wavetermdev/waveterm / serveFileDirectly

Function serveFileDirectly

tsunami/engine/serverhandlers.go:522–550  ·  view source on GitHub ↗

serveFileDirectly serves a file directly from an embed.FS to avoid redirect loops when serving directory paths that end with "/"

(w http.ResponseWriter, r *http.Request, embeddedFS fs.FS, requestPath, fileName string)

Source from the content-addressed store, hash-verified

520// serveFileDirectly serves a file directly from an embed.FS to avoid redirect loops
521// when serving directory paths that end with "/"
522func serveFileDirectly(w http.ResponseWriter, r *http.Request, embeddedFS fs.FS, requestPath, fileName string) bool {
523 if !strings.HasSuffix(requestPath, "/") {
524 return false
525 }
526
527 // Try to serve the specified file from that directory
528 var filePath string
529 if requestPath == "/" {
530 filePath = fileName
531 } else {
532 filePath = strings.TrimPrefix(requestPath, "/") + fileName
533 }
534
535 file, err := embeddedFS.Open(filePath)
536 if err != nil {
537 return false
538 }
539 defer file.Close()
540
541 // Get file info for modification time
542 fileInfo, err := file.Stat()
543 if err != nil {
544 return false
545 }
546
547 // Serve the file directly with proper mod time
548 http.ServeContent(w, r, fileName, fileInfo.ModTime(), file.(io.ReadSeeker))
549 return true
550}
551
552func (h *httpHandlers) handleStaticFiles(embeddedFS fs.FS) http.HandlerFunc {
553 fileServer := http.FileServer(http.FS(embeddedFS))

Callers 2

handleStaticFilesMethod · 0.85
handleStaticPathFilesMethod · 0.85

Calls 4

OpenMethod · 0.80
StatMethod · 0.80
ModTimeMethod · 0.80
CloseMethod · 0.65

Tested by

no test coverage detected