mirror of
https://github.com/filebrowser/filebrowser.git
synced 2025-05-09 03:32:56 +00:00

Former-commit-id: 10a2dcf7a277693d7f23419dde16715b8b3ab67d [formerly c0645b792ed36d75b76442016596d3086d23236a] [formerly a605776b898544c9034b05b240249afc515d8c91 [formerly 6bce6fb5849b4b7f1816d8e83a80dbc575aa420c]] Former-commit-id: deb481fbc32d90688fa9399f1f63f002d1008087 [formerly f7f130563c33d12ef0ff416a572d97f1b70101fb] Former-commit-id: f51b7316471d625380cd711d1c783ca9478a77dd
47 lines
1.2 KiB
Go
47 lines
1.2 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(ctx *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 = ctx.FileManager.assets.css.Bytes(filename)
|
|
case strings.HasPrefix(filename, "/js"):
|
|
filename = strings.Replace(filename, "/js/", "", 1)
|
|
file, err = ctx.FileManager.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
|
|
}
|