Skip to content

Commit

Permalink
feat: added image route and path hashing
Browse files Browse the repository at this point in the history
  • Loading branch information
abskrj committed Jan 28, 2024
1 parent 4825168 commit 08c5773
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 9 deletions.
13 changes: 13 additions & 0 deletions constants/image.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package constants

type IMAGE_QUERY int

const (
Width IMAGE_QUERY = iota
Height
)

var ImageQuery = map[IMAGE_QUERY]string{
Width: "width",
Height: "height",
}
14 changes: 13 additions & 1 deletion controllers/image.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
package controllers

type ImageController struct{}
import (
"zestream-server/utils"

"github.com/gin-gonic/gin"
)

type Image struct{}

func (*Image) Get(c *gin.Context) {
subpath := c.Param("subpath")
d := utils.GetQueryHash(c.Request.URL.RawQuery)
c.String(200, subpath+":"+d)
}
7 changes: 5 additions & 2 deletions routes/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,18 @@ func Init() *gin.Engine {
}
})

process := new(controllers.Process)
image := new(controllers.Image)

apiV1 := r.Group("/api/v1")

r.GET("health", controllers.Ping)

process := new(controllers.Process)

// /api/v1
apiV1.POST("video/process", process.Video)
apiV1.GET("url/presigned", controllers.GetPresignedURL)

r.GET("/i/*subpath", image.Get)

return r
}
6 changes: 0 additions & 6 deletions service/image.go
Original file line number Diff line number Diff line change
@@ -1,7 +1 @@
package service

type Image struct{}

func (*Image) Get() {

}
25 changes: 25 additions & 0 deletions utils/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ package utils

import (
"bytes"
"crypto/md5"
"crypto/rand"
"encoding/base64"
"encoding/hex"
"log"
"sort"
)

/*
Expand Down Expand Up @@ -41,3 +44,25 @@ func LogErr(err error) {
log.Println(err)
}
}

// sortString sorts the given string
func SortString(input string) string {
runes := []rune(input)

sort.Slice(runes, func(i, j int) bool {
return runes[i] < runes[j]
})

return string(runes)
}

// GetQueryHash returns the md5 hash of the query string
func GetQueryHash(query string) string {
sortedQuery := SortString(query)

hasher := md5.New()
hasher.Write([]byte(sortedQuery))
hash := hex.EncodeToString(hasher.Sum(nil))

return hash
}

0 comments on commit 08c5773

Please sign in to comment.