diff options
author | Sergey Dryabzhinsky <sergey.dryabzhinsky+github@gmail.com> | 2019-05-30 05:22:26 +0300 |
---|---|---|
committer | techknowlogick <techknowlogick@gitea.io> | 2019-05-29 22:22:26 -0400 |
commit | 3fd18838aa5c549842e88b770b8718f693614c75 (patch) | |
tree | 614d7aeeb9419f187fa39e9ff7a664afe67ca593 /routers | |
parent | d7494046ac8aaf3b01f0eae0039e38c6c4f9c246 (diff) | |
download | gitea-3fd18838aa5c549842e88b770b8718f693614c75.tar.gz gitea-3fd18838aa5c549842e88b770b8718f693614c75.zip |
Repository avatars (#6986)
* Repository avatars
- first variant of code from old work for gogs
- add migration 87
- add new option in app.ini
- add en-US locale string
- add new class in repository.less
* Add changed index.css, remove unused template name
* Update en-us doc about configuration options
* Add comments to new functions, add new option to docker app.ini
* Add comment for lint
* Remove variable, not needed
* Fix formatting
* Update swagger api template
* Check if avatar exists
* Fix avatar link/path checks
* Typo
* TEXT column can't have a default value
* Fixes:
- remove old avatar file on upload
- use ID in name of avatar file - users may upload same files
- add simple tests
* Fix fmt check
* Generate PNG instead of "static" GIF
* More informative comment
* Fix error message
* Update avatar upload checks:
- add file size check
- add new option
- update config docs
- add new string to en-us locale
* Fixes:
- use FileHEader field for check file size
- add new test - upload big image
* Fix formatting
* Update comments
* Update log message
* Removed wrong style - not needed
* Use Sync2 to migrate
* Update repos list view
- bigger avatar
- fix html blocks alignment
* A little adjust avatar size
* Use small icons for explore/repo list
* Use new cool avatar preparation func by @lafriks
* Missing changes for new function
* Remove unused import, move imports
* Missed new option definition in app.ini
Add file size check in user/profile avatar upload
* Use smaller field length for Avatar
* Use session to update repo DB data, update DeleteAvatar - use session too
* Fix err variable definition
* As suggested @lafriks - return as soon as possible, code readability
Diffstat (limited to 'routers')
-rw-r--r-- | routers/repo/setting.go | 59 | ||||
-rw-r--r-- | routers/routes/routes.go | 11 | ||||
-rw-r--r-- | routers/user/setting/profile.go | 4 |
3 files changed, 74 insertions, 0 deletions
diff --git a/routers/repo/setting.go b/routers/repo/setting.go index f58601633a..07649982d2 100644 --- a/routers/repo/setting.go +++ b/routers/repo/setting.go @@ -7,11 +7,14 @@ package repo import ( "errors" + "fmt" + "io/ioutil" "net/url" "regexp" "strings" "time" + "github.com/Unknwon/com" "mvdan.cc/xurls/v2" "code.gitea.io/gitea/models" @@ -727,3 +730,59 @@ func init() { panic(err) } } + +// UpdateAvatarSetting update repo's avatar +func UpdateAvatarSetting(ctx *context.Context, form auth.AvatarForm) error { + ctxRepo := ctx.Repo.Repository + + if form.Avatar == nil { + // No avatar is uploaded and we not removing it here. + // No random avatar generated here. + // Just exit, no action. + if !com.IsFile(ctxRepo.CustomAvatarPath()) { + log.Trace("No avatar was uploaded for repo: %d. Default icon will appear instead.", ctxRepo.ID) + } + return nil + } + + r, err := form.Avatar.Open() + if err != nil { + return fmt.Errorf("Avatar.Open: %v", err) + } + defer r.Close() + + if form.Avatar.Size > setting.AvatarMaxFileSize { + return errors.New(ctx.Tr("settings.uploaded_avatar_is_too_big")) + } + + data, err := ioutil.ReadAll(r) + if err != nil { + return fmt.Errorf("ioutil.ReadAll: %v", err) + } + if !base.IsImageFile(data) { + return errors.New(ctx.Tr("settings.uploaded_avatar_not_a_image")) + } + if err = ctxRepo.UploadAvatar(data); err != nil { + return fmt.Errorf("UploadAvatar: %v", err) + } + return nil +} + +// SettingsAvatar save new POSTed repository avatar +func SettingsAvatar(ctx *context.Context, form auth.AvatarForm) { + form.Source = auth.AvatarLocal + if err := UpdateAvatarSetting(ctx, form); err != nil { + ctx.Flash.Error(err.Error()) + } else { + ctx.Flash.Success(ctx.Tr("repo.settings.update_avatar_success")) + } + ctx.Redirect(ctx.Repo.RepoLink + "/settings") +} + +// SettingsDeleteAvatar delete repository avatar +func SettingsDeleteAvatar(ctx *context.Context) { + if err := ctx.Repo.Repository.DeleteAvatar(); err != nil { + ctx.Flash.Error(fmt.Sprintf("DeleteAvatar: %v", err)) + } + ctx.Redirect(ctx.Repo.RepoLink + "/settings") +} diff --git a/routers/routes/routes.go b/routers/routes/routes.go index d19823714b..eb5f73768e 100644 --- a/routers/routes/routes.go +++ b/routers/routes/routes.go @@ -159,6 +159,14 @@ func NewMacaron() *macaron.Macaron { ExpiresAfter: time.Hour * 6, }, )) + m.Use(public.StaticHandler( + setting.RepositoryAvatarUploadPath, + &public.Options{ + Prefix: "repo-avatars", + SkipLogging: setting.DisableRouterLog, + ExpiresAfter: time.Hour * 6, + }, + )) m.Use(templates.HTMLRenderer()) models.InitMailRender(templates.Mailer()) @@ -613,6 +621,9 @@ func RegisterRoutes(m *macaron.Macaron) { m.Group("/settings", func() { m.Combo("").Get(repo.Settings). Post(bindIgnErr(auth.RepoSettingForm{}), repo.SettingsPost) + m.Post("/avatar", binding.MultipartForm(auth.AvatarForm{}), repo.SettingsAvatar) + m.Post("/avatar/delete", repo.SettingsDeleteAvatar) + m.Group("/collaboration", func() { m.Combo("").Get(repo.Collaboration).Post(repo.CollaborationPost) m.Post("/access_mode", repo.ChangeCollaborationAccessMode) diff --git a/routers/user/setting/profile.go b/routers/user/setting/profile.go index 85c9c83fd1..ac5c4c97fb 100644 --- a/routers/user/setting/profile.go +++ b/routers/user/setting/profile.go @@ -127,6 +127,10 @@ func UpdateAvatarSetting(ctx *context.Context, form auth.AvatarForm, ctxUser *mo } defer fr.Close() + if form.Avatar.Size > setting.AvatarMaxFileSize { + return errors.New(ctx.Tr("settings.uploaded_avatar_is_too_big")) + } + data, err := ioutil.ReadAll(fr) if err != nil { return fmt.Errorf("ioutil.ReadAll: %v", err) |