Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3c5f23d3ac | ||
|
|
c6109e23af | ||
|
|
6ae09962ad |
40
main.go
40
main.go
@@ -204,16 +204,50 @@ func toRealPath(p string) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func setContentTypeIfMedia(w http.ResponseWriter, r *http.Request) {
|
func setContentTypeIfMedia(w http.ResponseWriter, r *http.Request) {
|
||||||
switch path.Ext(r.URL.Path) {
|
ext := strings.ToLower(path.Ext(r.URL.Path))
|
||||||
|
if i := strings.LastIndex(ext, "."); i != -1 {
|
||||||
|
ext = ext[i:]
|
||||||
|
}
|
||||||
|
switch ext {
|
||||||
case ".mp4":
|
case ".mp4":
|
||||||
w.Header().Set("Content-Type", "video/mp4")
|
w.Header().Set("Content-Type", "video/mp4")
|
||||||
case ".webm":
|
|
||||||
w.Header().Set("Content-Type", "video/webm")
|
|
||||||
case ".mkv":
|
case ".mkv":
|
||||||
w.Header().Set("Content-Type", "video/x-matroska")
|
w.Header().Set("Content-Type", "video/x-matroska")
|
||||||
case ".mp3":
|
case ".mp3":
|
||||||
w.Header().Set("Content-Type", "audio/mpeg3")
|
w.Header().Set("Content-Type", "audio/mpeg3")
|
||||||
case ".epub", ".mobi":
|
case ".epub", ".mobi":
|
||||||
w.Header().Set("Content-Disposition", "attachment")
|
w.Header().Set("Content-Disposition", "attachment")
|
||||||
|
case ".jpg", ".jpeg":
|
||||||
|
w.Header().Set("Content-Type", "image/jpeg")
|
||||||
|
case ".gif":
|
||||||
|
w.Header().Set("Content-Type", "image/gif")
|
||||||
|
case ".png":
|
||||||
|
w.Header().Set("Content-Type", "image/png")
|
||||||
|
case ".ico":
|
||||||
|
w.Header().Set("Content-Type", "image/x-icon")
|
||||||
|
case ".svg":
|
||||||
|
w.Header().Set("Content-Type", "image/svg+xml")
|
||||||
|
case ".css":
|
||||||
|
w.Header().Set("Content-Type", "text/css")
|
||||||
|
case ".js":
|
||||||
|
w.Header().Set("Content-Type", "text/javascript")
|
||||||
|
case ".json":
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
case ".html", ".htm":
|
||||||
|
w.Header().Set("Content-Type", "text/html")
|
||||||
|
case ".pdf":
|
||||||
|
w.Header().Set("Content-Type", "application/pdf")
|
||||||
|
case ".webm":
|
||||||
|
w.Header().Set("Content-Type", "video/webm")
|
||||||
|
case ".weba":
|
||||||
|
w.Header().Set("Content-Type", "audio/webm")
|
||||||
|
case ".webp":
|
||||||
|
w.Header().Set("Content-Type", "image/webp")
|
||||||
|
case ".zip":
|
||||||
|
w.Header().Set("Content-Type", "application/zip")
|
||||||
|
case ".7z":
|
||||||
|
w.Header().Set("Content-Type", "application/x-7z-compressed")
|
||||||
|
case ".tar":
|
||||||
|
w.Header().Set("Content-Type", "application/x-tar")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
34
main_test.go
Normal file
34
main_test.go
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
|
"net/url"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestSetContentType(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
cases := map[string]struct {
|
||||||
|
path string
|
||||||
|
want string
|
||||||
|
}{
|
||||||
|
"css with multi .": {
|
||||||
|
path: "/static/css/main.2145ce41.chunk.css",
|
||||||
|
want: "text/css",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for name, d := range cases {
|
||||||
|
c := d
|
||||||
|
t.Run(name, func(t *testing.T) {
|
||||||
|
r := &http.Request{URL: &url.URL{Path: c.path}}
|
||||||
|
w := httptest.NewRecorder()
|
||||||
|
setContentTypeIfMedia(w, r)
|
||||||
|
if ct := w.Header().Get("Content-Type"); ct != c.want {
|
||||||
|
t.Errorf("wrong content type: want %q, got %q", c.want, ct)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user