From 43a7a51e72316a4020aeafe0ebfb2d9bddde8378 Mon Sep 17 00:00:00 2001 From: Eric Bower Date: Mon, 30 Dec 2024 16:08:33 -0500 Subject: [PATCH] fix(pgs): tests --- pgs/web_asset_handler.go | 9 ++++----- pgs/web_test.go | 10 +++++++--- shared/storage/fs.go | 6 +++++- shared/storage/memory.go | 4 ++++ shared/storage/minio.go | 6 +++++- shared/storage/proxy.go | 2 +- 6 files changed, 26 insertions(+), 11 deletions(-) diff --git a/pgs/web_asset_handler.go b/pgs/web_asset_handler.go index 71484ae0..88c33ad8 100644 --- a/pgs/web_asset_handler.go +++ b/pgs/web_asset_handler.go @@ -68,9 +68,8 @@ func (h *ApiAssetHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { routes := calcRoutes(h.ProjectDir, h.Filepath, redirects) var contents io.ReadCloser - contentType := "" assetFilepath := "" - info := &sst.ObjectInfo{} + var info *sst.ObjectInfo status := http.StatusOK attempts := []string{} for _, fp := range routes { @@ -135,7 +134,7 @@ func (h *ApiAssetHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { attempts = append(attempts, fp.Filepath) logger = logger.With("filename", fp.Filepath) var c io.ReadCloser - c, _, err = h.Storage.ServeObject( + c, info, err = h.Storage.ServeObject( h.Bucket, fp.Filepath, h.ImgProcessOpts, @@ -159,8 +158,6 @@ func (h *ApiAssetHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { } defer contents.Close() - contentType = info.Metadata.Get("content-type") - var headers []*HeaderRule headersFp, headersInfo, err := h.Storage.GetObject(h.Bucket, filepath.Join(h.ProjectDir, "_headers")) if err == nil { @@ -195,7 +192,9 @@ func (h *ApiAssetHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { } } + contentType := "" if info != nil { + contentType = info.Metadata.Get("content-type") if info.Size != 0 { w.Header().Add("content-length", strconv.Itoa(int(info.Size))) } diff --git a/pgs/web_test.go b/pgs/web_test.go index 9445455d..9d817727 100644 --- a/pgs/web_test.go +++ b/pgs/web_test.go @@ -271,10 +271,14 @@ type ImageStorageMemory struct { Fpath string } -func (s *ImageStorageMemory) ServeObject(bucket sst.Bucket, fpath string, opts *storage.ImgProcessOpts) (io.ReadCloser, string, error) { +func (s *ImageStorageMemory) ServeObject(bucket sst.Bucket, fpath string, opts *storage.ImgProcessOpts) (io.ReadCloser, *sst.ObjectInfo, error) { s.Opts = opts s.Fpath = fpath - return io.NopCloser(strings.NewReader("hello world!")), "image/jpeg", nil + info := sst.ObjectInfo{ + Metadata: make(http.Header), + } + info.Metadata.Set("content-type", "image/jpeg") + return io.NopCloser(strings.NewReader("hello world!")), &info, nil } func TestImageManipulation(t *testing.T) { @@ -334,7 +338,7 @@ func TestImageManipulation(t *testing.T) { ct := responseRecorder.Header().Get("content-type") if ct != tc.contentType { - t.Errorf("Want status '%s', got '%s'", tc.contentType, ct) + t.Errorf("Want content type '%s', got '%s'", tc.contentType, ct) } body := strings.TrimSpace(responseRecorder.Body.String()) diff --git a/shared/storage/fs.go b/shared/storage/fs.go index f6b148bd..1074bbd5 100644 --- a/shared/storage/fs.go +++ b/shared/storage/fs.go @@ -3,6 +3,7 @@ package storage import ( "fmt" "io" + "net/http" "os" "path/filepath" "strings" @@ -24,7 +25,10 @@ func NewStorageFS(dir string) (*StorageFS, error) { func (s *StorageFS) ServeObject(bucket sst.Bucket, fpath string, opts *ImgProcessOpts) (io.ReadCloser, *sst.ObjectInfo, error) { var rc io.ReadCloser - var info *sst.ObjectInfo + info := &sst.ObjectInfo{ + Metadata: make(http.Header), + UserMetadata: map[string]string{}, + } var err error mimeType := GetMimeType(fpath) if !strings.HasPrefix(mimeType, "image/") || opts == nil || os.Getenv("IMGPROXY_URL") == "" { diff --git a/shared/storage/memory.go b/shared/storage/memory.go index 9393350c..a8cac896 100644 --- a/shared/storage/memory.go +++ b/shared/storage/memory.go @@ -2,6 +2,7 @@ package storage import ( "io" + "net/http" sst "github.com/picosh/pobj/storage" ) @@ -20,6 +21,9 @@ func NewStorageMemory(sto map[string]map[string]string) (*StorageMemory, error) func (s *StorageMemory) ServeObject(bucket sst.Bucket, fpath string, opts *ImgProcessOpts) (io.ReadCloser, *sst.ObjectInfo, error) { obj, info, err := s.GetObject(bucket, fpath) + if info.Metadata == nil { + info.Metadata = make(http.Header) + } mimeType := GetMimeType(fpath) info.Metadata.Set("content-type", mimeType) return obj, info, err diff --git a/shared/storage/minio.go b/shared/storage/minio.go index 371bcb8f..3be09a38 100644 --- a/shared/storage/minio.go +++ b/shared/storage/minio.go @@ -3,6 +3,7 @@ package storage import ( "fmt" "io" + "net/http" "os" "path/filepath" "strings" @@ -24,7 +25,10 @@ func NewStorageMinio(address, user, pass string) (*StorageMinio, error) { func (s *StorageMinio) ServeObject(bucket sst.Bucket, fpath string, opts *ImgProcessOpts) (io.ReadCloser, *sst.ObjectInfo, error) { var rc io.ReadCloser - var info *sst.ObjectInfo + info := &sst.ObjectInfo{ + Metadata: make(http.Header), + UserMetadata: map[string]string{}, + } var err error mimeType := GetMimeType(fpath) if !strings.HasPrefix(mimeType, "image/") || opts == nil || os.Getenv("IMGPROXY_URL") == "" { diff --git a/shared/storage/proxy.go b/shared/storage/proxy.go index 0bda10cb..6046c5a5 100644 --- a/shared/storage/proxy.go +++ b/shared/storage/proxy.go @@ -254,7 +254,7 @@ func HandleProxy(dataURL string, opts *ImgProcessOpts) (io.ReadCloser, *storage. return res.Body, info, nil } -// trimEtag removes quotes from the etag header, which matches the behavior of the minio-go SDK +// trimEtag removes quotes from the etag header, which matches the behavior of the minio-go SDK. func trimEtag(etag string) string { etag = strings.TrimPrefix(etag, "\"") return strings.TrimSuffix(etag, "\"")