diff --git a/modules/storage/local.go b/modules/storage/local.go index 7a5f00e63..076a7f5e2 100644 --- a/modules/storage/local.go +++ b/modules/storage/local.go @@ -64,3 +64,7 @@ func (l *LocalStorage) Delete(path string) error { p := filepath.Join(l.dir, path) return os.Remove(p) } + +func (l *LocalStorage) PresignedGetURL(path string, fileName string) (string,error) { + return "",nil +} diff --git a/modules/storage/minio.go b/modules/storage/minio.go index e93416bf7..5fafa9965 100644 --- a/modules/storage/minio.go +++ b/modules/storage/minio.go @@ -6,8 +6,10 @@ package storage import ( "io" + "net/url" "path" "strings" + "time" "github.com/minio/minio-go" ) @@ -16,6 +18,8 @@ var ( _ ObjectStorage = &MinioStorage{} ) +const PRESIGNED_URL_EXPIRE_TIME = time.Hour * 24 * 7 + // MinioStorage returns a minio bucket storage type MinioStorage struct { client *minio.Client @@ -68,3 +72,18 @@ func (m *MinioStorage) Save(path string, r io.Reader) (int64, error) { func (m *MinioStorage) Delete(path string) error { return m.client.RemoveObject(m.bucket, m.buildMinioPath(path)) } + +//Get Presigned URL +func (m *MinioStorage) PresignedGetURL(path string, fileName string) (string,error) { + // Set request parameters for content-disposition. + reqParams := make(url.Values) + reqParams.Set("response-content-disposition", "attachment; filename=\"" + fileName + "\"") + + var preURL *url.URL + preURL,err := m.client.PresignedGetObject(m.bucket, m.buildMinioPath(path), PRESIGNED_URL_EXPIRE_TIME, reqParams) + if err != nil { + return "",err + } + + return preURL.String(),nil +} diff --git a/modules/storage/storage.go b/modules/storage/storage.go index 963b38d0f..aca4a0b9c 100644 --- a/modules/storage/storage.go +++ b/modules/storage/storage.go @@ -16,6 +16,7 @@ type ObjectStorage interface { Save(path string, r io.Reader) (int64, error) Open(path string) (io.ReadCloser, error) Delete(path string) error + PresignedGetURL(path string, fileName string) (string, error) } // Copy copys a file from source ObjectStorage to dest ObjectStorage diff --git a/routers/repo/attachment.go b/routers/repo/attachment.go index 479e8b395..d059e9a83 100644 --- a/routers/repo/attachment.go +++ b/routers/repo/attachment.go @@ -5,6 +5,7 @@ package repo import ( + "code.gitea.io/gitea/modules/storage" "fmt" "net/http" "strings" @@ -13,7 +14,6 @@ import ( "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/setting" - "code.gitea.io/gitea/modules/storage" "code.gitea.io/gitea/modules/upload" ) @@ -124,6 +124,7 @@ func GetAttachment(ctx *context.Context) { } //If we have matched and access to release or issue + /* fr, err := storage.Attachments.Open(attach.RelativePath()) if err != nil { ctx.ServerError("Open", err) @@ -131,13 +132,29 @@ func GetAttachment(ctx *context.Context) { } defer fr.Close() + */ + + url, err := storage.Attachments.PresignedGetURL(attach.RelativePath(), attach.Name) + if err != nil { + ctx.ServerError("PresignedGetURL", err) + return + } + if err := attach.IncreaseDownloadCount(); err != nil { ctx.ServerError("Update", err) return } + ctx.JSON(200, map[string]string{ + "name": attach.Name, + "url": url, + }) + + /* if err = ServeData(ctx, attach.Name, fr); err != nil { ctx.ServerError("ServeData", err) return } + + */ }