mirror of
https://github.com/filebrowser/filebrowser.git
synced 2025-05-08 19:22:57 +00:00

Former-commit-id: f0de208a4ae4cd0df9056443fe7a6a823882eca2 [formerly 3c65bde6f2dede145eca8f0d7c10a4d8124a01ef] [formerly 97100fe7d36d445babbc13a669d9c75577cb02d7 [formerly 6e04c97bb9d3e689e583f347588740d2d214e62b]] Former-commit-id: 30f9afb795307f67cd9bccd2f35742a21ee69fde [formerly bc8a04744ca91ad2f951f0888cc11b023d6a067a] Former-commit-id: 7397ffff5d09a3a56b29f7119d696ab4a4507893
47 lines
1.1 KiB
Go
47 lines
1.1 KiB
Go
package filemanager
|
|
|
|
import (
|
|
"errors"
|
|
"mime"
|
|
"net/http"
|
|
"path/filepath"
|
|
"strings"
|
|
)
|
|
|
|
// assetsURL is the url where static assets are served.
|
|
const assetsURL = "/_internal"
|
|
|
|
// Serve provides the needed assets for the front-end
|
|
func serveAssets(c *requestContext, w http.ResponseWriter, r *http.Request) (int, error) {
|
|
// gets the filename to be used with Assets function
|
|
filename := strings.TrimPrefix(r.URL.Path, assetsURL)
|
|
|
|
var file []byte
|
|
var err error
|
|
|
|
switch {
|
|
case strings.HasPrefix(filename, "/css"):
|
|
filename = strings.Replace(filename, "/css/", "", 1)
|
|
file, err = c.fm.assets.css.Bytes(filename)
|
|
case strings.HasPrefix(filename, "/js"):
|
|
filename = strings.Replace(filename, "/js/", "", 1)
|
|
file, err = c.fm.assets.js.Bytes(filename)
|
|
default:
|
|
err = errors.New("not found")
|
|
}
|
|
|
|
if err != nil {
|
|
return http.StatusNotFound, nil
|
|
}
|
|
|
|
// Get the file extension and its mimetype
|
|
extension := filepath.Ext(filename)
|
|
mediatype := mime.TypeByExtension(extension)
|
|
|
|
// Write the header with the Content-Type and write the file
|
|
// content to the buffer
|
|
w.Header().Set("Content-Type", mediatype)
|
|
w.Write(file)
|
|
return 200, nil
|
|
}
|