Files
hfb_sys/backend/internal/modules/file/handler.go
T

409 lines
11 KiB
Go

package file
import (
"context"
"errors"
"io"
"net/http"
"strconv"
"strings"
"time"
"hfb_sys/backend/internal/filekey"
"hfb_sys/backend/internal/logging"
"hfb_sys/backend/internal/middleware"
"hfb_sys/backend/internal/tenancy"
"hfb_sys/backend/pkg/response"
"github.com/gin-gonic/gin"
"go.uber.org/zap"
)
type Handler struct {
service *Service
storage *Storage
objectAuthorizer ObjectAuthorizer
adminObjectAuthorizer AdminObjectAuthorizer
uploadOwnerRecorder UploadOwnerRecorder
accessTickets *AccessTicketManager
authGate chan struct{}
}
// ObjectAuthorizer 校验用户是否可读取指定私有对象。
type ObjectAuthorizer func(ctx context.Context, userID uint64, key string) (bool, error)
// AdminObjectAuthorizer 校验管理员是否可在当前经营站点读取指定私有对象。
type AdminObjectAuthorizer func(ctx context.Context, adminID uint64, key string) (bool, error)
// UploadOwnerRecorder 保存用户或管理员上传私有文件的归属。
type UploadOwnerRecorder func(ctx context.Context, userID, adminID uint64, objectKeys []string) error
const maxConcurrentFileAuthorizations = 8
func NewHandler(service *Service, storage *Storage, objectAuthorizer ObjectAuthorizer, uploadOwnerRecorder UploadOwnerRecorder, adminObjectAuthorizers ...AdminObjectAuthorizer) *Handler {
var adminObjectAuthorizer AdminObjectAuthorizer
if len(adminObjectAuthorizers) > 0 {
adminObjectAuthorizer = adminObjectAuthorizers[0]
}
return &Handler{
service: service,
storage: storage,
objectAuthorizer: objectAuthorizer,
adminObjectAuthorizer: adminObjectAuthorizer,
uploadOwnerRecorder: uploadOwnerRecorder,
authGate: make(chan struct{}, maxConcurrentFileAuthorizations),
}
}
func (h *Handler) SetAccessTicketManager(manager *AccessTicketManager) {
h.accessTickets = manager
}
func (h *Handler) Upload(c *gin.Context) {
header, err := c.FormFile("file")
if err != nil {
response.BadRequest(c, "请选择要上传的文件")
return
}
reader, err := header.Open()
if err != nil {
response.BadRequest(c, "文件读取失败")
return
}
defer func() {
_ = reader.Close()
}()
item, err := h.service.Upload(uploadRequest{
Context: c.Request.Context(),
Scene: c.PostForm("scene"),
Header: header,
Reader: reader,
ContentType: header.Header.Get("Content-Type"),
})
if err != nil {
writeFileError(c, err)
return
}
if h.uploadOwnerRecorder != nil {
var userID uint64
if value, exists := c.Get("user_id"); exists {
id, ok := value.(uint64)
if !ok {
response.Unauthorized(c, "用户上下文无效")
return
}
userID = id
}
var adminID uint64
if value, exists := c.Get("admin_id"); exists {
id, ok := value.(uint64)
if !ok {
response.Unauthorized(c, "管理员上下文无效")
return
}
adminID = id
}
if (userID > 0 || adminID > 0) &&
h.uploadOwnerRecorder(c.Request.Context(), userID, adminID, item.ObjectKeys()) != nil {
// 归属记录失败时补偿删除刚写入的对象,避免用户重试产生孤儿文件。
if h.storage != nil {
if removeErr := h.storage.RemoveObjects(c.Request.Context(), item.ObjectKeys()); removeErr != nil {
logging.FromContext(c.Request.Context()).Warn("补偿删除上传对象失败", zap.Error(removeErr))
}
}
response.ServiceUnavailable(c, "文件归属记录失败")
return
}
}
response.Created(c, item)
}
type batchAccessRequest struct {
Keys []string `json:"keys"`
}
type batchAccessResponse struct {
Tickets map[string]string `json:"tickets"`
ExpiresInSeconds int64 `json:"expires_in_seconds"`
}
// BatchAccess 在同一请求内校验相册图片,并签发短期对象访问票据。
func (h *Handler) BatchAccess(c *gin.Context) {
if h.accessTickets == nil {
response.ServiceUnavailable(c, "文件访问票据未启用")
return
}
var req batchAccessRequest
if err := c.ShouldBindJSON(&req); err != nil {
response.BadRequest(c, "文件列表格式不正确")
return
}
if len(req.Keys) == 0 || len(req.Keys) > 100 {
response.BadRequest(c, "每次最多校验 100 个文件")
return
}
subjectType, subjectID := fileAccessSubject(c)
if subjectID == 0 {
response.Unauthorized(c, "缺少文件访问身份")
return
}
if !h.acquireAuthorizationSlot(c.Request.Context(), 500*time.Millisecond) {
c.Header("Retry-After", "1")
response.Error(c, http.StatusTooManyRequests, "file_auth_busy", "图片鉴权繁忙,请稍后重试")
return
}
defer h.releaseAuthorizationSlot()
ctx, cancel := context.WithTimeout(c.Request.Context(), 5*time.Second)
defer cancel()
tenantID := tenancy.IDFromContext(ctx)
tickets := make(map[string]string, len(req.Keys))
seen := make(map[string]struct{}, len(req.Keys))
for _, rawKey := range req.Keys {
rawKey = strings.TrimSpace(rawKey)
canonical := filekey.Canonical(rawKey)
if rawKey == "" || canonical == "" {
continue
}
identity := rawKey + "\x00" + canonical
if _, ok := seen[identity]; ok {
continue
}
seen[identity] = struct{}{}
allowed, err := h.authorizeObject(ctx, subjectType, subjectID, rawKey)
if err != nil {
response.ServiceUnavailable(c, "文件权限校验失败")
return
}
if !allowed {
continue
}
if ticket := h.accessTickets.Issue(subjectType, subjectID, tenantID, canonical); ticket != "" {
tickets[rawKey] = ticket
}
}
response.OK(c, batchAccessResponse{
Tickets: tickets,
ExpiresInSeconds: int64(accessTicketTTL.Seconds()),
})
}
func (h *Handler) Object(c *gin.Context) {
h.writeObject(c, false)
}
func (h *Handler) PublicObject(c *gin.Context) {
h.writeObject(c, true)
}
func (h *Handler) writeObject(c *gin.Context, publicOnly bool) {
totalStart := time.Now()
if h.storage == nil {
response.ServiceUnavailable(c, "文件存储未连接")
return
}
key := strings.TrimSpace(c.Query("key"))
if key == "" || strings.Contains(key, "..") {
response.BadRequest(c, "文件 key 不正确")
return
}
if publicOnly && !isPublicObjectKey(key) {
response.Error(c, http.StatusNotFound, "not_found", "文件不存在或暂不可访问")
return
}
authMS := 0.0
accessSource := "public"
if !publicOnly {
authStart := time.Now()
subjectType, subjectID := fileAccessSubject(c)
tenantID := tenancy.IDFromContext(c.Request.Context())
if ticket := strings.TrimSpace(c.Query("ticket")); ticket != "" &&
h.accessTickets != nil &&
h.accessTickets.Valid(ticket, subjectType, subjectID, tenantID, key) {
accessSource = "ticket"
} else {
accessSource = "database"
if !h.acquireAuthorizationSlot(c.Request.Context(), 500*time.Millisecond) {
c.Header("Retry-After", "1")
response.Error(c, http.StatusTooManyRequests, "file_auth_busy", "图片鉴权繁忙,请稍后重试")
return
}
defer h.releaseAuthorizationSlot()
authCtx, cancel := context.WithTimeout(c.Request.Context(), 3*time.Second)
allowed, err := h.authorizeObject(authCtx, subjectType, subjectID, key)
cancel()
if err != nil {
response.ServiceUnavailable(c, "文件权限校验失败")
return
}
if !allowed {
response.Error(c, http.StatusForbidden, "forbidden", "无权访问该文件")
return
}
}
authMS = float64(time.Since(authStart).Microseconds()) / 1000
}
storageStart := time.Now()
object, err := h.storage.Get(c.Request.Context(), key)
storageMS := float64(time.Since(storageStart).Microseconds()) / 1000
if err != nil {
fields := []zap.Field{
zap.String("access_source", accessSource),
zap.String("object_prefix", fileObjectPrefix(key)),
zap.Float64("auth_ms", authMS),
zap.Float64("storage_open_ms", storageMS),
zap.Error(err),
}
logger := logging.FromContext(c.Request.Context())
logger.Debug("文件存储打开失败", fields...)
if storageMS >= 500 {
logger.Warn("文件存储打开慢请求", fields...)
}
response.Error(c, http.StatusNotFound, "not_found", "文件不存在或暂不可访问")
return
}
defer func() {
_ = object.Reader.Close()
}()
contentType := object.ContentType
if contentType == "" {
contentType = "application/octet-stream"
}
c.Header("Content-Type", contentType)
if publicOnly {
c.Header("Cache-Control", "public, max-age=86400")
} else {
c.Header("Cache-Control", "private, max-age=300")
}
c.Header("Content-Length", strconv.FormatInt(object.Size, 10))
c.Status(http.StatusOK)
streamStart := time.Now()
_, streamErr := io.Copy(c.Writer, object.Reader)
streamMS := float64(time.Since(streamStart).Microseconds()) / 1000
totalMS := float64(time.Since(totalStart).Microseconds()) / 1000
if streamErr != nil {
response.RecordError(c, streamErr)
}
fields := []zap.Field{
zap.Bool("public", publicOnly),
zap.String("access_source", accessSource),
zap.String("object_prefix", fileObjectPrefix(key)),
zap.Int64("object_size", object.Size),
zap.Int("status", c.Writer.Status()),
zap.Float64("auth_ms", authMS),
zap.Float64("storage_open_ms", storageMS),
zap.Float64("stream_ms", streamMS),
zap.Float64("total_ms", totalMS),
}
if streamErr != nil {
fields = append(fields, zap.Error(streamErr))
}
logger := logging.FromContext(c.Request.Context())
logger.Debug("私有文件读取", fields...)
if totalMS >= 1000 {
logger.Warn("文件读取慢请求", fields...)
}
}
func isPublicObjectKey(key string) bool {
for _, prefix := range []string{
"home-banner/",
"avatar/",
"announcement/",
"mohong/",
"crash/",
"aw-recycle/",
"cooperation-feedback/",
"support-qrcode/",
"points-mall/",
} {
if strings.HasPrefix(key, prefix) {
return true
}
}
return false
}
func fileAccessSubject(c *gin.Context) (string, uint64) {
if value, exists := c.Get(middleware.ContextAdminID); exists {
if adminID, ok := value.(uint64); ok && adminID > 0 {
return "admin", adminID
}
}
if value, exists := c.Get(middleware.ContextUserID); exists {
if userID, ok := value.(uint64); ok && userID > 0 {
return "user", userID
}
}
return "", 0
}
func (h *Handler) authorizeObject(ctx context.Context, subjectType string, subjectID uint64, key string) (bool, error) {
if subjectID == 0 {
return false, nil
}
if subjectType == "admin" {
if h.adminObjectAuthorizer == nil {
return false, nil
}
return h.adminObjectAuthorizer(ctx, subjectID, key)
}
if h.objectAuthorizer == nil {
return false, nil
}
return h.objectAuthorizer(ctx, subjectID, key)
}
func (h *Handler) acquireAuthorizationSlot(ctx context.Context, wait time.Duration) bool {
if h.authGate == nil {
return true
}
select {
case h.authGate <- struct{}{}:
return true
default:
}
timer := time.NewTimer(wait)
defer timer.Stop()
select {
case h.authGate <- struct{}{}:
return true
case <-ctx.Done():
return false
case <-timer.C:
return false
}
}
func (h *Handler) releaseAuthorizationSlot() {
if h.authGate == nil {
return
}
select {
case <-h.authGate:
default:
}
}
func fileObjectPrefix(key string) string {
if index := strings.IndexByte(key, '/'); index > 0 {
return key[:index]
}
return key
}
func writeFileError(c *gin.Context, err error) {
response.RecordError(c, err)
switch {
case errors.Is(err, ErrDependencyUnavailable):
response.ServiceUnavailable(c, "文件存储未连接")
case errors.Is(err, ErrInvalidFile):
response.BadRequest(c, "文件不符合规则,仅支持 10MB 内的 JPG、PNG、WebP 或 PDF")
default:
response.Error(c, http.StatusInternalServerError, "internal_error", "文件服务暂时不可用")
}
}