From db0215899a335cb889bfe160007d48a1d3a630fd Mon Sep 17 00:00:00 2001 From: truthhun <1272881215@qq.com> Date: Wed, 15 Nov 2023 13:10:08 +0800 Subject: [PATCH] =?UTF-8?q?hash=E9=97=AE=E9=A2=98=E6=A3=80=E6=B5=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- biz/attachment.go | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/biz/attachment.go b/biz/attachment.go index 605bc35..d70f504 100644 --- a/biz/attachment.go +++ b/biz/attachment.go @@ -38,6 +38,11 @@ type AttachmentAPIService struct { logger *zap.Logger } +var errorHash = map[string]interface{}{ + "code": http.StatusBadRequest, + "message": "hash值必须32位", +} + func NewAttachmentAPIService(dbModel *model.DBModel, logger *zap.Logger) (service *AttachmentAPIService) { return &AttachmentAPIService{dbModel: dbModel, logger: logger.Named("AttachmentAPIService")} } @@ -243,7 +248,7 @@ func (s *AttachmentAPIService) UploadConfig(ctx *gin.Context) { func (s *AttachmentAPIService) ViewDocumentPages(ctx *gin.Context) { hash := ctx.Param("hash") if len(hash) != 32 { - ctx.JSON(http.StatusNotFound, nil) + ctx.JSON(http.StatusNotFound, errorHash) return } page := strings.TrimLeft(ctx.Param("page"), "./") @@ -261,6 +266,11 @@ func (s *AttachmentAPIService) ViewDocumentPages(ctx *gin.Context) { func (s *AttachmentAPIService) ViewDocumentCover(ctx *gin.Context) { hash := ctx.Param("hash") + if len(hash) != 32 { + ctx.JSON(http.StatusNotFound, errorHash) + return + } + file := fmt.Sprintf("documents/%s/%s/cover.png", strings.Join(strings.Split(hash, "")[:5], "/"), hash) if len(hash) != 32 { ctx.JSON(http.StatusNotFound, map[string]interface{}{"code": http.StatusNotFound, "message": "文件不存在"}) @@ -278,10 +288,11 @@ func (s *AttachmentAPIService) DownloadDocument(ctx *gin.Context) { jwtToken, err := jwt.ParseWithClaims(token, claims, func(t *jwt.Token) (interface{}, error) { return []byte(cfg.SecretKey), nil }) - if err != nil || !jwtToken.Valid { + if err != nil || !jwtToken.Valid || len(claims.Id) != 32 { ctx.String(http.StatusBadRequest, "下载链接已失效") return } + filename := ctx.Query("filename") file := fmt.Sprintf("documents/%s/%s%s", strings.Join(strings.Split(claims.Id, "")[:5], "/"), claims.Id, filepath.Ext(filename)) ctx.FileAttachment(file, filename)