utils created
This commit is contained in:
parent
875e4eefbd
commit
d690b2c162
2 changed files with 57 additions and 0 deletions
7
internal/interfaces/utils.go
Normal file
7
internal/interfaces/utils.go
Normal file
|
|
@ -0,0 +1,7 @@
|
||||||
|
package interfaces
|
||||||
|
|
||||||
|
type Utils interface {
|
||||||
|
MerchImageVolumePath(u, m string) string
|
||||||
|
FilenameFromType(imgType string) string
|
||||||
|
MerchImageURL(u, m, t string) string
|
||||||
|
}
|
||||||
50
internal/utils/handler.go
Normal file
50
internal/utils/handler.go
Normal file
|
|
@ -0,0 +1,50 @@
|
||||||
|
package utils
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"net/url"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Handler struct {
|
||||||
|
domain string
|
||||||
|
volume string
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewHandler(domain, volume string) *Handler {
|
||||||
|
return &Handler{
|
||||||
|
domain: domain,
|
||||||
|
volume: volume,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// MerchImageVolumePath
|
||||||
|
// Returns a string like volume/merchImages/userUuid/merchUuid/filename.jpg
|
||||||
|
func (h *Handler) MerchImageVolumePath(u, m string) string {
|
||||||
|
return fmt.Sprintf("%s/merchImages/%s/%s", h.volume, u, m)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *Handler) FilenameFromType(imgType string) string {
|
||||||
|
switch imgType {
|
||||||
|
case "thumbnail":
|
||||||
|
return "thumbnail.jpg"
|
||||||
|
case "full":
|
||||||
|
return "full.jpg"
|
||||||
|
default:
|
||||||
|
return "unknown"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *Handler) MerchImageURL(u, m, t string) string {
|
||||||
|
base, err := url.Parse(h.domain)
|
||||||
|
if err != nil {
|
||||||
|
panic(fmt.Errorf("invalid domain in MerchImageURL: %w", err))
|
||||||
|
}
|
||||||
|
|
||||||
|
base.Path = fmt.Sprintf("/merchImages/%s/%s", url.PathEscape(u), url.PathEscape(m))
|
||||||
|
|
||||||
|
params := url.Values{}
|
||||||
|
params.Set("type", t)
|
||||||
|
base.RawQuery = params.Encode()
|
||||||
|
|
||||||
|
return base.String()
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue