Removed reinvented wheel from Static.Handle
This commit is contained in:
@@ -1,8 +1,6 @@
|
|||||||
package wrouter
|
package wrouter
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"io"
|
|
||||||
"mime"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
@@ -34,32 +32,15 @@ func (static *Static) Handle(urlPath string, w http.ResponseWriter, req *http.Re
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
// Try loading the file
|
// Look for the file
|
||||||
filepath := path.Join(static.path, subpath)
|
filepath := path.Join(static.path, subpath)
|
||||||
info, err := os.Stat(filepath)
|
info, err := os.Stat(filepath)
|
||||||
if err != nil || info.IsDir() {
|
if err != nil || info.IsDir() {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
file, err := os.Open(filepath)
|
|
||||||
if err != nil || file == nil {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
// Find and convert extension
|
// Serve it
|
||||||
ep := strings.LastIndex(filepath, ".")
|
http.ServeFile(w, req, filepath)
|
||||||
ext := ""
|
|
||||||
if ep != -1 {
|
|
||||||
ext = filepath[ep:]
|
|
||||||
}
|
|
||||||
mimeType := mime.TypeByExtension(ext)
|
|
||||||
if mimeType == "" {
|
|
||||||
mimeType = "text/plain"
|
|
||||||
}
|
|
||||||
w.Header().Set("Content-Type", mimeType)
|
|
||||||
|
|
||||||
// Submit
|
|
||||||
w.WriteHeader(200)
|
|
||||||
io.Copy(w, file)
|
|
||||||
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user