server: use real content length, use system MIME info instead of handrolling it

This commit is contained in:
mappu 2017-10-28 13:27:44 +13:00
parent 1a281095f0
commit 6abdc8d2a9

26
main.go
View File

@ -5,8 +5,9 @@ import (
"fmt"
"io/ioutil"
"log"
"mime"
"net/http"
"strings"
"path/filepath"
"code.ivysaur.me/libnmdc"
"github.com/googollee/go-socket.io"
@ -204,27 +205,8 @@ func (this *App) StaticRequestHandler(w http.ResponseWriter, r *http.Request) {
return
}
knownContentTypes := map[string]string{
".htm": "text/html",
".png": "image/png",
".ico": "image/x-icon",
// No CSS/JS since they're embedded in the HTML
}
foundMime := false
for ext, mimeType := range knownContentTypes {
if strings.HasSuffix(fileName, ext) {
w.Header().Set("Content-Type", mimeType)
foundMime = true
break
}
}
if !foundMime {
w.Header().Set("Content-Type", "application/x-octet-stream")
}
dataInfo, _ := AssetInfo(fileName)
w.Header().Set("Content-Length", fmt.Sprintf("%d", dataInfo.Size()))
w.Header().Set("Content-Type", mime.TypeByExtension(filepath.Ext(fileName)))
w.Header().Set("Content-Length", fmt.Sprintf("%d", len(data)))
w.Write(data)
}