diff --git a/server/files.go b/server/files.go index 31f504a..3df184a 100644 --- a/server/files.go +++ b/server/files.go @@ -4,9 +4,11 @@ import ( "io" "io/ioutil" "local/dndex/config" + "local/simpleserve/simpleserve" "net/http" "os" "path" + "strings" "github.com/google/uuid" ) @@ -95,6 +97,8 @@ func (rest *REST) filesDelete(w http.ResponseWriter, r *http.Request) { } func (rest *REST) filesGet(w http.ResponseWriter, r *http.Request) { + simpleserve.SetContentTypeIfMedia(w, r) + r.URL.Path = strings.TrimSuffix(r.URL.Path, path.Ext(r.URL.Path)) localPath := rest.filesPath(r) if stat, err := os.Stat(localPath); os.IsNotExist(err) { rest.respNotFound(w) diff --git a/server/files_test.go b/server/files_test.go index 8be910a..c5f628a 100644 --- a/server/files_test.go +++ b/server/files_test.go @@ -2,6 +2,7 @@ package server import ( "bytes" + "fmt" "net/http" "net/http/httptest" "strings" @@ -12,14 +13,17 @@ import ( func TestFiles(t *testing.T) { cases := map[string]func(*testing.T, *REST, func(*http.Request)){ - "create-get": func(t *testing.T, rest *REST, scope func(r *http.Request)) { + "create-get w/ content type": func(t *testing.T, rest *REST, scope func(r *http.Request)) { content := uuid.New().String() w := testFilesPost(t, rest, scope, content) if w.Code != http.StatusOK { t.Fatal(w.Code, string(w.Body.Bytes())) } - s := string(w.Body.Bytes()) + s := fmt.Sprintf("%s.jpg", w.Body.Bytes()) w = testFilesGet(t, rest, s, scope) + if w.Header().Get("Content-Type") != "image/jpeg" { + t.Fatal(w.Header()) + } if w.Code != http.StatusOK { t.Fatal(w.Code, string(w.Body.Bytes())) }