Former-commit-id: 9c36dea485ddd16a90f0a615e20baddaee6b9cde
This commit is contained in:
Henrique Dias
2017-06-24 12:12:15 +01:00
parent b0e81a80a0
commit 850c4a10e1
23 changed files with 2408 additions and 0 deletions

49
http_assets.go Normal file
View File

@@ -0,0 +1,49 @@
package filemanager
import (
"errors"
"mime"
"net/http"
"path/filepath"
"strings"
)
// AssetsURL is the url of the assets
const AssetsURL = "/_filemanagerinternal"
// 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
filename := strings.Replace(r.URL.Path, m.BaseURL+AssetsURL, "", 1)
var file []byte
var err error
switch {
case strings.HasPrefix(filename, "/css"):
filename = strings.Replace(filename, "/css/", "", 1)
file, err = m.Assets.CSS.Bytes(filename)
case strings.HasPrefix(filename, "/js"):
filename = strings.Replace(filename, "/js/", "", 1)
file, err = m.Assets.requiredJS.Bytes(filename)
case strings.HasPrefix(filename, "/vendor"):
filename = strings.Replace(filename, "/vendor/", "", 1)
file, err = m.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
}