From 4d8f75f7c91fc1f03a558269741260e0c7b1f25a Mon Sep 17 00:00:00 2001 From: breel Date: Fri, 28 Aug 2020 16:07:24 -0600 Subject: [PATCH] Accept ext on entity files to set content type in resp --- server/files.go | 4 ++++ server/files_test.go | 8 ++++++-- 2 files changed, 10 insertions(+), 2 deletions(-) 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())) }