From 085a4ab65a8e2c09bd296307e3d31e9df9bf7fc1 Mon Sep 17 00:00:00 2001 From: zouap Date: Tue, 19 Oct 2021 11:44:44 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=BB=A3=E7=A0=81=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zouap --- modules/storage/obs.go | 19 ++++++++++++++++ routers/repo/attachment.go | 46 ++++++++++++++++++++++++++++++++------ routers/routes/routes.go | 3 ++- 3 files changed, 60 insertions(+), 8 deletions(-) diff --git a/modules/storage/obs.go b/modules/storage/obs.go index 8685c17b6..813e9d017 100755 --- a/modules/storage/obs.go +++ b/modules/storage/obs.go @@ -5,6 +5,7 @@ package storage import ( + "fmt" "io" "path" "strconv" @@ -129,6 +130,24 @@ func ObsMultiPartUpload(uuid string, uploadId string, partNumber int, fileName s } +func ObsDownload(uuid string, fileName string) (io.ReadCloser, error) { + input := &obs.GetObjectInput{} + input.Bucket = setting.Bucket + input.Key = strings.TrimPrefix(path.Join(setting.BasePath, path.Join(uuid[0:1], uuid[1:2], uuid, fileName)), "/") + output, err := ObsCli.GetObject(input) + if err == nil { + log.Info("StorageClass:%s, ETag:%s, ContentType:%s, ContentLength:%d, LastModified:%s\n", + output.StorageClass, output.ETag, output.ContentType, output.ContentLength, output.LastModified) + return output.Body, nil + } else if obsError, ok := err.(obs.ObsError); ok { + fmt.Printf("Code:%s\n", obsError.Code) + fmt.Printf("Message:%s\n", obsError.Message) + return nil, obsError + } else { + return nil, err + } +} + func ObsGenMultiPartSignedUrl(uuid string, uploadId string, partNumber int, fileName string) (string, error) { input := &obs.CreateSignedUrlInput{} diff --git a/routers/repo/attachment.go b/routers/repo/attachment.go index bd80c070d..9468d37db 100755 --- a/routers/repo/attachment.go +++ b/routers/repo/attachment.go @@ -262,10 +262,15 @@ func GetAttachment(ctx *context.Context) { return } } else { - url, err = storage.ObsGetPreSignedUrl(attach.UUID, attach.Name) - if err != nil { - ctx.ServerError("ObsGetPreSignedUrl", err) - return + if setting.PROXYURL != "" { + url = setting.PROXYURL + "/obs_proxy_download?uuid=" + attach.UUID + "&file_name=" + attach.Name + log.Info("return url=" + url) + } else { + url, err = storage.ObsGetPreSignedUrl(attach.UUID, attach.Name) + if err != nil { + ctx.ServerError("ObsGetPreSignedUrl", err) + return + } } } @@ -273,7 +278,6 @@ func GetAttachment(ctx *context.Context) { ctx.ServerError("Update", err) return } - http.Redirect(ctx.Resp, ctx.Req.Request, url, http.StatusMovedPermanently) } else { fr, err := storage.Attachments.Open(attach.RelativePath()) @@ -282,7 +286,7 @@ func GetAttachment(ctx *context.Context) { return } defer fr.Close() - + log.Info("go here to download.") if err = increaseDownloadCount(attach, dataSet); err != nil { ctx.ServerError("Update", err) return @@ -680,6 +684,34 @@ func PutOBSProxyUpload(ctx *context.Context) { } } +func GetOBSProxyDownload(ctx *context.Context) { + uuid := ctx.Query("uuid") + fileName := ctx.Query("file_name") + + body, err := storage.ObsDownload(uuid, fileName) + if err != nil { + log.Info("upload error.") + } else { + defer body.Close() + ctx.Resp.Header().Set("Content-Disposition", "attachment; filename="+fileName) + ctx.Resp.Header().Set("Content-Type", "application/octet-stream") + p := make([]byte, 1024) + var readErr error + var readCount int + // 读取对象内容 + for { + readCount, readErr = body.Read(p) + if readCount > 0 { + ctx.Resp.Write(p[:readCount]) + //fmt.Printf("%s", p[:readCount]) + } + if readErr != nil { + break + } + } + } +} + func GetMultipartUploadUrl(ctx *context.Context) { uuid := ctx.Query("uuid") uploadID := ctx.Query("uploadID") @@ -708,7 +740,7 @@ func GetMultipartUploadUrl(ctx *context.Context) { } } else { if setting.PROXYURL != "" { - url = setting.PROXYURL + "?uuid=" + uuid + "&uploadId=" + uploadID + "&partNumber=" + fmt.Sprint(partNumber) + "&file_name=" + fileName + url = setting.PROXYURL + "/obs_proxy_multipart?uuid=" + uuid + "&uploadId=" + uploadID + "&partNumber=" + fmt.Sprint(partNumber) + "&file_name=" + fileName log.Info("return url=" + url) } else { url, err = storage.ObsGenMultiPartSignedUrl(uuid, uploadID, partNumber, fileName) diff --git a/routers/routes/routes.go b/routers/routes/routes.go index 388499bfc..0a17c832c 100755 --- a/routers/routes/routes.go +++ b/routers/routes/routes.go @@ -13,6 +13,7 @@ import ( "time" "code.gitea.io/gitea/routers/operation" + "code.gitea.io/gitea/routers/private" "code.gitea.io/gitea/routers/secure" @@ -34,7 +35,6 @@ import ( "code.gitea.io/gitea/routers/dev" "code.gitea.io/gitea/routers/events" "code.gitea.io/gitea/routers/org" - "code.gitea.io/gitea/routers/private" "code.gitea.io/gitea/routers/repo" "code.gitea.io/gitea/routers/user" userSetting "code.gitea.io/gitea/routers/user/setting" @@ -567,6 +567,7 @@ func RegisterRoutes(m *macaron.Macaron) { m.Get("/get_chunks", repo.GetSuccessChunks) m.Get("/new_multipart", repo.NewMultipart) m.Put("/obs_proxy_multipart", repo.PutOBSProxyUpload) + m.Get("/obs_proxy_download", repo.GetOBSProxyDownload) m.Get("/get_multipart_url", repo.GetMultipartUploadUrl) m.Post("/complete_multipart", repo.CompleteMultipart) m.Post("/update_chunk", repo.UpdateMultipart)