Skip to content

Commit

Permalink
fix(pgs): tests
Browse files Browse the repository at this point in the history
  • Loading branch information
neurosnap committed Dec 30, 2024
1 parent 28b4a62 commit 43a7a51
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 11 deletions.
9 changes: 4 additions & 5 deletions pgs/web_asset_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
Expand All @@ -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 {
Expand Down Expand Up @@ -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)))
}
Expand Down
10 changes: 7 additions & 3 deletions pgs/web_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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())
Expand Down
6 changes: 5 additions & 1 deletion shared/storage/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package storage
import (
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"strings"
Expand All @@ -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") == "" {
Expand Down
4 changes: 4 additions & 0 deletions shared/storage/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package storage

import (
"io"
"net/http"

sst "github.com/picosh/pobj/storage"
)
Expand All @@ -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
Expand Down
6 changes: 5 additions & 1 deletion shared/storage/minio.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package storage
import (
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"strings"
Expand All @@ -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") == "" {
Expand Down
2 changes: 1 addition & 1 deletion shared/storage/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, "\"")
Expand Down

0 comments on commit 43a7a51

Please sign in to comment.