- commit
- 43a7a51
- parent
- 28b4a62
- author
- Eric Bower
- date
- 2024-12-30 16:08:33 -0500 EST
fix(pgs): tests
6 files changed,
+26,
-11
+4,
-5
1@@ -68,9 +68,8 @@ func (h *ApiAssetHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
2 routes := calcRoutes(h.ProjectDir, h.Filepath, redirects)
3
4 var contents io.ReadCloser
5- contentType := ""
6 assetFilepath := ""
7- info := &sst.ObjectInfo{}
8+ var info *sst.ObjectInfo
9 status := http.StatusOK
10 attempts := []string{}
11 for _, fp := range routes {
12@@ -135,7 +134,7 @@ func (h *ApiAssetHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
13 attempts = append(attempts, fp.Filepath)
14 logger = logger.With("filename", fp.Filepath)
15 var c io.ReadCloser
16- c, _, err = h.Storage.ServeObject(
17+ c, info, err = h.Storage.ServeObject(
18 h.Bucket,
19 fp.Filepath,
20 h.ImgProcessOpts,
21@@ -159,8 +158,6 @@ func (h *ApiAssetHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
22 }
23 defer contents.Close()
24
25- contentType = info.Metadata.Get("content-type")
26-
27 var headers []*HeaderRule
28 headersFp, headersInfo, err := h.Storage.GetObject(h.Bucket, filepath.Join(h.ProjectDir, "_headers"))
29 if err == nil {
30@@ -195,7 +192,9 @@ func (h *ApiAssetHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
31 }
32 }
33
34+ contentType := ""
35 if info != nil {
36+ contentType = info.Metadata.Get("content-type")
37 if info.Size != 0 {
38 w.Header().Add("content-length", strconv.Itoa(int(info.Size)))
39 }
+7,
-3
1@@ -271,10 +271,14 @@ type ImageStorageMemory struct {
2 Fpath string
3 }
4
5-func (s *ImageStorageMemory) ServeObject(bucket sst.Bucket, fpath string, opts *storage.ImgProcessOpts) (io.ReadCloser, string, error) {
6+func (s *ImageStorageMemory) ServeObject(bucket sst.Bucket, fpath string, opts *storage.ImgProcessOpts) (io.ReadCloser, *sst.ObjectInfo, error) {
7 s.Opts = opts
8 s.Fpath = fpath
9- return io.NopCloser(strings.NewReader("hello world!")), "image/jpeg", nil
10+ info := sst.ObjectInfo{
11+ Metadata: make(http.Header),
12+ }
13+ info.Metadata.Set("content-type", "image/jpeg")
14+ return io.NopCloser(strings.NewReader("hello world!")), &info, nil
15 }
16
17 func TestImageManipulation(t *testing.T) {
18@@ -334,7 +338,7 @@ func TestImageManipulation(t *testing.T) {
19
20 ct := responseRecorder.Header().Get("content-type")
21 if ct != tc.contentType {
22- t.Errorf("Want status '%s', got '%s'", tc.contentType, ct)
23+ t.Errorf("Want content type '%s', got '%s'", tc.contentType, ct)
24 }
25
26 body := strings.TrimSpace(responseRecorder.Body.String())
1@@ -3,6 +3,7 @@ package storage
2 import (
3 "fmt"
4 "io"
5+ "net/http"
6 "os"
7 "path/filepath"
8 "strings"
9@@ -24,7 +25,10 @@ func NewStorageFS(dir string) (*StorageFS, error) {
10
11 func (s *StorageFS) ServeObject(bucket sst.Bucket, fpath string, opts *ImgProcessOpts) (io.ReadCloser, *sst.ObjectInfo, error) {
12 var rc io.ReadCloser
13- var info *sst.ObjectInfo
14+ info := &sst.ObjectInfo{
15+ Metadata: make(http.Header),
16+ UserMetadata: map[string]string{},
17+ }
18 var err error
19 mimeType := GetMimeType(fpath)
20 if !strings.HasPrefix(mimeType, "image/") || opts == nil || os.Getenv("IMGPROXY_URL") == "" {
1@@ -2,6 +2,7 @@ package storage
2
3 import (
4 "io"
5+ "net/http"
6
7 sst "github.com/picosh/pobj/storage"
8 )
9@@ -20,6 +21,9 @@ func NewStorageMemory(sto map[string]map[string]string) (*StorageMemory, error)
10
11 func (s *StorageMemory) ServeObject(bucket sst.Bucket, fpath string, opts *ImgProcessOpts) (io.ReadCloser, *sst.ObjectInfo, error) {
12 obj, info, err := s.GetObject(bucket, fpath)
13+ if info.Metadata == nil {
14+ info.Metadata = make(http.Header)
15+ }
16 mimeType := GetMimeType(fpath)
17 info.Metadata.Set("content-type", mimeType)
18 return obj, info, err
1@@ -3,6 +3,7 @@ package storage
2 import (
3 "fmt"
4 "io"
5+ "net/http"
6 "os"
7 "path/filepath"
8 "strings"
9@@ -24,7 +25,10 @@ func NewStorageMinio(address, user, pass string) (*StorageMinio, error) {
10
11 func (s *StorageMinio) ServeObject(bucket sst.Bucket, fpath string, opts *ImgProcessOpts) (io.ReadCloser, *sst.ObjectInfo, error) {
12 var rc io.ReadCloser
13- var info *sst.ObjectInfo
14+ info := &sst.ObjectInfo{
15+ Metadata: make(http.Header),
16+ UserMetadata: map[string]string{},
17+ }
18 var err error
19 mimeType := GetMimeType(fpath)
20 if !strings.HasPrefix(mimeType, "image/") || opts == nil || os.Getenv("IMGPROXY_URL") == "" {
1@@ -254,7 +254,7 @@ func HandleProxy(dataURL string, opts *ImgProcessOpts) (io.ReadCloser, *storage.
2 return res.Body, info, nil
3 }
4
5-// trimEtag removes quotes from the etag header, which matches the behavior of the minio-go SDK
6+// trimEtag removes quotes from the etag header, which matches the behavior of the minio-go SDK.
7 func trimEtag(etag string) string {
8 etag = strings.TrimPrefix(etag, "\"")
9 return strings.TrimSuffix(etag, "\"")