f
This commit is contained in:
@@ -3,6 +3,7 @@ package handlers
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
@@ -35,18 +36,25 @@ func NewPDFGHandler(
|
||||
// DownloadPDF 下载PDF文件
|
||||
// GET /api/v1/pdfg/download?id=报告ID
|
||||
func (h *PDFGHandler) DownloadPDF(c *gin.Context) {
|
||||
reportID := c.Query("id")
|
||||
rawReportID := c.Query("id")
|
||||
|
||||
if reportID == "" {
|
||||
if rawReportID == "" {
|
||||
h.responseBuilder.BadRequest(c, "报告ID不能为空")
|
||||
return
|
||||
}
|
||||
|
||||
// 从对外报告ID中解析内部缓存键(使用最后一个'-'后的部分作为缓存键)
|
||||
cacheKey := rawReportID
|
||||
if idx := strings.LastIndex(rawReportID, "-"); idx > 0 && idx < len(rawReportID)-1 {
|
||||
cacheKey = rawReportID[idx+1:]
|
||||
}
|
||||
|
||||
// 从缓存获取PDF
|
||||
pdfBytes, hit, createdAt, err := h.cacheManager.GetByCacheKey(reportID)
|
||||
pdfBytes, hit, createdAt, err := h.cacheManager.GetByCacheKey(cacheKey)
|
||||
if err != nil {
|
||||
h.logger.Error("获取PDF缓存失败",
|
||||
zap.String("report_id", reportID),
|
||||
zap.String("report_id", rawReportID),
|
||||
zap.String("cache_key", cacheKey),
|
||||
zap.Error(err),
|
||||
)
|
||||
h.responseBuilder.InternalError(c, "获取PDF文件失败")
|
||||
@@ -55,7 +63,8 @@ func (h *PDFGHandler) DownloadPDF(c *gin.Context) {
|
||||
|
||||
if !hit {
|
||||
h.logger.Warn("PDF文件不存在或已过期",
|
||||
zap.String("report_id", reportID),
|
||||
zap.String("report_id", rawReportID),
|
||||
zap.String("cache_key", cacheKey),
|
||||
)
|
||||
h.responseBuilder.NotFound(c, "PDF文件不存在或已过期,请重新生成")
|
||||
return
|
||||
@@ -65,7 +74,8 @@ func (h *PDFGHandler) DownloadPDF(c *gin.Context) {
|
||||
expiresAt := createdAt.Add(24 * time.Hour)
|
||||
if time.Now().After(expiresAt) {
|
||||
h.logger.Warn("PDF文件已过期",
|
||||
zap.String("report_id", reportID),
|
||||
zap.String("report_id", rawReportID),
|
||||
zap.String("cache_key", cacheKey),
|
||||
zap.Time("expires_at", expiresAt),
|
||||
)
|
||||
h.responseBuilder.NotFound(c, "PDF文件已过期,请重新生成")
|
||||
@@ -74,14 +84,22 @@ func (h *PDFGHandler) DownloadPDF(c *gin.Context) {
|
||||
|
||||
// 设置响应头
|
||||
c.Header("Content-Type", "application/pdf")
|
||||
c.Header("Content-Disposition", `attachment; filename="大数据租赁风险报告.pdf"`)
|
||||
// 使用报告ID前缀作为下载文件名的一部分,避免泄露内部缓存键
|
||||
filename := "大数据租赁风险报告.pdf"
|
||||
if idx := strings.LastIndex(rawReportID, "-"); idx > 0 {
|
||||
// 使用前缀(报告编号部分)作为文件名的一部分
|
||||
prefix := rawReportID[:idx]
|
||||
filename = fmt.Sprintf("大数据租赁风险报告_%s.pdf", prefix)
|
||||
}
|
||||
c.Header("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, filename))
|
||||
c.Header("Content-Length", fmt.Sprintf("%d", len(pdfBytes)))
|
||||
|
||||
// 发送PDF文件
|
||||
c.Data(http.StatusOK, "application/pdf", pdfBytes)
|
||||
|
||||
h.logger.Info("PDF文件下载成功",
|
||||
zap.String("report_id", reportID),
|
||||
zap.String("report_id", rawReportID),
|
||||
zap.String("cache_key", cacheKey),
|
||||
zap.Int("file_size", len(pdfBytes)),
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user