From 80a6b0f5bce15a641fc75f5f1ef6e42ef54424bc Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Wed, 14 Oct 2020 21:07:51 +0800 Subject: 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 --- modules/storage/storage.go | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) (limited to 'modules/storage/storage.go') diff --git a/modules/storage/storage.go b/modules/storage/storage.go index 8b1c336ae6..1fa04119c7 100644 --- a/modules/storage/storage.go +++ b/modules/storage/storage.go @@ -82,12 +82,32 @@ func Copy(dstStorage ObjectStorage, dstPath string, srcStorage ObjectStorage, sr 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 ( // Attachments represents attachments storage Attachments ObjectStorage // LFS represents lfs storage LFS ObjectStorage + + // Avatars represents user avatars storage + Avatars ObjectStorage + // RepoAvatars represents repository avatars storage + RepoAvatars ObjectStorage ) // Init init the stoarge @@ -96,6 +116,14 @@ func Init() error { return err } + if err := initAvatars(); err != nil { + return err + } + + if err := initRepoAvatars(); err != nil { + return err + } + return initLFS() } @@ -112,6 +140,11 @@ func NewStorage(typStr string, cfg interface{}) (ObjectStorage, error) { return fn(context.Background(), cfg) } +func initAvatars() (err error) { + Avatars, err = NewStorage(setting.Avatar.Storage.Type, setting.Avatar.Storage) + return +} + func initAttachments() (err error) { Attachments, err = NewStorage(setting.Attachment.Storage.Type, setting.Attachment.Storage) return @@ -121,3 +154,8 @@ func initLFS() (err error) { LFS, err = NewStorage(setting.LFS.Storage.Type, setting.LFS.Storage) return } + +func initRepoAvatars() (err error) { + RepoAvatars, err = NewStorage(setting.RepoAvatar.Storage.Type, setting.RepoAvatar.Storage) + return +} -- cgit v1.2.3