Accept ext on entity files to set content type in resp

master
breel 2020-08-28 16:07:24 -06:00
parent 3a3ee3912d
commit 4d8f75f7c9
2 changed files with 10 additions and 2 deletions

View File

@ -4,9 +4,11 @@ import (
"io" "io"
"io/ioutil" "io/ioutil"
"local/dndex/config" "local/dndex/config"
"local/simpleserve/simpleserve"
"net/http" "net/http"
"os" "os"
"path" "path"
"strings"
"github.com/google/uuid" "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) { 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) localPath := rest.filesPath(r)
if stat, err := os.Stat(localPath); os.IsNotExist(err) { if stat, err := os.Stat(localPath); os.IsNotExist(err) {
rest.respNotFound(w) rest.respNotFound(w)

View File

@ -2,6 +2,7 @@ package server
import ( import (
"bytes" "bytes"
"fmt"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"strings" "strings"
@ -12,14 +13,17 @@ import (
func TestFiles(t *testing.T) { func TestFiles(t *testing.T) {
cases := map[string]func(*testing.T, *REST, func(*http.Request)){ 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() content := uuid.New().String()
w := testFilesPost(t, rest, scope, content) w := testFilesPost(t, rest, scope, content)
if w.Code != http.StatusOK { if w.Code != http.StatusOK {
t.Fatal(w.Code, string(w.Body.Bytes())) 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) w = testFilesGet(t, rest, s, scope)
if w.Header().Get("Content-Type") != "image/jpeg" {
t.Fatal(w.Header())
}
if w.Code != http.StatusOK { if w.Code != http.StatusOK {
t.Fatal(w.Code, string(w.Body.Bytes())) t.Fatal(w.Code, string(w.Body.Bytes()))
} }