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
This commit is contained in:
Lunny Xiao 2020-10-14 21:07:51 +08:00 committed by GitHub
parent 93f7525061
commit 80a6b0f5bc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
21 changed files with 705 additions and 477 deletions

View File

@ -91,6 +91,20 @@ func migrateLFS(dstStorage storage.ObjectStorage) error {
}) })
} }
func migrateAvatars(dstStorage storage.ObjectStorage) error {
return models.IterateUser(func(user *models.User) error {
_, err := storage.Copy(dstStorage, user.CustomAvatarRelativePath(), storage.Avatars, user.CustomAvatarRelativePath())
return err
})
}
func migrateRepoAvatars(dstStorage storage.ObjectStorage) error {
return models.IterateRepository(func(repo *models.Repository) error {
_, err := storage.Copy(dstStorage, repo.CustomAvatarRelativePath(), storage.RepoAvatars, repo.CustomAvatarRelativePath())
return err
})
}
func runMigrateStorage(ctx *cli.Context) error { func runMigrateStorage(ctx *cli.Context) error {
if err := initDB(); err != nil { if err := initDB(); err != nil {
return err return err
@ -142,9 +156,8 @@ func runMigrateStorage(ctx *cli.Context) error {
UseSSL: ctx.Bool("minio-use-ssl"), UseSSL: ctx.Bool("minio-use-ssl"),
}) })
default: default:
return fmt.Errorf("Unsupported attachments storage type: %s", ctx.String("storage")) return fmt.Errorf("Unsupported storage type: %s", ctx.String("storage"))
} }
if err != nil { if err != nil {
return err return err
} }
@ -159,6 +172,14 @@ func runMigrateStorage(ctx *cli.Context) error {
if err := migrateLFS(dstStorage); err != nil { if err := migrateLFS(dstStorage); err != nil {
return err return err
} }
case "avatars":
if err := migrateAvatars(dstStorage); err != nil {
return err
}
case "repo-avatars":
if err := migrateRepoAvatars(dstStorage); err != nil {
return err
}
default: default:
return fmt.Errorf("Unsupported storage: %s", ctx.String("type")) return fmt.Errorf("Unsupported storage: %s", ctx.String("type"))
} }

View File

@ -564,16 +564,21 @@ Define allowed algorithms and their minimum key length (use -1 to disable a type
- `DISABLE_GRAVATAR`: **false**: Enable this to use local avatars only. - `DISABLE_GRAVATAR`: **false**: Enable this to use local avatars only.
- `ENABLE_FEDERATED_AVATAR`: **false**: Enable support for federated avatars (see - `ENABLE_FEDERATED_AVATAR`: **false**: Enable support for federated avatars (see
[http://www.libravatar.org](http://www.libravatar.org)). [http://www.libravatar.org](http://www.libravatar.org)).
- `AVATAR_STORAGE_TYPE`: **default**: Storage type defined in `[storage.xxx]`. Default is `default` which will read `[storage]` if no section `[storage]` will be a type `local`.
- `AVATAR_UPLOAD_PATH`: **data/avatars**: Path to store user avatar image files. - `AVATAR_UPLOAD_PATH`: **data/avatars**: Path to store user avatar image files.
- `AVATAR_MAX_WIDTH`: **4096**: Maximum avatar image width in pixels.
- `AVATAR_MAX_HEIGHT`: **3072**: Maximum avatar image height in pixels.
- `AVATAR_MAX_FILE_SIZE`: **1048576** (1Mb): Maximum avatar image file size in bytes.
- `REPOSITORY_AVATAR_STORAGE_TYPE`: **default**: Storage type defined in `[storage.xxx]`. Default is `default` which will read `[storage]` if no section `[storage]` will be a type `local`.
- `REPOSITORY_AVATAR_UPLOAD_PATH`: **data/repo-avatars**: Path to store repository avatar image files. - `REPOSITORY_AVATAR_UPLOAD_PATH`: **data/repo-avatars**: Path to store repository avatar image files.
- `REPOSITORY_AVATAR_FALLBACK`: **none**: How Gitea deals with missing repository avatars - `REPOSITORY_AVATAR_FALLBACK`: **none**: How Gitea deals with missing repository avatars
- none = no avatar will be displayed - none = no avatar will be displayed
- random = random avatar will be generated - random = random avatar will be generated
- image = default image will be used (which is set in `REPOSITORY_AVATAR_DEFAULT_IMAGE`) - image = default image will be used (which is set in `REPOSITORY_AVATAR_FALLBACK_IMAGE`)
- `REPOSITORY_AVATAR_FALLBACK_IMAGE`: **/img/repo_default.png**: Image used as default repository avatar (if `REPOSITORY_AVATAR_FALLBACK` is set to image and none was uploaded) - `REPOSITORY_AVATAR_FALLBACK_IMAGE`: **/img/repo_default.png**: Image used as default repository avatar (if `REPOSITORY_AVATAR_FALLBACK` is set to image and none was uploaded)
- `AVATAR_MAX_WIDTH`: **4096**: Maximum avatar image width in pixels.
- `AVATAR_MAX_HEIGHT`: **3072**: Maximum avatar image height in pixels.
- `AVATAR_MAX_FILE_SIZE`: **1048576** (1Mb): Maximum avatar image file size in bytes.
## Project (`project`) ## Project (`project`)

View File

@ -182,6 +182,20 @@ menu:
- `DISABLE_GRAVATAR`: 开启则只使用内部头像。 - `DISABLE_GRAVATAR`: 开启则只使用内部头像。
- `ENABLE_FEDERATED_AVATAR`: 启用头像联盟支持 (参见 http://www.libravatar.org) - `ENABLE_FEDERATED_AVATAR`: 启用头像联盟支持 (参见 http://www.libravatar.org)
- `AVATAR_STORAGE_TYPE`: **local**: 头像存储类型,可以为 `local``minio`,分别支持本地文件系统和 minio 兼容的API。
- `AVATAR_UPLOAD_PATH`: **data/avatars**: 存储头像的文件系统路径。
- `AVATAR_MAX_WIDTH`: **4096**: 头像最大宽度,单位像素。
- `AVATAR_MAX_HEIGHT`: **3072**: 头像最大高度,单位像素。
- `AVATAR_MAX_FILE_SIZE`: **1048576** (1Mb): 头像最大大小。
- `REPOSITORY_AVATAR_STORAGE_TYPE`: **local**: 仓库头像存储类型,可以为 `local``minio`,分别支持本地文件系统和 minio 兼容的API。
- `REPOSITORY_AVATAR_UPLOAD_PATH`: **data/repo-avatars**: 存储仓库头像的路径。
- `REPOSITORY_AVATAR_FALLBACK`: **none**: 当头像丢失时的处理方式
- none = 不显示头像
- random = 显示随机生成的头像
- image = 显示默认头像,通过 `REPOSITORY_AVATAR_FALLBACK_IMAGE` 设置
- `REPOSITORY_AVATAR_FALLBACK_IMAGE`: **/img/repo_default.png**: 默认仓库头像
## Attachment (`attachment`) ## Attachment (`attachment`)
- `ENABLED`: 是否允许用户上传附件。 - `ENABLED`: 是否允许用户上传附件。

View File

@ -58,7 +58,7 @@ LFS_MINIO_BASE_PATH = lfs/
LFS_MINIO_USE_SSL = false LFS_MINIO_USE_SSL = false
[attachment] [attachment]
STORE_TYPE = minio STORAGE_TYPE = minio
SERVE_DIRECT = false SERVE_DIRECT = false
MINIO_ENDPOINT = minio:9000 MINIO_ENDPOINT = minio:9000
MINIO_ACCESS_KEY_ID = 123456 MINIO_ACCESS_KEY_ID = 123456
@ -87,6 +87,7 @@ ENABLE_NOTIFY_MAIL = true
[picture] [picture]
DISABLE_GRAVATAR = false DISABLE_GRAVATAR = false
ENABLE_FEDERATED_AVATAR = false ENABLE_FEDERATED_AVATAR = false
AVATAR_UPLOAD_PATH = integrations/gitea-integration-mysql/data/avatars AVATAR_UPLOAD_PATH = integrations/gitea-integration-mysql/data/avatars
REPOSITORY_AVATAR_UPLOAD_PATH = integrations/gitea-integration-mysql/data/repo-avatars REPOSITORY_AVATAR_UPLOAD_PATH = integrations/gitea-integration-mysql/data/repo-avatars

View File

@ -61,7 +61,7 @@ func renameExistingUserAvatarName(x *xorm.Engine) error {
for _, user := range users { for _, user := range users {
oldAvatar := user.Avatar oldAvatar := user.Avatar
if stat, err := os.Stat(filepath.Join(setting.AvatarUploadPath, oldAvatar)); err != nil || !stat.Mode().IsRegular() { if stat, err := os.Stat(filepath.Join(setting.Avatar.Path, oldAvatar)); err != nil || !stat.Mode().IsRegular() {
if err == nil { if err == nil {
err = fmt.Errorf("Error: \"%s\" is not a regular file", oldAvatar) err = fmt.Errorf("Error: \"%s\" is not a regular file", oldAvatar)
} }
@ -86,7 +86,7 @@ func renameExistingUserAvatarName(x *xorm.Engine) error {
return fmt.Errorf("[user: %s] user table update: %v", user.LowerName, err) return fmt.Errorf("[user: %s] user table update: %v", user.LowerName, err)
} }
deleteList[filepath.Join(setting.AvatarUploadPath, oldAvatar)] = struct{}{} deleteList[filepath.Join(setting.Avatar.Path, oldAvatar)] = struct{}{}
migrated++ migrated++
select { select {
case <-ticker.C: case <-ticker.C:
@ -135,7 +135,7 @@ func renameExistingUserAvatarName(x *xorm.Engine) error {
// copyOldAvatarToNewLocation copies oldAvatar to newAvatarLocation // copyOldAvatarToNewLocation copies oldAvatar to newAvatarLocation
// and returns newAvatar location // and returns newAvatar location
func copyOldAvatarToNewLocation(userID int64, oldAvatar string) (string, error) { func copyOldAvatarToNewLocation(userID int64, oldAvatar string) (string, error) {
fr, err := os.Open(filepath.Join(setting.AvatarUploadPath, oldAvatar)) fr, err := os.Open(filepath.Join(setting.Avatar.Path, oldAvatar))
if err != nil { if err != nil {
return "", fmt.Errorf("os.Open: %v", err) return "", fmt.Errorf("os.Open: %v", err)
} }
@ -151,7 +151,7 @@ func copyOldAvatarToNewLocation(userID int64, oldAvatar string) (string, error)
return newAvatar, nil return newAvatar, nil
} }
if err := ioutil.WriteFile(filepath.Join(setting.AvatarUploadPath, newAvatar), data, 0666); err != nil { if err := ioutil.WriteFile(filepath.Join(setting.Avatar.Path, newAvatar), data, 0666); err != nil {
return "", fmt.Errorf("ioutil.WriteFile: %v", err) return "", fmt.Errorf("ioutil.WriteFile: %v", err)
} }

View File

@ -11,10 +11,10 @@ import (
"code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/storage"
"code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/modules/util" "code.gitea.io/gitea/modules/util"
"github.com/unknwon/com"
"xorm.io/builder" "xorm.io/builder"
"xorm.io/xorm" "xorm.io/xorm"
) )
@ -310,11 +310,9 @@ func deleteOrg(e *xorm.Session, u *User) error {
} }
if len(u.Avatar) > 0 { if len(u.Avatar) > 0 {
avatarPath := u.CustomAvatarPath() avatarPath := u.CustomAvatarRelativePath()
if com.IsExist(avatarPath) { if err := storage.Avatars.Delete(avatarPath); err != nil {
if err := util.Remove(avatarPath); err != nil { return fmt.Errorf("Failed to remove %s: %v", avatarPath, err)
return fmt.Errorf("Failed to remove %s: %v", avatarPath, err)
}
} }
} }

View File

@ -7,7 +7,6 @@ package models
import ( import (
"context" "context"
"crypto/md5"
"errors" "errors"
"fmt" "fmt"
"html/template" "html/template"
@ -15,7 +14,6 @@ import (
// Needed for jpeg support // Needed for jpeg support
_ "image/jpeg" _ "image/jpeg"
"image/png"
"io/ioutil" "io/ioutil"
"net" "net"
"net/url" "net/url"
@ -27,7 +25,6 @@ import (
"strings" "strings"
"time" "time"
"code.gitea.io/gitea/modules/avatar"
"code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/markup" "code.gitea.io/gitea/modules/markup"
"code.gitea.io/gitea/modules/options" "code.gitea.io/gitea/modules/options"
@ -1796,11 +1793,8 @@ func DeleteRepository(doer *User, uid, repoID int64) error {
} }
if len(repo.Avatar) > 0 { if len(repo.Avatar) > 0 {
avatarPath := repo.CustomAvatarPath() if err := storage.RepoAvatars.Delete(repo.CustomAvatarRelativePath()); err != nil {
if com.IsExist(avatarPath) { return fmt.Errorf("Failed to remove %s: %v", repo.Avatar, err)
if err := util.Remove(avatarPath); err != nil {
return fmt.Errorf("Failed to remove %s: %v", avatarPath, err)
}
} }
} }
@ -2239,187 +2233,6 @@ func (repo *Repository) GetUserFork(userID int64) (*Repository, error) {
return &forkedRepo, nil return &forkedRepo, nil
} }
// CustomAvatarPath returns repository custom avatar file path.
func (repo *Repository) CustomAvatarPath() string {
// Avatar empty by default
if len(repo.Avatar) == 0 {
return ""
}
return filepath.Join(setting.RepositoryAvatarUploadPath, repo.Avatar)
}
// generateRandomAvatar generates a random avatar for repository.
func (repo *Repository) generateRandomAvatar(e Engine) error {
idToString := fmt.Sprintf("%d", repo.ID)
seed := idToString
img, err := avatar.RandomImage([]byte(seed))
if err != nil {
return fmt.Errorf("RandomImage: %v", err)
}
repo.Avatar = idToString
if err = os.MkdirAll(filepath.Dir(repo.CustomAvatarPath()), os.ModePerm); err != nil {
return fmt.Errorf("MkdirAll: %v", err)
}
fw, err := os.Create(repo.CustomAvatarPath())
if err != nil {
return fmt.Errorf("Create: %v", err)
}
defer fw.Close()
if err = png.Encode(fw, img); err != nil {
return fmt.Errorf("Encode: %v", err)
}
log.Info("New random avatar created for repository: %d", repo.ID)
if _, err := e.ID(repo.ID).Cols("avatar").NoAutoTime().Update(repo); err != nil {
return err
}
return nil
}
// RemoveRandomAvatars removes the randomly generated avatars that were created for repositories
func RemoveRandomAvatars(ctx context.Context) error {
return x.
Where("id > 0").BufferSize(setting.Database.IterateBufferSize).
Iterate(new(Repository),
func(idx int, bean interface{}) error {
repository := bean.(*Repository)
select {
case <-ctx.Done():
return ErrCancelledf("before random avatars removed for %s", repository.FullName())
default:
}
stringifiedID := strconv.FormatInt(repository.ID, 10)
if repository.Avatar == stringifiedID {
return repository.DeleteAvatar()
}
return nil
})
}
// RelAvatarLink returns a relative link to the repository's avatar.
func (repo *Repository) RelAvatarLink() string {
return repo.relAvatarLink(x)
}
func (repo *Repository) relAvatarLink(e Engine) string {
// If no avatar - path is empty
avatarPath := repo.CustomAvatarPath()
if len(avatarPath) == 0 || !com.IsFile(avatarPath) {
switch mode := setting.RepositoryAvatarFallback; mode {
case "image":
return setting.RepositoryAvatarFallbackImage
case "random":
if err := repo.generateRandomAvatar(e); err != nil {
log.Error("generateRandomAvatar: %v", err)
}
default:
// default behaviour: do not display avatar
return ""
}
}
return setting.AppSubURL + "/repo-avatars/" + repo.Avatar
}
// AvatarLink returns a link to the repository's avatar.
func (repo *Repository) AvatarLink() string {
return repo.avatarLink(x)
}
// avatarLink returns user avatar absolute link.
func (repo *Repository) avatarLink(e Engine) string {
link := repo.relAvatarLink(e)
// link may be empty!
if len(link) > 0 {
if link[0] == '/' && link[1] != '/' {
return setting.AppURL + strings.TrimPrefix(link, setting.AppSubURL)[1:]
}
}
return link
}
// UploadAvatar saves custom avatar for repository.
// FIXME: split uploads to different subdirs in case we have massive number of repos.
func (repo *Repository) UploadAvatar(data []byte) error {
m, err := avatar.Prepare(data)
if err != nil {
return err
}
sess := x.NewSession()
defer sess.Close()
if err = sess.Begin(); err != nil {
return err
}
oldAvatarPath := repo.CustomAvatarPath()
// Users can upload the same image to other repo - prefix it with ID
// Then repo will be removed - only it avatar file will be removed
repo.Avatar = fmt.Sprintf("%d-%x", repo.ID, md5.Sum(data))
if _, err := sess.ID(repo.ID).Cols("avatar").Update(repo); err != nil {
return fmt.Errorf("UploadAvatar: Update repository avatar: %v", err)
}
if err := os.MkdirAll(setting.RepositoryAvatarUploadPath, os.ModePerm); err != nil {
return fmt.Errorf("UploadAvatar: Failed to create dir %s: %v", setting.RepositoryAvatarUploadPath, err)
}
fw, err := os.Create(repo.CustomAvatarPath())
if err != nil {
return fmt.Errorf("UploadAvatar: Create file: %v", err)
}
defer fw.Close()
if err = png.Encode(fw, *m); err != nil {
return fmt.Errorf("UploadAvatar: Encode png: %v", err)
}
if len(oldAvatarPath) > 0 && oldAvatarPath != repo.CustomAvatarPath() {
if err := util.Remove(oldAvatarPath); err != nil {
return fmt.Errorf("UploadAvatar: Failed to remove old repo avatar %s: %v", oldAvatarPath, err)
}
}
return sess.Commit()
}
// DeleteAvatar deletes the repos's custom avatar.
func (repo *Repository) DeleteAvatar() error {
// Avatar not exists
if len(repo.Avatar) == 0 {
return nil
}
avatarPath := repo.CustomAvatarPath()
log.Trace("DeleteAvatar[%d]: %s", repo.ID, avatarPath)
sess := x.NewSession()
defer sess.Close()
if err := sess.Begin(); err != nil {
return err
}
repo.Avatar = ""
if _, err := sess.ID(repo.ID).Cols("avatar").Update(repo); err != nil {
return fmt.Errorf("DeleteAvatar: Update repository avatar: %v", err)
}
if _, err := os.Stat(avatarPath); err == nil {
if err := util.Remove(avatarPath); err != nil {
return fmt.Errorf("DeleteAvatar: Failed to remove %s: %v", avatarPath, err)
}
} else {
// // Schrodinger: file may or may not exist. See err for details.
log.Trace("DeleteAvatar[%d]: %v", err)
}
return sess.Commit()
}
// GetOriginalURLHostname returns the hostname of a URL or the URL // GetOriginalURLHostname returns the hostname of a URL or the URL
func (repo *Repository) GetOriginalURLHostname() string { func (repo *Repository) GetOriginalURLHostname() string {
u, err := url.Parse(repo.OriginalURL) u, err := url.Parse(repo.OriginalURL)
@ -2502,3 +2315,25 @@ func DoctorUserStarNum() (err error) {
return return
} }
// IterateRepository iterate repositories
func IterateRepository(f func(repo *Repository) error) error {
var start int
var batchSize = setting.Database.IterateBufferSize
for {
var repos = make([]*Repository, 0, batchSize)
if err := x.Limit(batchSize, start).Find(&repos); err != nil {
return err
}
if len(repos) == 0 {
return nil
}
start += len(repos)
for _, repo := range repos {
if err := f(repo); err != nil {
return err
}
}
}
}

190
models/repo_avatar.go Normal file
View File

@ -0,0 +1,190 @@
// Copyright 2020 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package models
import (
"context"
"crypto/md5"
"fmt"
"image/png"
"io"
"strconv"
"strings"
"code.gitea.io/gitea/modules/avatar"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/storage"
)
// CustomAvatarRelativePath returns repository custom avatar file path.
func (repo *Repository) CustomAvatarRelativePath() string {
return repo.Avatar
}
// generateRandomAvatar generates a random avatar for repository.
func (repo *Repository) generateRandomAvatar(e Engine) error {
idToString := fmt.Sprintf("%d", repo.ID)
seed := idToString
img, err := avatar.RandomImage([]byte(seed))
if err != nil {
return fmt.Errorf("RandomImage: %v", err)
}
repo.Avatar = idToString
if err := storage.SaveFrom(storage.RepoAvatars, repo.CustomAvatarRelativePath(), func(w io.Writer) error {
if err := png.Encode(w, img); err != nil {
log.Error("Encode: %v", err)
}
return err
}); err != nil {
return fmt.Errorf("Failed to create dir %s: %v", repo.CustomAvatarRelativePath(), err)
}
log.Info("New random avatar created for repository: %d", repo.ID)
if _, err := e.ID(repo.ID).Cols("avatar").NoAutoTime().Update(repo); err != nil {
return err
}
return nil
}
// RemoveRandomAvatars removes the randomly generated avatars that were created for repositories
func RemoveRandomAvatars(ctx context.Context) error {
return x.
Where("id > 0").BufferSize(setting.Database.IterateBufferSize).
Iterate(new(Repository),
func(idx int, bean interface{}) error {
repository := bean.(*Repository)
select {
case <-ctx.Done():
return ErrCancelledf("before random avatars removed for %s", repository.FullName())
default:
}
stringifiedID := strconv.FormatInt(repository.ID, 10)
if repository.Avatar == stringifiedID {
return repository.DeleteAvatar()
}
return nil
})
}
// RelAvatarLink returns a relative link to the repository's avatar.
func (repo *Repository) RelAvatarLink() string {
return repo.relAvatarLink(x)
}
func (repo *Repository) relAvatarLink(e Engine) string {
// If no avatar - path is empty
avatarPath := repo.CustomAvatarRelativePath()
if len(avatarPath) == 0 {
switch mode := setting.RepoAvatar.Fallback; mode {
case "image":
return setting.RepoAvatar.FallbackImage
case "random":
if err := repo.generateRandomAvatar(e); err != nil {
log.Error("generateRandomAvatar: %v", err)
}
default:
// default behaviour: do not display avatar
return ""
}
}
return setting.AppSubURL + "/repo-avatars/" + repo.Avatar
}
// AvatarLink returns a link to the repository's avatar.
func (repo *Repository) AvatarLink() string {
return repo.avatarLink(x)
}
// avatarLink returns user avatar absolute link.
func (repo *Repository) avatarLink(e Engine) string {
link := repo.relAvatarLink(e)
// link may be empty!
if len(link) > 0 {
if link[0] == '/' && link[1] != '/' {
return setting.AppURL + strings.TrimPrefix(link, setting.AppSubURL)[1:]
}
}
return link
}
// UploadAvatar saves custom avatar for repository.
// FIXME: split uploads to different subdirs in case we have massive number of repos.
func (repo *Repository) UploadAvatar(data []byte) error {
m, err := avatar.Prepare(data)
if err != nil {
return err
}
newAvatar := fmt.Sprintf("%d-%x", repo.ID, md5.Sum(data))
if repo.Avatar == newAvatar { // upload the same picture
return nil
}
sess := x.NewSession()
defer sess.Close()
if err = sess.Begin(); err != nil {
return err
}
oldAvatarPath := repo.CustomAvatarRelativePath()
// Users can upload the same image to other repo - prefix it with ID
// Then repo will be removed - only it avatar file will be removed
repo.Avatar = newAvatar
if _, err := sess.ID(repo.ID).Cols("avatar").Update(repo); err != nil {
return fmt.Errorf("UploadAvatar: Update repository avatar: %v", err)
}
if err := storage.SaveFrom(storage.RepoAvatars, repo.CustomAvatarRelativePath(), func(w io.Writer) error {
if err := png.Encode(w, *m); err != nil {
log.Error("Encode: %v", err)
}
return err
}); err != nil {
return fmt.Errorf("UploadAvatar %s failed: Failed to remove old repo avatar %s: %v", repo.RepoPath(), newAvatar, err)
}
if len(oldAvatarPath) > 0 {
if err := storage.RepoAvatars.Delete(oldAvatarPath); err != nil {
return fmt.Errorf("UploadAvatar: Failed to remove old repo avatar %s: %v", oldAvatarPath, err)
}
}
return sess.Commit()
}
// DeleteAvatar deletes the repos's custom avatar.
func (repo *Repository) DeleteAvatar() error {
// Avatar not exists
if len(repo.Avatar) == 0 {
return nil
}
avatarPath := repo.CustomAvatarRelativePath()
log.Trace("DeleteAvatar[%d]: %s", repo.ID, avatarPath)
sess := x.NewSession()
defer sess.Close()
if err := sess.Begin(); err != nil {
return err
}
repo.Avatar = ""
if _, err := sess.ID(repo.ID).Cols("avatar").Update(repo); err != nil {
return fmt.Errorf("DeleteAvatar: Update repository avatar: %v", err)
}
if err := storage.RepoAvatars.Delete(avatarPath); err != nil {
return fmt.Errorf("DeleteAvatar: Failed to remove %s: %v", avatarPath, err)
}
return sess.Commit()
}

View File

@ -10,10 +10,10 @@ import (
"code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/storage"
"code.gitea.io/gitea/modules/util" "code.gitea.io/gitea/modules/util"
"github.com/gobwas/glob" "github.com/gobwas/glob"
"github.com/unknwon/com"
) )
// GenerateRepoOptions contains the template units to generate // GenerateRepoOptions contains the template units to generate
@ -139,7 +139,7 @@ func GenerateWebhooks(ctx DBContext, templateRepo, generateRepo *Repository) err
// GenerateAvatar generates the avatar from a template repository // GenerateAvatar generates the avatar from a template repository
func GenerateAvatar(ctx DBContext, templateRepo, generateRepo *Repository) error { func GenerateAvatar(ctx DBContext, templateRepo, generateRepo *Repository) error {
generateRepo.Avatar = strings.Replace(templateRepo.Avatar, strconv.FormatInt(templateRepo.ID, 10), strconv.FormatInt(generateRepo.ID, 10), 1) generateRepo.Avatar = strings.Replace(templateRepo.Avatar, strconv.FormatInt(templateRepo.ID, 10), strconv.FormatInt(generateRepo.ID, 10), 1)
if err := com.Copy(templateRepo.CustomAvatarPath(), generateRepo.CustomAvatarPath()); err != nil { if _, err := storage.Copy(storage.RepoAvatars, generateRepo.CustomAvatarRelativePath(), storage.RepoAvatars, templateRepo.CustomAvatarRelativePath()); err != nil {
return err return err
} }

View File

@ -70,6 +70,11 @@ func MainTest(m *testing.M, pathToGiteaRoot string) {
setting.Attachment.Storage.Path = filepath.Join(setting.AppDataPath, "attachments") setting.Attachment.Storage.Path = filepath.Join(setting.AppDataPath, "attachments")
setting.LFS.Storage.Path = filepath.Join(setting.AppDataPath, "lfs") setting.LFS.Storage.Path = filepath.Join(setting.AppDataPath, "lfs")
setting.Avatar.Storage.Path = filepath.Join(setting.AppDataPath, "avatars")
setting.RepoAvatar.Storage.Path = filepath.Join(setting.AppDataPath, "repo-avatars")
if err = storage.Init(); err != nil { if err = storage.Init(); err != nil {
fatalTestError("storage.Init: %v\n", err) fatalTestError("storage.Init: %v\n", err)
} }

View File

@ -8,29 +8,26 @@ package models
import ( import (
"container/list" "container/list"
"context" "context"
"crypto/md5"
"crypto/sha256" "crypto/sha256"
"crypto/subtle" "crypto/subtle"
"encoding/hex" "encoding/hex"
"errors" "errors"
"fmt" "fmt"
_ "image/jpeg" // Needed for jpeg support _ "image/jpeg" // Needed for jpeg support
"image/png"
"os" "os"
"path/filepath" "path/filepath"
"regexp" "regexp"
"strconv"
"strings" "strings"
"time" "time"
"unicode/utf8" "unicode/utf8"
"code.gitea.io/gitea/modules/avatar"
"code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/base"
"code.gitea.io/gitea/modules/generate" "code.gitea.io/gitea/modules/generate"
"code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/public" "code.gitea.io/gitea/modules/public"
"code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/storage"
"code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/modules/structs"
api "code.gitea.io/gitea/modules/structs" api "code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/modules/timeutil" "code.gitea.io/gitea/modules/timeutil"
@ -347,104 +344,6 @@ func (u *User) GenerateActivateCode() string {
return u.GenerateEmailActivateCode(u.Email) return u.GenerateEmailActivateCode(u.Email)
} }
// CustomAvatarPath returns user custom avatar file path.
func (u *User) CustomAvatarPath() string {
return filepath.Join(setting.AvatarUploadPath, u.Avatar)
}
// GenerateRandomAvatar generates a random avatar for user.
func (u *User) GenerateRandomAvatar() error {
return u.generateRandomAvatar(x)
}
func (u *User) generateRandomAvatar(e Engine) error {
seed := u.Email
if len(seed) == 0 {
seed = u.Name
}
img, err := avatar.RandomImage([]byte(seed))
if err != nil {
return fmt.Errorf("RandomImage: %v", err)
}
// NOTICE for random avatar, it still uses id as avatar name, but custom avatar use md5
// since random image is not a user's photo, there is no security for enumable
if u.Avatar == "" {
u.Avatar = fmt.Sprintf("%d", u.ID)
}
if err = os.MkdirAll(filepath.Dir(u.CustomAvatarPath()), os.ModePerm); err != nil {
return fmt.Errorf("MkdirAll: %v", err)
}
fw, err := os.Create(u.CustomAvatarPath())
if err != nil {
return fmt.Errorf("Create: %v", err)
}
defer fw.Close()
if _, err := e.ID(u.ID).Cols("avatar").Update(u); err != nil {
return err
}
if err = png.Encode(fw, img); err != nil {
return fmt.Errorf("Encode: %v", err)
}
log.Info("New random avatar created: %d", u.ID)
return nil
}
// SizedRelAvatarLink returns a link to the user's avatar via
// the local explore page. Function returns immediately.
// When applicable, the link is for an avatar of the indicated size (in pixels).
func (u *User) SizedRelAvatarLink(size int) string {
return strings.TrimSuffix(setting.AppSubURL, "/") + "/user/avatar/" + u.Name + "/" + strconv.Itoa(size)
}
// RealSizedAvatarLink returns a link to the user's avatar. When
// applicable, the link is for an avatar of the indicated size (in pixels).
//
// This function make take time to return when federated avatars
// are in use, due to a DNS lookup need
//
func (u *User) RealSizedAvatarLink(size int) string {
if u.ID == -1 {
return base.DefaultAvatarLink()
}
switch {
case u.UseCustomAvatar:
if !com.IsFile(u.CustomAvatarPath()) {
return base.DefaultAvatarLink()
}
return setting.AppSubURL + "/avatars/" + u.Avatar
case setting.DisableGravatar, setting.OfflineMode:
if !com.IsFile(u.CustomAvatarPath()) {
if err := u.GenerateRandomAvatar(); err != nil {
log.Error("GenerateRandomAvatar: %v", err)
}
}
return setting.AppSubURL + "/avatars/" + u.Avatar
}
return base.SizedAvatarLink(u.AvatarEmail, size)
}
// RelAvatarLink returns a relative link to the user's avatar. The link
// may either be a sub-URL to this site, or a full URL to an external avatar
// service.
func (u *User) RelAvatarLink() string {
return u.SizedRelAvatarLink(base.DefaultAvatarSize)
}
// AvatarLink returns user avatar absolute link.
func (u *User) AvatarLink() string {
link := u.RelAvatarLink()
if link[0] == '/' && link[1] != '/' {
return setting.AppURL + strings.TrimPrefix(link, setting.AppSubURL)[1:]
}
return link
}
// GetFollowers returns range of user's followers. // GetFollowers returns range of user's followers.
func (u *User) GetFollowers(listOptions ListOptions) ([]*User, error) { func (u *User) GetFollowers(listOptions ListOptions) ([]*User, error) {
sess := x. sess := x.
@ -537,64 +436,6 @@ func (u *User) IsPasswordSet() bool {
return !u.ValidatePassword("") return !u.ValidatePassword("")
} }
// UploadAvatar saves custom avatar for user.
// FIXME: split uploads to different subdirs in case we have massive users.
func (u *User) UploadAvatar(data []byte) error {
m, err := avatar.Prepare(data)
if err != nil {
return err
}
sess := x.NewSession()
defer sess.Close()
if err = sess.Begin(); err != nil {
return err
}
u.UseCustomAvatar = true
// Different users can upload same image as avatar
// If we prefix it with u.ID, it will be separated
// Otherwise, if any of the users delete his avatar
// Other users will lose their avatars too.
u.Avatar = fmt.Sprintf("%x", md5.Sum([]byte(fmt.Sprintf("%d-%x", u.ID, md5.Sum(data)))))
if err = updateUser(sess, u); err != nil {
return fmt.Errorf("updateUser: %v", err)
}
if err := os.MkdirAll(setting.AvatarUploadPath, os.ModePerm); err != nil {
return fmt.Errorf("Failed to create dir %s: %v", setting.AvatarUploadPath, err)
}
fw, err := os.Create(u.CustomAvatarPath())
if err != nil {
return fmt.Errorf("Create: %v", err)
}
defer fw.Close()
if err = png.Encode(fw, *m); err != nil {
return fmt.Errorf("Encode: %v", err)
}
return sess.Commit()
}
// DeleteAvatar deletes the user's custom avatar.
func (u *User) DeleteAvatar() error {
log.Trace("DeleteAvatar[%d]: %s", u.ID, u.CustomAvatarPath())
if len(u.Avatar) > 0 {
if err := util.Remove(u.CustomAvatarPath()); err != nil {
return fmt.Errorf("Failed to remove %s: %v", u.CustomAvatarPath(), err)
}
}
u.UseCustomAvatar = false
u.Avatar = ""
if _, err := x.ID(u.ID).Cols("avatar, use_custom_avatar").Update(u); err != nil {
return fmt.Errorf("UpdateUser: %v", err)
}
return nil
}
// IsOrganization returns true if user is actually a organization. // IsOrganization returns true if user is actually a organization.
func (u *User) IsOrganization() bool { func (u *User) IsOrganization() bool {
return u.Type == UserTypeOrganization return u.Type == UserTypeOrganization
@ -1285,17 +1126,14 @@ func deleteUser(e *xorm.Session, u *User) error {
// Note: There are something just cannot be roll back, // Note: There are something just cannot be roll back,
// so just keep error logs of those operations. // so just keep error logs of those operations.
path := UserPath(u.Name) path := UserPath(u.Name)
if err := util.RemoveAll(path); err != nil { if err := util.RemoveAll(path); err != nil {
return fmt.Errorf("Failed to RemoveAll %s: %v", path, err) return fmt.Errorf("Failed to RemoveAll %s: %v", path, err)
} }
if len(u.Avatar) > 0 { if len(u.Avatar) > 0 {
avatarPath := u.CustomAvatarPath() avatarPath := u.CustomAvatarRelativePath()
if com.IsExist(avatarPath) { if err := storage.Avatars.Delete(avatarPath); err != nil {
if err := util.Remove(avatarPath); err != nil { return fmt.Errorf("Failed to remove %s: %v", avatarPath, err)
return fmt.Errorf("Failed to remove %s: %v", avatarPath, err)
}
} }
} }
@ -2034,3 +1872,25 @@ func SyncExternalUsers(ctx context.Context, updateExisting bool) error {
} }
return nil return nil
} }
// IterateUser iterate users
func IterateUser(f func(user *User) error) error {
var start int
var batchSize = setting.Database.IterateBufferSize
for {
var users = make([]*User, 0, batchSize)
if err := x.Limit(batchSize, start).Find(&users); err != nil {
return err
}
if len(users) == 0 {
return nil
}
start += len(users)
for _, user := range users {
if err := f(user); err != nil {
return err
}
}
}
}

169
models/user_avatar.go Normal file
View File

@ -0,0 +1,169 @@
// Copyright 2020 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package models
import (
"crypto/md5"
"fmt"
"image/png"
"io"
"strconv"
"strings"
"code.gitea.io/gitea/modules/avatar"
"code.gitea.io/gitea/modules/base"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/storage"
)
// CustomAvatarRelativePath returns user custom avatar relative path.
func (u *User) CustomAvatarRelativePath() string {
return u.Avatar
}
// GenerateRandomAvatar generates a random avatar for user.
func (u *User) GenerateRandomAvatar() error {
return u.generateRandomAvatar(x)
}
func (u *User) generateRandomAvatar(e Engine) error {
seed := u.Email
if len(seed) == 0 {
seed = u.Name
}
img, err := avatar.RandomImage([]byte(seed))
if err != nil {
return fmt.Errorf("RandomImage: %v", err)
}
// NOTICE for random avatar, it still uses id as avatar name, but custom avatar use md5
// since random image is not a user's photo, there is no security for enumable
if u.Avatar == "" {
u.Avatar = fmt.Sprintf("%d", u.ID)
}
if err := storage.SaveFrom(storage.Avatars, u.CustomAvatarRelativePath(), func(w io.Writer) error {
if err := png.Encode(w, img); err != nil {
log.Error("Encode: %v", err)
}
return err
}); err != nil {
return fmt.Errorf("Failed to create dir %s: %v", u.CustomAvatarRelativePath(), err)
}
if _, err := e.ID(u.ID).Cols("avatar").Update(u); err != nil {
return err
}
log.Info("New random avatar created: %d", u.ID)
return nil
}
// SizedRelAvatarLink returns a link to the user's avatar via
// the local explore page. Function returns immediately.
// When applicable, the link is for an avatar of the indicated size (in pixels).
func (u *User) SizedRelAvatarLink(size int) string {
return strings.TrimSuffix(setting.AppSubURL, "/") + "/user/avatar/" + u.Name + "/" + strconv.Itoa(size)
}
// RealSizedAvatarLink returns a link to the user's avatar. When
// applicable, the link is for an avatar of the indicated size (in pixels).
//
// This function make take time to return when federated avatars
// are in use, due to a DNS lookup need
//
func (u *User) RealSizedAvatarLink(size int) string {
if u.ID == -1 {
return base.DefaultAvatarLink()
}
switch {
case u.UseCustomAvatar:
if u.Avatar == "" {
return base.DefaultAvatarLink()
}
return setting.AppSubURL + "/avatars/" + u.Avatar
case setting.DisableGravatar, setting.OfflineMode:
if u.Avatar == "" {
if err := u.GenerateRandomAvatar(); err != nil {
log.Error("GenerateRandomAvatar: %v", err)
}
}
return setting.AppSubURL + "/avatars/" + u.Avatar
}
return base.SizedAvatarLink(u.AvatarEmail, size)
}
// RelAvatarLink returns a relative link to the user's avatar. The link
// may either be a sub-URL to this site, or a full URL to an external avatar
// service.
func (u *User) RelAvatarLink() string {
return u.SizedRelAvatarLink(base.DefaultAvatarSize)
}
// AvatarLink returns user avatar absolute link.
func (u *User) AvatarLink() string {
link := u.RelAvatarLink()
if link[0] == '/' && link[1] != '/' {
return setting.AppURL + strings.TrimPrefix(link, setting.AppSubURL)[1:]
}
return link
}
// UploadAvatar saves custom avatar for user.
// FIXME: split uploads to different subdirs in case we have massive users.
func (u *User) UploadAvatar(data []byte) error {
m, err := avatar.Prepare(data)
if err != nil {
return err
}
sess := x.NewSession()
defer sess.Close()
if err = sess.Begin(); err != nil {
return err
}
u.UseCustomAvatar = true
// Different users can upload same image as avatar
// If we prefix it with u.ID, it will be separated
// Otherwise, if any of the users delete his avatar
// Other users will lose their avatars too.
u.Avatar = fmt.Sprintf("%x", md5.Sum([]byte(fmt.Sprintf("%d-%x", u.ID, md5.Sum(data)))))
if err = updateUser(sess, u); err != nil {
return fmt.Errorf("updateUser: %v", err)
}
if err := storage.SaveFrom(storage.Avatars, u.CustomAvatarRelativePath(), func(w io.Writer) error {
if err := png.Encode(w, *m); err != nil {
log.Error("Encode: %v", err)
}
return err
}); err != nil {
return fmt.Errorf("Failed to create dir %s: %v", u.CustomAvatarRelativePath(), err)
}
return sess.Commit()
}
// DeleteAvatar deletes the user's custom avatar.
func (u *User) DeleteAvatar() error {
aPath := u.CustomAvatarRelativePath()
log.Trace("DeleteAvatar[%d]: %s", u.ID, aPath)
if len(u.Avatar) > 0 {
if err := storage.Avatars.Delete(aPath); err != nil {
return fmt.Errorf("Failed to remove %s: %v", aPath, err)
}
}
u.UseCustomAvatar = false
u.Avatar = ""
if _, err := x.ID(u.ID).Cols("avatar, use_custom_avatar").Update(u); err != nil {
return fmt.Errorf("UpdateUser: %v", err)
}
return nil
}

View File

@ -9,6 +9,7 @@ import (
"fmt" "fmt"
"image" "image"
"image/color/palette" "image/color/palette"
// Enable PNG support: // Enable PNG support:
_ "image/png" _ "image/png"
"math/rand" "math/rand"
@ -57,11 +58,11 @@ func Prepare(data []byte) (*image.Image, error) {
if err != nil { if err != nil {
return nil, fmt.Errorf("DecodeConfig: %v", err) return nil, fmt.Errorf("DecodeConfig: %v", err)
} }
if imgCfg.Width > setting.AvatarMaxWidth { if imgCfg.Width > setting.Avatar.MaxWidth {
return nil, fmt.Errorf("Image width is too large: %d > %d", imgCfg.Width, setting.AvatarMaxWidth) return nil, fmt.Errorf("Image width is too large: %d > %d", imgCfg.Width, setting.Avatar.MaxWidth)
} }
if imgCfg.Height > setting.AvatarMaxHeight { if imgCfg.Height > setting.Avatar.MaxHeight {
return nil, fmt.Errorf("Image height is too large: %d > %d", imgCfg.Height, setting.AvatarMaxHeight) return nil, fmt.Errorf("Image height is too large: %d > %d", imgCfg.Height, setting.Avatar.MaxHeight)
} }
img, _, err := image.Decode(bytes.NewReader(data)) img, _, err := image.Decode(bytes.NewReader(data))

View File

@ -22,8 +22,8 @@ func Test_RandomImage(t *testing.T) {
} }
func Test_PrepareWithPNG(t *testing.T) { func Test_PrepareWithPNG(t *testing.T) {
setting.AvatarMaxWidth = 4096 setting.Avatar.MaxWidth = 4096
setting.AvatarMaxHeight = 4096 setting.Avatar.MaxHeight = 4096
data, err := ioutil.ReadFile("testdata/avatar.png") data, err := ioutil.ReadFile("testdata/avatar.png")
assert.NoError(t, err) assert.NoError(t, err)
@ -36,8 +36,8 @@ func Test_PrepareWithPNG(t *testing.T) {
} }
func Test_PrepareWithJPEG(t *testing.T) { func Test_PrepareWithJPEG(t *testing.T) {
setting.AvatarMaxWidth = 4096 setting.Avatar.MaxWidth = 4096
setting.AvatarMaxHeight = 4096 setting.Avatar.MaxHeight = 4096
data, err := ioutil.ReadFile("testdata/avatar.jpeg") data, err := ioutil.ReadFile("testdata/avatar.jpeg")
assert.NoError(t, err) assert.NoError(t, err)
@ -50,15 +50,15 @@ func Test_PrepareWithJPEG(t *testing.T) {
} }
func Test_PrepareWithInvalidImage(t *testing.T) { func Test_PrepareWithInvalidImage(t *testing.T) {
setting.AvatarMaxWidth = 5 setting.Avatar.MaxWidth = 5
setting.AvatarMaxHeight = 5 setting.Avatar.MaxHeight = 5
_, err := Prepare([]byte{}) _, err := Prepare([]byte{})
assert.EqualError(t, err, "DecodeConfig: image: unknown format") assert.EqualError(t, err, "DecodeConfig: image: unknown format")
} }
func Test_PrepareWithInvalidImageSize(t *testing.T) { func Test_PrepareWithInvalidImageSize(t *testing.T) {
setting.AvatarMaxWidth = 5 setting.Avatar.MaxWidth = 5
setting.AvatarMaxHeight = 5 setting.Avatar.MaxHeight = 5
data, err := ioutil.ReadFile("testdata/avatar.png") data, err := ioutil.ReadFile("testdata/avatar.png")
assert.NoError(t, err) assert.NoError(t, err)

View File

@ -47,7 +47,8 @@ var (
ConnMaxLifetime time.Duration ConnMaxLifetime time.Duration
IterateBufferSize int IterateBufferSize int
}{ }{
Timeout: 500, Timeout: 500,
IterateBufferSize: 50,
} }
) )

114
modules/setting/picture.go Normal file
View File

@ -0,0 +1,114 @@
// Copyright 2020 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package setting
import (
"net/url"
"code.gitea.io/gitea/modules/log"
"strk.kbt.io/projects/go/libravatar"
)
// settings
var (
// Picture settings
Avatar = struct {
Storage
MaxWidth int
MaxHeight int
MaxFileSize int64
}{
MaxWidth: 4096,
MaxHeight: 3072,
MaxFileSize: 1048576,
}
GravatarSource string
GravatarSourceURL *url.URL
DisableGravatar bool
EnableFederatedAvatar bool
LibravatarService *libravatar.Libravatar
RepoAvatar = struct {
Storage
Fallback string
FallbackImage string
}{}
)
func newPictureService() {
sec := Cfg.Section("picture")
avatarSec := Cfg.Section("avatar")
storageType := sec.Key("AVATAR_STORAGE_TYPE").MustString("")
// Specifically default PATH to AVATAR_UPLOAD_PATH
avatarSec.Key("PATH").MustString(
sec.Key("AVATAR_UPLOAD_PATH").String())
Avatar.Storage = getStorage("avatars", storageType, avatarSec)
Avatar.MaxWidth = sec.Key("AVATAR_MAX_WIDTH").MustInt(4096)
Avatar.MaxHeight = sec.Key("AVATAR_MAX_HEIGHT").MustInt(3072)
Avatar.MaxFileSize = sec.Key("AVATAR_MAX_FILE_SIZE").MustInt64(1048576)
switch source := sec.Key("GRAVATAR_SOURCE").MustString("gravatar"); source {
case "duoshuo":
GravatarSource = "http://gravatar.duoshuo.com/avatar/"
case "gravatar":
GravatarSource = "https://secure.gravatar.com/avatar/"
case "libravatar":
GravatarSource = "https://seccdn.libravatar.org/avatar/"
default:
GravatarSource = source
}
DisableGravatar = sec.Key("DISABLE_GRAVATAR").MustBool()
EnableFederatedAvatar = sec.Key("ENABLE_FEDERATED_AVATAR").MustBool(!InstallLock)
if OfflineMode {
DisableGravatar = true
EnableFederatedAvatar = false
}
if DisableGravatar {
EnableFederatedAvatar = false
}
if EnableFederatedAvatar || !DisableGravatar {
var err error
GravatarSourceURL, err = url.Parse(GravatarSource)
if err != nil {
log.Fatal("Failed to parse Gravatar URL(%s): %v",
GravatarSource, err)
}
}
if EnableFederatedAvatar {
LibravatarService = libravatar.New()
if GravatarSourceURL.Scheme == "https" {
LibravatarService.SetUseHTTPS(true)
LibravatarService.SetSecureFallbackHost(GravatarSourceURL.Host)
} else {
LibravatarService.SetUseHTTPS(false)
LibravatarService.SetFallbackHost(GravatarSourceURL.Host)
}
}
newRepoAvatarService()
}
func newRepoAvatarService() {
sec := Cfg.Section("picture")
repoAvatarSec := Cfg.Section("repo-avatar")
storageType := sec.Key("REPOSITORY_AVATAR_STORAGE_TYPE").MustString("")
// Specifically default PATH to AVATAR_UPLOAD_PATH
repoAvatarSec.Key("PATH").MustString(
sec.Key("REPOSITORY_AVATAR_UPLOAD_PATH").String())
RepoAvatar.Storage = getStorage("repo-avatars", storageType, repoAvatarSec)
RepoAvatar.Fallback = sec.Key("REPOSITORY_AVATAR_FALLBACK").MustString("none")
RepoAvatar.FallbackImage = sec.Key("REPOSITORY_AVATAR_FALLBACK_IMAGE").MustString("/img/repo_default.png")
}

View File

@ -30,7 +30,6 @@ import (
"github.com/unknwon/com" "github.com/unknwon/com"
gossh "golang.org/x/crypto/ssh" gossh "golang.org/x/crypto/ssh"
ini "gopkg.in/ini.v1" ini "gopkg.in/ini.v1"
"strk.kbt.io/projects/go/libravatar"
) )
// Scheme describes protocol types // Scheme describes protocol types
@ -272,20 +271,6 @@ var (
DefaultEmailNotification string DefaultEmailNotification string
} }
// Picture settings
AvatarUploadPath string
AvatarMaxWidth int
AvatarMaxHeight int
GravatarSource string
GravatarSourceURL *url.URL
DisableGravatar bool
EnableFederatedAvatar bool
LibravatarService *libravatar.Libravatar
AvatarMaxFileSize int64
RepositoryAvatarUploadPath string
RepositoryAvatarFallback string
RepositoryAvatarFallbackImage string
// Log settings // Log settings
LogLevel string LogLevel string
StacktraceLogLevel string StacktraceLogLevel string
@ -864,59 +849,7 @@ func NewContext() {
newRepository() newRepository()
sec = Cfg.Section("picture") newPictureService()
AvatarUploadPath = sec.Key("AVATAR_UPLOAD_PATH").MustString(path.Join(AppDataPath, "avatars"))
forcePathSeparator(AvatarUploadPath)
if !filepath.IsAbs(AvatarUploadPath) {
AvatarUploadPath = path.Join(AppWorkPath, AvatarUploadPath)
}
RepositoryAvatarUploadPath = sec.Key("REPOSITORY_AVATAR_UPLOAD_PATH").MustString(path.Join(AppDataPath, "repo-avatars"))
forcePathSeparator(RepositoryAvatarUploadPath)
if !filepath.IsAbs(RepositoryAvatarUploadPath) {
RepositoryAvatarUploadPath = path.Join(AppWorkPath, RepositoryAvatarUploadPath)
}
RepositoryAvatarFallback = sec.Key("REPOSITORY_AVATAR_FALLBACK").MustString("none")
RepositoryAvatarFallbackImage = sec.Key("REPOSITORY_AVATAR_FALLBACK_IMAGE").MustString("/img/repo_default.png")
AvatarMaxWidth = sec.Key("AVATAR_MAX_WIDTH").MustInt(4096)
AvatarMaxHeight = sec.Key("AVATAR_MAX_HEIGHT").MustInt(3072)
AvatarMaxFileSize = sec.Key("AVATAR_MAX_FILE_SIZE").MustInt64(1048576)
switch source := sec.Key("GRAVATAR_SOURCE").MustString("gravatar"); source {
case "duoshuo":
GravatarSource = "http://gravatar.duoshuo.com/avatar/"
case "gravatar":
GravatarSource = "https://secure.gravatar.com/avatar/"
case "libravatar":
GravatarSource = "https://seccdn.libravatar.org/avatar/"
default:
GravatarSource = source
}
DisableGravatar = sec.Key("DISABLE_GRAVATAR").MustBool()
EnableFederatedAvatar = sec.Key("ENABLE_FEDERATED_AVATAR").MustBool(!InstallLock)
if OfflineMode {
DisableGravatar = true
EnableFederatedAvatar = false
}
if DisableGravatar {
EnableFederatedAvatar = false
}
if EnableFederatedAvatar || !DisableGravatar {
GravatarSourceURL, err = url.Parse(GravatarSource)
if err != nil {
log.Fatal("Failed to parse Gravatar URL(%s): %v",
GravatarSource, err)
}
}
if EnableFederatedAvatar {
LibravatarService = libravatar.New()
if GravatarSourceURL.Scheme == "https" {
LibravatarService.SetUseHTTPS(true)
LibravatarService.SetSecureFallbackHost(GravatarSourceURL.Host)
} else {
LibravatarService.SetUseHTTPS(false)
LibravatarService.SetFallbackHost(GravatarSourceURL.Host)
}
}
if err = Cfg.Section("ui").MapTo(&UI); err != nil { if err = Cfg.Section("ui").MapTo(&UI); err != nil {
log.Fatal("Failed to map UI settings: %v", err) log.Fatal("Failed to map UI settings: %v", err)

View File

@ -82,12 +82,32 @@ func Copy(dstStorage ObjectStorage, dstPath string, srcStorage ObjectStorage, sr
return dstStorage.Save(dstPath, f) return dstStorage.Save(dstPath, f)
} }
// SaveFrom saves data to the ObjectStorage with path p from the callback
func SaveFrom(objStorage ObjectStorage, p string, callback func(w io.Writer) error) error {
pr, pw := io.Pipe()
defer pr.Close()
go func() {
defer pw.Close()
if err := callback(pw); err != nil {
_ = pw.CloseWithError(err)
}
}()
_, err := objStorage.Save(p, pr)
return err
}
var ( var (
// Attachments represents attachments storage // Attachments represents attachments storage
Attachments ObjectStorage Attachments ObjectStorage
// LFS represents lfs storage // LFS represents lfs storage
LFS ObjectStorage LFS ObjectStorage
// Avatars represents user avatars storage
Avatars ObjectStorage
// RepoAvatars represents repository avatars storage
RepoAvatars ObjectStorage
) )
// Init init the stoarge // Init init the stoarge
@ -96,6 +116,14 @@ func Init() error {
return err return err
} }
if err := initAvatars(); err != nil {
return err
}
if err := initRepoAvatars(); err != nil {
return err
}
return initLFS() return initLFS()
} }
@ -112,6 +140,11 @@ func NewStorage(typStr string, cfg interface{}) (ObjectStorage, error) {
return fn(context.Background(), cfg) return fn(context.Background(), cfg)
} }
func initAvatars() (err error) {
Avatars, err = NewStorage(setting.Avatar.Storage.Type, setting.Avatar.Storage)
return
}
func initAttachments() (err error) { func initAttachments() (err error) {
Attachments, err = NewStorage(setting.Attachment.Storage.Type, setting.Attachment.Storage) Attachments, err = NewStorage(setting.Attachment.Storage.Type, setting.Attachment.Storage)
return return
@ -121,3 +154,8 @@ func initLFS() (err error) {
LFS, err = NewStorage(setting.LFS.Storage.Type, setting.LFS.Storage) LFS, err = NewStorage(setting.LFS.Storage.Type, setting.LFS.Storage)
return return
} }
func initRepoAvatars() (err error) {
RepoAvatars, err = NewStorage(setting.RepoAvatar.Storage.Type, setting.RepoAvatar.Storage)
return
}

View File

@ -30,7 +30,6 @@ import (
mirror_service "code.gitea.io/gitea/services/mirror" mirror_service "code.gitea.io/gitea/services/mirror"
repo_service "code.gitea.io/gitea/services/repository" repo_service "code.gitea.io/gitea/services/repository"
"github.com/unknwon/com"
"mvdan.cc/xurls/v2" "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 avatar is uploaded and we not removing it here.
// No random avatar generated here. // No random avatar generated here.
// Just exit, no action. // 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) log.Trace("No avatar was uploaded for repo: %d. Default icon will appear instead.", ctxRepo.ID)
} }
return nil return nil
@ -940,7 +939,7 @@ func UpdateAvatarSetting(ctx *context.Context, form auth.AvatarForm) error {
} }
defer r.Close() 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")) return errors.New(ctx.Tr("settings.uploaded_avatar_is_too_big"))
} }

View File

@ -7,8 +7,10 @@ package routes
import ( import (
"bytes" "bytes"
"encoding/gob" "encoding/gob"
"io"
"net/http" "net/http"
"path" "path"
"strings"
"text/template" "text/template"
"time" "time"
@ -21,6 +23,7 @@ import (
"code.gitea.io/gitea/modules/options" "code.gitea.io/gitea/modules/options"
"code.gitea.io/gitea/modules/public" "code.gitea.io/gitea/modules/public"
"code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/storage"
"code.gitea.io/gitea/modules/templates" "code.gitea.io/gitea/modules/templates"
"code.gitea.io/gitea/modules/validation" "code.gitea.io/gitea/modules/validation"
"code.gitea.io/gitea/routers" "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. // NewMacaron initializes Macaron instance.
func NewMacaron() *macaron.Macaron { func NewMacaron() *macaron.Macaron {
gob.Register(&u2f.Challenge{}) gob.Register(&u2f.Challenge{})
@ -149,22 +207,9 @@ func NewMacaron() *macaron.Macaron {
ExpiresAfter: setting.StaticCacheTime, ExpiresAfter: setting.StaticCacheTime,
}, },
)) ))
m.Use(public.StaticHandler(
setting.AvatarUploadPath, m.Use(storageHandler(setting.Avatar.Storage, "avatars", storage.Avatars))
&public.Options{ m.Use(storageHandler(setting.RepoAvatar.Storage, "repo-avatars", storage.RepoAvatars))
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(templates.HTMLRenderer()) m.Use(templates.HTMLRenderer())
mailer.InitMailRender(templates.Mailer()) mailer.InitMailRender(templates.Mailer())

View File

@ -20,7 +20,6 @@ import (
"code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/setting"
"github.com/unknwon/com"
"github.com/unknwon/i18n" "github.com/unknwon/i18n"
) )
@ -133,7 +132,7 @@ func UpdateAvatarSetting(ctx *context.Context, form auth.AvatarForm, ctxUser *mo
} }
defer fr.Close() 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")) 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 { if err = ctxUser.UploadAvatar(data); err != nil {
return fmt.Errorf("UploadAvatar: %v", err) 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, // No avatar is uploaded but setting has been changed to enable,
// generate a random one when needed. // generate a random one when needed.
if err := ctxUser.GenerateRandomAvatar(); err != nil { if err := ctxUser.GenerateRandomAvatar(); err != nil {