64 lines
1.4 KiB
Go
64 lines
1.4 KiB
Go
package handlers
|
|
|
|
import (
|
|
"database/sql"
|
|
"net/http"
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
func prestashopImageURL(baseURL string, imageID sql.NullInt64, imageType string) string {
|
|
if !imageID.Valid || imageID.Int64 == 0 {
|
|
return ""
|
|
}
|
|
id := strconv.FormatInt(imageID.Int64, 10)
|
|
var path strings.Builder
|
|
path.Grow(len(baseURL) + len(id)*2 + len(imageType) + 16)
|
|
path.WriteString(strings.TrimRight(baseURL, "/"))
|
|
path.WriteString("/img/p")
|
|
for _, ch := range id {
|
|
path.WriteByte('/')
|
|
path.WriteRune(ch)
|
|
}
|
|
path.WriteByte('/')
|
|
path.WriteString(id)
|
|
if strings.TrimSpace(imageType) != "" {
|
|
path.WriteByte('-')
|
|
path.WriteString(strings.TrimSpace(imageType))
|
|
}
|
|
path.WriteString(".webp")
|
|
return path.String()
|
|
}
|
|
|
|
func requestBaseURL(req *http.Request) string {
|
|
if req == nil {
|
|
return ""
|
|
}
|
|
|
|
scheme := "http"
|
|
if req.TLS != nil {
|
|
scheme = "https"
|
|
} else if forwarded := req.Header.Get("X-Forwarded-Proto"); forwarded != "" {
|
|
if strings.Contains(forwarded, ",") {
|
|
forwarded = strings.TrimSpace(strings.Split(forwarded, ",")[0])
|
|
}
|
|
if strings.TrimSpace(forwarded) != "" {
|
|
scheme = strings.TrimSpace(forwarded)
|
|
}
|
|
}
|
|
|
|
host := req.Header.Get("X-Forwarded-Host")
|
|
if host == "" {
|
|
host = req.Host
|
|
}
|
|
if strings.Contains(host, ",") {
|
|
host = strings.TrimSpace(strings.Split(host, ",")[0])
|
|
}
|
|
host = strings.TrimSpace(host)
|
|
if host == "" {
|
|
return ""
|
|
}
|
|
|
|
return scheme + "://" + host
|
|
}
|