Files
geo/server/internal/tenant/transport/asset_handler.go
T
root 87e329207c feat(publish): add baijiahao and jianshu desktop adapters
Wires up Baijiahao (百家号) and Jianshu (简书) as first-class desktop
publish targets, with risk-control prompts surfaced in the runtime
controller and a normalized error message in publish records. Adds
external-link buttons in the publish management table, an asset
format conversion endpoint for cover image compatibility, and
reorders publish-status display priority so failures take precedence
over partial successes.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-26 19:47:35 +08:00

124 lines
3.0 KiB
Go

package transport
import (
"bytes"
"crypto/hmac"
"crypto/sha256"
"encoding/base64"
"fmt"
"image"
"image/jpeg"
"image/png"
"net/http"
"strings"
"github.com/gin-gonic/gin"
_ "golang.org/x/image/webp"
"github.com/geo-platform/tenant-api/internal/bootstrap"
)
type AssetHandler struct {
secret string
app *bootstrap.App
}
func NewAssetHandler(app *bootstrap.App) *AssetHandler {
return &AssetHandler{
secret: strings.TrimSpace(app.Config.JWT.Secret),
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
}
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 {
encodedKey := base64.RawURLEncoding.EncodeToString([]byte(objectKey))
mac := hmac.New(sha256.New, []byte(h.secret))
_, _ = mac.Write([]byte(objectKey))
signature := base64.RawURLEncoding.EncodeToString(mac.Sum(nil))
return fmt.Sprintf("%s.%s", encodedKey, signature)
}
func (h *AssetHandler) parseToken(token string) (string, bool) {
parts := strings.Split(token, ".")
if len(parts) != 2 {
return "", false
}
objectKeyBytes, err := base64.RawURLEncoding.DecodeString(parts[0])
if err != nil {
return "", false
}
objectKey := string(objectKeyBytes)
if strings.TrimSpace(objectKey) == "" {
return "", false
}
expected := h.signObjectKey(objectKey)
if !hmac.Equal([]byte(expected), []byte(token)) {
return "", false
}
return objectKey, true
}