diff --git a/client/internal/http/aws_auth.go b/client/internal/http/aws_auth.go index 6303175..fdfc7a5 100644 --- a/client/internal/http/aws_auth.go +++ b/client/internal/http/aws_auth.go @@ -102,7 +102,7 @@ func (a *AWSAuth) Auth(c *gin.Context) { verifySig := getSignatureFromAWSHeader(verifyReq) if !strings.EqualFold(verifySig, reqSig) { - logger.Warnf("signature mismatch, expect(%d): %s, actual(%d): %s", len(reqSig), reqSig, len(verifySig), verifySig) + logger.Warnf("signature mismatch, input header: %s, verify: %s", authorizationHeader, verifySig) c.AbortWithStatusJSON(http.StatusOK, Failed(errorcode.Unauthorized, "signature mismatch")) return } @@ -158,7 +158,7 @@ func (a *AWSAuth) AuthWithoutBody(c *gin.Context) { verifySig := getSignatureFromAWSHeader(verifyReq) if !strings.EqualFold(verifySig, reqSig) { - logger.Warnf("signature mismatch, expect(%d): %s, actual(%d): %s", len(reqSig), reqSig, len(verifySig), verifySig) + logger.Warnf("signature mismatch, input header: %s, verify: %s", authorizationHeader, verifySig) c.AbortWithStatusJSON(http.StatusOK, Failed(errorcode.Unauthorized, "signature mismatch")) return } @@ -221,6 +221,7 @@ func (a *AWSAuth) PresignedAuth(c *gin.Context) { verifySig := getSignatureFromAWSQuery(uri) if !strings.EqualFold(verifySig, signature) { + logger.Warnf("signature mismatch, input: %s, verify: %s", signature, verifySig) c.AbortWithStatusJSON(http.StatusOK, Failed(errorcode.Unauthorized, "signature mismatch")) return } diff --git a/client/internal/http/presigned.go b/client/internal/http/presigned.go index 27cf389..aef1c0e 100644 --- a/client/internal/http/presigned.go +++ b/client/internal/http/presigned.go @@ -82,6 +82,48 @@ func (s *PresignedService) ObjectDownloadByPath(ctx *gin.Context) { } } +func (s *PresignedService) ObjectDownload(ctx *gin.Context) { + log := logger.WithField("HTTP", "Presigned.ObjectDownloadByPath") + + var req cdsapi.PresignedObjectDownload + if err := ctx.ShouldBindQuery(&req); err != nil { + log.Warnf("binding query: %s", err.Error()) + ctx.JSON(http.StatusBadRequest, Failed(errorcode.BadArgument, "missing argument or invalid argument")) + return + } + + off := req.Offset + len := int64(-1) + if req.Length != nil { + len = *req.Length + } + + file, err := s.svc.ObjectSvc().Download(req.UserID, downloader.DownloadReqeust{ + ObjectID: req.ObjectID, + Offset: off, + Length: len, + }) + if err != nil { + log.Warnf("downloading object: %s", err.Error()) + ctx.JSON(http.StatusOK, Failed(errorcode.OperationFailed, "download object failed")) + return + } + defer file.File.Close() + + ctx.Header("Content-Disposition", "attachment; filename="+url.PathEscape(path.Base(file.Object.Path))) + ctx.Header("Content-Type", "application/octet-stream") + ctx.Header("Content-Transfer-Encoding", "binary") + + n, err := io.Copy(ctx.Writer, file.File) + if err != nil { + log.Warnf("copying file: %s", err.Error()) + } + + if config.Cfg().StorageID > 0 { + s.svc.AccessStat.AddAccessCounter(file.Object.ObjectID, file.Object.PackageID, config.Cfg().StorageID, math2.DivOrDefault(float64(n), float64(file.Object.Size), 1)) + } +} + func (s *PresignedService) ObjectUpload(ctx *gin.Context) { log := logger.WithField("HTTP", "Presigned.ObjectUpload") diff --git a/client/internal/http/server.go b/client/internal/http/server.go index 123e66a..3a12144 100644 --- a/client/internal/http/server.go +++ b/client/internal/http/server.go @@ -125,6 +125,7 @@ func (s *Server) routeV1(eg *gin.Engine, rt gin.IRoutes) { rt.POST(cdsapi.ObjectCompleteMultipartUploadPath, s.Object().CompleteMultipartUpload) rt.GET(cdsapi.PresignedObjectDownloadByPathPath, s.awsAuth.PresignedAuth, s.Presigned().ObjectDownloadByPath) + rt.GET(cdsapi.PresignedObjectDownloadPath, s.awsAuth.PresignedAuth, s.Presigned().ObjectDownload) rt.POST(cdsapi.PresignedObjectUploadPath, s.awsAuth.PresignedAuth, s.Presigned().ObjectUpload) rt.POST(cdsapi.PresignedObjectNewMultipartUploadPath, s.awsAuth.PresignedAuth, s.Presigned().ObjectNewMultipartUpload)