From 08c577380ee12a263c3c430ca0ffd4b39e5eef6a Mon Sep 17 00:00:00 2001 From: Abhishek Raj Date: Sun, 28 Jan 2024 16:01:30 +0530 Subject: [PATCH] feat: added image route and path hashing --- constants/image.go | 13 +++++++++++++ controllers/image.go | 14 +++++++++++++- routes/routes.go | 7 +++++-- service/image.go | 6 ------ utils/helper.go | 25 +++++++++++++++++++++++++ 5 files changed, 56 insertions(+), 9 deletions(-) create mode 100644 constants/image.go diff --git a/constants/image.go b/constants/image.go new file mode 100644 index 0000000..e842930 --- /dev/null +++ b/constants/image.go @@ -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", +} diff --git a/controllers/image.go b/controllers/image.go index 08a98ec..e8285fc 100644 --- a/controllers/image.go +++ b/controllers/image.go @@ -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) +} diff --git a/routes/routes.go b/routes/routes.go index 944c840..16c5047 100644 --- a/routes/routes.go +++ b/routes/routes.go @@ -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 } diff --git a/service/image.go b/service/image.go index d3549c1..6d43c33 100644 --- a/service/image.go +++ b/service/image.go @@ -1,7 +1 @@ package service - -type Image struct{} - -func (*Image) Get() { - -} diff --git a/utils/helper.go b/utils/helper.go index f6f6507..32d02a9 100644 --- a/utils/helper.go +++ b/utils/helper.go @@ -2,9 +2,12 @@ package utils import ( "bytes" + "crypto/md5" "crypto/rand" "encoding/base64" + "encoding/hex" "log" + "sort" ) /* @@ -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 +}