package httpapi

// uploads.go — Hilfsmittel für sicheres Serving von Uploads (N5, N6).

import (
	"net/http"
	"os"
)

// neuteredFileSystem wraps an http.FileSystem and disables directory listing (N5).
// When Open() returns a directory, it returns an error as if the file was not found.
type neuteredFileSystem struct {
	fs http.FileSystem
}

func (nfs neuteredFileSystem) Open(path string) (http.File, error) {
	f, err := nfs.fs.Open(path)
	if err != nil {
		return nil, err
	}
	s, err := f.Stat()
	if err != nil {
		f.Close() //nolint:errcheck
		return nil, err
	}
	if s.IsDir() {
		// Return os.ErrNotExist so http.FileServer responds with 404.
		f.Close() //nolint:errcheck
		return nil, os.ErrNotExist
	}
	return f, nil
}
