Skip to content

Commit

Permalink
fix: Reverting geojson shortcut for s3 request count consideration
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 d098690 commit 1529a0a
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 32 deletions.
19 changes: 15 additions & 4 deletions pkg/crud/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,12 @@ func (a App) handleDir(w http.ResponseWriter, r *http.Request, request provider.
}

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

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

Expand Down Expand Up @@ -172,13 +172,24 @@ 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, items []absto.Item) {
func (a App) serveGeoJSON(w http.ResponseWriter, r *http.Request, request provider.Request, item absto.Item, items []absto.Item) {
if len(items) == 0 {
w.WriteHeader(http.StatusNoContent)
return
}

etag, ok := provider.EtagMatch(w, r, a.exifHash(r.Context(), items))
ctx := r.Context()

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

etag, ok := provider.EtagMatch(w, r, hash)
if ok {
return
}
Expand Down
55 changes: 30 additions & 25 deletions pkg/crud/get_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,6 @@ import (
)

func BenchmarkServeGeoJSON(b *testing.B) {
ctrl := gomock.NewController(b)
defer ctrl.Finish()

mockExif := mocks.NewExif(ctrl)

mockExif.EXPECT().GetExifFor(gomock.Any(), gomock.Any()).Return(exas.Exif{
Geocode: exas.Geocode{
Latitude: 1.0,
Longitude: 1.0,
},
Date: time.Date(2022, 0o2, 22, 22, 0o2, 22, 0, time.UTC),
}, nil).AnyTimes()

mockeStorage := mocks.NewStorage(ctrl)

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

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

r := httptest.NewRequest(http.MethodGet, "/", nil)
request := provider.Request{}
items := []absto.Item{
{
ID: "1234",
Expand All @@ -62,7 +38,36 @@ func BenchmarkServeGeoJSON(b *testing.B) {
},
}

ctrl := gomock.NewController(b)
defer ctrl.Finish()

mockExif := mocks.NewExif(ctrl)

mockExif.EXPECT().GetExifFor(gomock.Any(), gomock.Any()).Return(exas.Exif{
Geocode: exas.Geocode{
Latitude: 1.0,
Longitude: 1.0,
},
Date: time.Date(2022, 0o2, 22, 22, 0o2, 22, 0, time.UTC),
}, nil).AnyTimes()

mockExif.EXPECT().ListDir(gomock.Any(), gomock.Any()).Return(items, nil).AnyTimes()

instance := App{
exifApp: mockExif,
}

r := httptest.NewRequest(http.MethodGet, "/", nil)
request := provider.Request{}
item := absto.Item{
ID: "1234",
Name: "first.jpeg",
Pathname: "/first.jpeg",
Extension: ".jpeg",
IsDir: false,
}

for i := 0; i < b.N; i++ {
instance.serveGeoJSON(httptest.NewRecorder(), r, request, items)
instance.serveGeoJSON(httptest.NewRecorder(), r, request, item, items)
}
}
15 changes: 15 additions & 0 deletions pkg/mocks/exif.go

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

1 change: 1 addition & 0 deletions pkg/provider/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ 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
16 changes: 13 additions & 3 deletions pkg/thumbnail/thumbnail.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/ViBiOh/httputils/v4/pkg/httperror"
"github.com/ViBiOh/httputils/v4/pkg/logger"
prom "github.com/ViBiOh/httputils/v4/pkg/prometheus"
"github.com/ViBiOh/httputils/v4/pkg/query"
"github.com/ViBiOh/httputils/v4/pkg/request"
"github.com/ViBiOh/httputils/v4/pkg/sha"
"github.com/prometheus/client_golang/prometheus"
Expand Down Expand Up @@ -194,16 +195,25 @@ func (a App) Serve(w http.ResponseWriter, r *http.Request, item absto.Item) {
http.ServeContent(w, r, path.Base(thumbnailInfo.Pathname), item.Date, reader)
}

// List return all thumbnail in a base64 form
func (a App) List(w http.ResponseWriter, r *http.Request, items []absto.Item) {
// List return all thumbnails in a base64 form
func (a App) List(w http.ResponseWriter, r *http.Request, request provider.Request, item absto.Item, items []absto.Item) {
if len(items) == 0 {
w.WriteHeader(http.StatusNoContent)
return
}

ctx := r.Context()
var hash string

etag, ok := provider.EtagMatch(w, r, a.thumbnailHash(ctx, items))
if query.GetBool(r, "search") {
hash = a.thumbnailHash(ctx, items)
} else if thumbnails, err := a.ListDir(ctx, item); err != nil {
logger.WithField("item", item.Pathname).Error("unable to list thumbnails: %s", err)
} else {
hash = sha.New(thumbnails)
}

etag, ok := provider.EtagMatch(w, r, hash)
if ok {
return
}
Expand Down

0 comments on commit 1529a0a

Please sign in to comment.