130 lines
3.3 KiB
Go
130 lines
3.3 KiB
Go
package transport
|
|
|
|
import (
|
|
"bytes"
|
|
"image"
|
|
"image/jpeg"
|
|
"image/png"
|
|
"net/http"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
_ "golang.org/x/image/webp"
|
|
|
|
"github.com/geo-platform/tenant-api/internal/bootstrap"
|
|
"github.com/geo-platform/tenant-api/internal/shared/publicasset"
|
|
)
|
|
|
|
type AssetHandler struct {
|
|
app *bootstrap.App
|
|
}
|
|
|
|
func NewAssetHandler(app *bootstrap.App) *AssetHandler {
|
|
return &AssetHandler{app: app}
|
|
}
|
|
|
|
func (h *AssetHandler) BuildArticleImageURL(objectKey string) string {
|
|
token := h.signObjectKey(objectKey)
|
|
return "/api/public/assets/" + token
|
|
}
|
|
|
|
func (h *AssetHandler) Serve(c *gin.Context) {
|
|
objectKey, ok := h.parseToken(c.Param("token"))
|
|
if !ok {
|
|
c.AbortWithStatus(http.StatusNotFound)
|
|
return
|
|
}
|
|
|
|
h.serveObject(c, objectKey)
|
|
}
|
|
|
|
func (h *AssetHandler) ServeDesktop(c *gin.Context) {
|
|
client := MustDesktopClient(c.Request.Context())
|
|
objectKey, ok := objectKeyFromAssetToken(c.Param("token"))
|
|
if !ok || !isDesktopClientAssetKey(objectKey, client.TenantID) {
|
|
c.AbortWithStatus(http.StatusNotFound)
|
|
return
|
|
}
|
|
|
|
h.serveObject(c, objectKey)
|
|
}
|
|
|
|
func (h *AssetHandler) serveObject(c *gin.Context, objectKey string) {
|
|
content, err := h.app.ObjectStorage.GetBytes(c.Request.Context(), objectKey)
|
|
if err != nil || len(content) == 0 {
|
|
c.AbortWithStatus(http.StatusNotFound)
|
|
return
|
|
}
|
|
|
|
if converted, contentType, ok := convertPublicAssetFormat(content, c.Query("format")); ok {
|
|
c.Header("Cache-Control", "public, max-age=31536000, immutable")
|
|
c.Header("Content-Type", contentType)
|
|
c.Writer.WriteHeader(http.StatusOK)
|
|
_, _ = c.Writer.Write(converted)
|
|
return
|
|
}
|
|
|
|
contentType := http.DetectContentType(content)
|
|
c.Header("Cache-Control", "public, max-age=31536000, immutable")
|
|
c.Header("Content-Type", contentType)
|
|
c.Writer.WriteHeader(http.StatusOK)
|
|
_, _ = c.Writer.Write(content)
|
|
}
|
|
|
|
func convertPublicAssetFormat(content []byte, format string) ([]byte, string, bool) {
|
|
normalized := strings.ToLower(strings.TrimSpace(format))
|
|
if normalized == "" {
|
|
return nil, "", false
|
|
}
|
|
if normalized != "png" && normalized != "jpg" && normalized != "jpeg" {
|
|
return nil, "", false
|
|
}
|
|
|
|
decoded, _, err := image.Decode(bytes.NewReader(content))
|
|
if err != nil {
|
|
return nil, "", false
|
|
}
|
|
|
|
var output bytes.Buffer
|
|
if normalized == "png" {
|
|
if err := png.Encode(&output, decoded); err != nil {
|
|
return nil, "", false
|
|
}
|
|
return output.Bytes(), "image/png", true
|
|
}
|
|
|
|
if err := jpeg.Encode(&output, decoded, &jpeg.Options{Quality: 92}); err != nil {
|
|
return nil, "", false
|
|
}
|
|
return output.Bytes(), "image/jpeg", true
|
|
}
|
|
|
|
func (h *AssetHandler) signObjectKey(objectKey string) string {
|
|
return publicasset.SignObjectKey(objectKey, h.secret())
|
|
}
|
|
|
|
func (h *AssetHandler) secret() string {
|
|
if h == nil || h.app == nil || h.app.Config() == nil {
|
|
return ""
|
|
}
|
|
return strings.TrimSpace(h.app.Config().JWT.Secret)
|
|
}
|
|
|
|
func (h *AssetHandler) parseToken(token string) (string, bool) {
|
|
return publicasset.ParseObjectKeyToken(token, h.secret())
|
|
}
|
|
|
|
func objectKeyFromAssetToken(token string) (string, bool) {
|
|
return publicasset.ObjectKeyFromToken(token)
|
|
}
|
|
|
|
func isDesktopClientAssetKey(objectKey string, tenantID int64) bool {
|
|
if tenantID <= 0 {
|
|
return false
|
|
}
|
|
tenantPrefix := "tenants/" + strconv.FormatInt(tenantID, 10) + "/"
|
|
return strings.HasPrefix(objectKey, tenantPrefix+"images/") ||
|
|
strings.HasPrefix(objectKey, tenantPrefix+"articles/")
|
|
}
|