2017-06-24 11:12:15 +00:00
|
|
|
package filemanager
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"mime"
|
|
|
|
"net/http"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2017-06-25 13:24:26 +00:00
|
|
|
// assetsURL is the url where static assets are served.
|
|
|
|
const assetsURL = "/_internal"
|
2017-06-24 11:12:15 +00:00
|
|
|
|
|
|
|
// Serve provides the needed assets for the front-end
|
|
|
|
func serveAssets(w http.ResponseWriter, r *http.Request, m *FileManager) (int, error) {
|
|
|
|
// gets the filename to be used with Assets function
|
2017-06-25 14:49:24 +00:00
|
|
|
filename := strings.Replace(r.URL.Path, m.BaseURL+assetsURL, "", 1)
|
2017-06-24 11:12:15 +00:00
|
|
|
|
|
|
|
var file []byte
|
|
|
|
var err error
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case strings.HasPrefix(filename, "/css"):
|
|
|
|
filename = strings.Replace(filename, "/css/", "", 1)
|
2017-06-25 21:39:18 +00:00
|
|
|
|
|
|
|
if m.Assets.CSS != nil {
|
|
|
|
file, err = m.Assets.JS.Bytes(filename)
|
|
|
|
if err == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
file, err = m.Assets.baseCSS.Bytes(filename)
|
2017-06-24 11:12:15 +00:00
|
|
|
case strings.HasPrefix(filename, "/js"):
|
|
|
|
filename = strings.Replace(filename, "/js/", "", 1)
|
|
|
|
file, err = m.Assets.requiredJS.Bytes(filename)
|
|
|
|
case strings.HasPrefix(filename, "/vendor"):
|
2017-06-25 08:11:25 +00:00
|
|
|
if m.Assets.JS != nil {
|
|
|
|
filename = strings.Replace(filename, "/vendor/", "", 1)
|
|
|
|
file, err = m.Assets.JS.Bytes(filename)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
fallthrough
|
2017-06-24 11:12:15 +00:00
|
|
|
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
|
|
|
|
}
|