2016-06-25 20:57:10 +00:00
|
|
|
package assets
|
|
|
|
|
|
|
|
import (
|
|
|
|
"mime"
|
|
|
|
"net/http"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
|
2016-06-28 09:24:02 +00:00
|
|
|
"github.com/hacdias/caddy-filemanager/config"
|
2016-06-25 20:57:10 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// BaseURL is the url of the assets
|
2016-08-22 11:24:26 +00:00
|
|
|
const BaseURL = "/_filemanagerinternal"
|
2016-06-25 20:57:10 +00:00
|
|
|
|
2016-06-28 09:24:02 +00:00
|
|
|
// Serve provides the needed assets for the front-end
|
|
|
|
func Serve(w http.ResponseWriter, r *http.Request, c *config.Config) (int, error) {
|
2016-06-25 20:57:10 +00:00
|
|
|
// gets the filename to be used with Assets function
|
|
|
|
filename := strings.Replace(r.URL.Path, c.BaseURL+BaseURL, "public", 1)
|
|
|
|
file, err := Asset(filename)
|
|
|
|
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
|
|
|
|
}
|