diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2020-10-14 21:07:51 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-10-14 21:07:51 +0800 |
commit | 80a6b0f5bce15a641fc75f5f1ef6e42ef54424bc (patch) | |
tree | 504c7ccdc9cb42e0e282abdd8dbb75c4b24e9f5b /routers | |
parent | 93f7525061bc9e6f5be734aba0de31b64c63d7a8 (diff) | |
download | gitea-80a6b0f5bce15a641fc75f5f1ef6e42ef54424bc.tar.gz gitea-80a6b0f5bce15a641fc75f5f1ef6e42ef54424bc.zip |
Avatars and Repo avatars support storing in minio (#12516)
* Avatar support minio
* Support repo avatar minio storage
* Add missing migration
* Fix bug
* Fix test
* Add test for minio store type on avatars and repo avatars; Add documents
* Fix bug
* Fix bug
* Add back missed avatar link method
* refactor codes
* Simplify the codes
* Code improvements
* Fix lint
* Fix test mysql
* Fix test mysql
* Fix test mysql
* Fix settings
* Fix test
* fix test
* Fix bug
Diffstat (limited to 'routers')
-rw-r--r-- | routers/repo/setting.go | 5 | ||||
-rw-r--r-- | routers/routes/routes.go | 77 | ||||
-rw-r--r-- | routers/user/setting/profile.go | 5 |
3 files changed, 65 insertions, 22 deletions
diff --git a/routers/repo/setting.go b/routers/repo/setting.go index 1b75522958..e4f8adc38f 100644 --- a/routers/repo/setting.go +++ b/routers/repo/setting.go @@ -30,7 +30,6 @@ import ( mirror_service "code.gitea.io/gitea/services/mirror" repo_service "code.gitea.io/gitea/services/repository" - "github.com/unknwon/com" "mvdan.cc/xurls/v2" ) @@ -928,7 +927,7 @@ func UpdateAvatarSetting(ctx *context.Context, form auth.AvatarForm) error { // No avatar is uploaded and we not removing it here. // No random avatar generated here. // Just exit, no action. - if !com.IsFile(ctxRepo.CustomAvatarPath()) { + if ctxRepo.CustomAvatarRelativePath() == "" { log.Trace("No avatar was uploaded for repo: %d. Default icon will appear instead.", ctxRepo.ID) } return nil @@ -940,7 +939,7 @@ func UpdateAvatarSetting(ctx *context.Context, form auth.AvatarForm) error { } defer r.Close() - if form.Avatar.Size > setting.AvatarMaxFileSize { + if form.Avatar.Size > setting.Avatar.MaxFileSize { return errors.New(ctx.Tr("settings.uploaded_avatar_is_too_big")) } diff --git a/routers/routes/routes.go b/routers/routes/routes.go index 97f4e5aeaf..a09e53efc1 100644 --- a/routers/routes/routes.go +++ b/routers/routes/routes.go @@ -7,8 +7,10 @@ package routes import ( "bytes" "encoding/gob" + "io" "net/http" "path" + "strings" "text/template" "time" @@ -21,6 +23,7 @@ import ( "code.gitea.io/gitea/modules/options" "code.gitea.io/gitea/modules/public" "code.gitea.io/gitea/modules/setting" + "code.gitea.io/gitea/modules/storage" "code.gitea.io/gitea/modules/templates" "code.gitea.io/gitea/modules/validation" "code.gitea.io/gitea/routers" @@ -107,6 +110,61 @@ func RouterHandler(level log.Level) func(ctx *macaron.Context) { } } +func storageHandler(storageSetting setting.Storage, prefix string, objStore storage.ObjectStorage) macaron.Handler { + if storageSetting.ServeDirect { + return func(ctx *macaron.Context) { + req := ctx.Req.Request + if req.Method != "GET" && req.Method != "HEAD" { + return + } + + if !strings.HasPrefix(req.RequestURI, "/"+prefix) { + return + } + + rPath := strings.TrimPrefix(req.RequestURI, "/"+prefix) + u, err := objStore.URL(rPath, path.Base(rPath)) + if err != nil { + ctx.Error(500, err.Error()) + return + } + http.Redirect( + ctx.Resp, + req, + u.String(), + 301, + ) + } + } + + return func(ctx *macaron.Context) { + req := ctx.Req.Request + if req.Method != "GET" && req.Method != "HEAD" { + return + } + + if !strings.HasPrefix(req.RequestURI, "/"+prefix) { + return + } + + rPath := strings.TrimPrefix(req.RequestURI, "/"+prefix) + rPath = strings.TrimPrefix(rPath, "/") + //If we have matched and access to release or issue + fr, err := objStore.Open(rPath) + if err != nil { + ctx.Error(500, err.Error()) + return + } + defer fr.Close() + + _, err = io.Copy(ctx.Resp, fr) + if err != nil { + ctx.Error(500, err.Error()) + return + } + } +} + // NewMacaron initializes Macaron instance. func NewMacaron() *macaron.Macaron { gob.Register(&u2f.Challenge{}) @@ -149,22 +207,9 @@ func NewMacaron() *macaron.Macaron { ExpiresAfter: setting.StaticCacheTime, }, )) - m.Use(public.StaticHandler( - setting.AvatarUploadPath, - &public.Options{ - Prefix: "avatars", - SkipLogging: setting.DisableRouterLog, - ExpiresAfter: setting.StaticCacheTime, - }, - )) - m.Use(public.StaticHandler( - setting.RepositoryAvatarUploadPath, - &public.Options{ - Prefix: "repo-avatars", - SkipLogging: setting.DisableRouterLog, - ExpiresAfter: setting.StaticCacheTime, - }, - )) + + m.Use(storageHandler(setting.Avatar.Storage, "avatars", storage.Avatars)) + m.Use(storageHandler(setting.RepoAvatar.Storage, "repo-avatars", storage.RepoAvatars)) m.Use(templates.HTMLRenderer()) mailer.InitMailRender(templates.Mailer()) diff --git a/routers/user/setting/profile.go b/routers/user/setting/profile.go index fe0506946a..1cb00aa77f 100644 --- a/routers/user/setting/profile.go +++ b/routers/user/setting/profile.go @@ -20,7 +20,6 @@ import ( "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/setting" - "github.com/unknwon/com" "github.com/unknwon/i18n" ) @@ -133,7 +132,7 @@ func UpdateAvatarSetting(ctx *context.Context, form auth.AvatarForm, ctxUser *mo } defer fr.Close() - if form.Avatar.Size > setting.AvatarMaxFileSize { + if form.Avatar.Size > setting.Avatar.MaxFileSize { return errors.New(ctx.Tr("settings.uploaded_avatar_is_too_big")) } @@ -147,7 +146,7 @@ func UpdateAvatarSetting(ctx *context.Context, form auth.AvatarForm, ctxUser *mo if err = ctxUser.UploadAvatar(data); err != nil { return fmt.Errorf("UploadAvatar: %v", err) } - } else if ctxUser.UseCustomAvatar && !com.IsFile(ctxUser.CustomAvatarPath()) { + } else if ctxUser.UseCustomAvatar && ctxUser.Avatar == "" { // No avatar is uploaded but setting has been changed to enable, // generate a random one when needed. if err := ctxUser.GenerateRandomAvatar(); err != nil { |