Skip to content

Commit

Permalink
fix: Fixing computing of etag for geojson
Browse files Browse the repository at this point in the history
Signed-off-by: Vincent Boutour <[email protected]>
  • Loading branch information
ViBiOh committed May 20, 2022
1 parent 9b85f26 commit 49f3702
Show file tree
Hide file tree
Showing 6 changed files with 10 additions and 43 deletions.
16 changes: 3 additions & 13 deletions pkg/crud/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func (a App) handleDir(w http.ResponseWriter, r *http.Request, request provider.
}

if query.GetBool(r, "geojson") {
a.serveGeoJSON(w, r, request, item, items)
a.serveGeoJSON(w, r, request, items)
return renderer.Page{}, nil
}

Expand Down Expand Up @@ -172,23 +172,13 @@ func (a App) listFiles(r *http.Request, request provider.Request, item absto.Ite
return items, err
}

func (a App) serveGeoJSON(w http.ResponseWriter, r *http.Request, request provider.Request, item absto.Item, items []absto.Item) {
func (a App) serveGeoJSON(w http.ResponseWriter, r *http.Request, request provider.Request, items []absto.Item) {
if len(items) == 0 {
w.WriteHeader(http.StatusNoContent)
return
}

ctx := r.Context()

var hash string
if query.GetBool(r, "search") || request.IsStory() {
hash = a.exifHash(ctx, items)
} else if exifs, err := a.exifApp.ListDir(ctx, item); err == nil {
hash = sha.New(exifs)
} else if err != nil {
logger.Error("unable to list geojson directory: %s", err)
}

hash := a.exifHash(r.Context(), items)
etag, ok := provider.EtagMatch(w, r, hash)
if ok {
return
Expand Down
17 changes: 6 additions & 11 deletions pkg/crud/get_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,13 @@ func BenchmarkServeGeoJSON(b *testing.B) {
Date: time.Date(2022, 0o2, 22, 22, 0o2, 22, 0, time.UTC),
}, nil).AnyTimes()

mockExif.EXPECT().ListDir(gomock.Any(), gomock.Any()).Return(nil, nil).AnyTimes()
mockeStorage := mocks.NewStorage(ctrl)

mockeStorage.EXPECT().Info(gomock.Any(), gomock.Any()).Return(absto.Item{}, nil).AnyTimes()

instance := App{
exifApp: mockExif,
exifApp: mockExif,
storageApp: mockeStorage,
}

r := httptest.NewRequest(http.MethodGet, "/", nil)
Expand Down Expand Up @@ -59,15 +62,7 @@ func BenchmarkServeGeoJSON(b *testing.B) {
},
}

item := absto.Item{
ID: "1234",
Name: "/",
Pathname: "/",
Extension: "",
IsDir: true,
}

for i := 0; i < b.N; i++ {
instance.serveGeoJSON(httptest.NewRecorder(), r, request, item, items)
instance.serveGeoJSON(httptest.NewRecorder(), r, request, items)
}
}
2 changes: 1 addition & 1 deletion pkg/exif/exif.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func (a App) ListDir(ctx context.Context, item absto.Item) ([]absto.Item, error)
return nil, nil
}

exifs, err := a.listStorageApp.List(ctx, Path(item))
exifs, err := a.listStorageApp.List(ctx, provider.MetadataDirectory(item))
if err != nil && !absto.IsNotExist(err) {
return exifs, err
}
Expand Down
15 changes: 0 additions & 15 deletions pkg/mocks/exif.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion pkg/provider/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ type WebhookManager interface {
// ExifManager description
//go:generate mockgen -destination ../mocks/exif.go -mock_names ExifManager=Exif -package mocks github.com/ViBiOh/fibr/pkg/provider ExifManager
type ExifManager interface {
ListDir(ctx context.Context, item absto.Item) ([]absto.Item, error)
GetAggregateFor(ctx context.Context, item absto.Item) (Aggregate, error)
GetExifFor(ctx context.Context, item absto.Item) (exas.Exif, error)
SaveExifFor(ctx context.Context, item absto.Item, exif exas.Exif) error
Expand Down
2 changes: 0 additions & 2 deletions pkg/provider/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,5 @@ func EtagMatch(w http.ResponseWriter, r *http.Request, hash string) (etag string
match = true
}

logger.Info("Request is `%s`, Hash is `%s`, `If-None-Match` is `%s`, Match ? %t", r.URL.String(), hash, r.Header.Get("If-None-Match"), match)

return
}

0 comments on commit 49f3702

Please sign in to comment.