Skip to content

Commit

Permalink
feat: lower path parameter in file resource in middleware (#57)
Browse files Browse the repository at this point in the history
  • Loading branch information
ayoubfaouzi authored Feb 23, 2024
1 parent e82f37d commit 0636142
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
5 changes: 5 additions & 0 deletions internal/file/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ func (m middleware) VerifyHash(next echo.HandlerFunc) echo.HandlerFunc {
m.logger.Error("failed to match sha256 regex for doc %v", sha256)
return e.BadRequest("invalid sha256 hash")
}

// Change the <sha256> path paramater to lower case. This will reflect on
// any handler that uses `VerifyHash` middleware.
c.SetParamValues(sha256)

docExists, err := m.service.Exists(c.Request().Context(), sha256)
if err != nil {
return err
Expand Down
11 changes: 5 additions & 6 deletions internal/file/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,7 @@ func (r repository) Patch(ctx context.Context, key, path string,

// Delete deletes a file with the specified ID from the database.
func (r repository) Delete(ctx context.Context, id string) error {
key := strings.ToLower(id)
return r.db.Delete(ctx, key)
return r.db.Delete(ctx, id)
}

// Count returns the number of the file records in the database.
Expand Down Expand Up @@ -163,7 +162,7 @@ func (r repository) Summary(ctx context.Context, id string) (
var results interface{}
var query string
params := make(map[string]interface{}, 1)
params["sha256"] = strings.ToLower(id)
params["sha256"] = id

if user, ok := ctx.Value(entity.UserKey).(entity.User); ok {
params["loggedInUser"] = user.ID()
Expand Down Expand Up @@ -191,7 +190,7 @@ func (r repository) Comments(ctx context.Context, id string, offset,
params := make(map[string]interface{}, 1)
params["offset"] = offset
params["limit"] = limit
params["sha256"] = strings.ToLower(id)
params["sha256"] = id

if user, ok := ctx.Value(entity.UserKey).(entity.User); ok {
params["loggedInUser"] = user.ID()
Expand All @@ -213,7 +212,7 @@ func (r repository) CountStrings(ctx context.Context, id string) (int, error) {
var count int

params := make(map[string]interface{}, 1)
params["sha256"] = strings.ToLower(id)
params["sha256"] = id

query := r.db.N1QLQuery[dbcontext.CountStrings]
err := r.db.Count(ctx, query, params, &count)
Expand All @@ -228,7 +227,7 @@ func (r repository) Strings(ctx context.Context, id string, offset,
params := make(map[string]interface{}, 1)
params["offset"] = offset
params["limit"] = limit
params["sha256"] = strings.ToLower(id)
params["sha256"] = id

query := r.db.N1QLQuery[dbcontext.FileStrings]
err := r.db.Query(ctx, query, params, &results)
Expand Down

0 comments on commit 0636142

Please sign in to comment.