Skip to content

Commit 5da8a84

Browse files
zeripathlunny
andauthored
Fix Storage mapping (#13297) (#13307)
* Fix Storage mapping (#13297) Backport #13297 This PR fixes several bugs in setting storage * The default STORAGE_TYPE should be the provided type. * The Storage config should be passed in to NewStorage as a pointer - otherwise the Mappable interface function MapTo will not be found * There was a bug in the MapTo function. Fix #13286 Signed-off-by: Andrew Thornton <art27@cantab.net> * add missing changes from backport #13164 Signed-off-by: Andrew Thornton <art27@cantab.net> Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
1 parent d795bfc commit 5da8a84

File tree

4 files changed

+29
-17
lines changed

4 files changed

+29
-17
lines changed

modules/setting/storage.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ type Storage struct {
2121

2222
// MapTo implements the Mappable interface
2323
func (s *Storage) MapTo(v interface{}) error {
24-
pathValue := reflect.ValueOf(v).FieldByName("Path")
24+
pathValue := reflect.ValueOf(v).Elem().FieldByName("Path")
2525
if pathValue.IsValid() && pathValue.Kind() == reflect.String {
2626
pathValue.SetString(s.Path)
2727
}
@@ -46,7 +46,7 @@ func getStorage(name, typ string, overrides ...*ini.Section) Storage {
4646

4747
var storage Storage
4848

49-
storage.Type = sec.Key("STORAGE_TYPE").MustString("")
49+
storage.Type = sec.Key("STORAGE_TYPE").MustString(typ)
5050
storage.ServeDirect = sec.Key("SERVE_DIRECT").MustBool(false)
5151

5252
// Global Defaults

modules/storage/local.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"os"
1212
"path/filepath"
1313

14+
"code.gitea.io/gitea/modules/log"
1415
"code.gitea.io/gitea/modules/util"
1516
)
1617

@@ -39,7 +40,7 @@ func NewLocalStorage(ctx context.Context, cfg interface{}) (ObjectStorage, error
3940
return nil, err
4041
}
4142
config := configInterface.(LocalStorageConfig)
42-
43+
log.Info("Creating new Local Storage at %s", config.Path)
4344
if err := os.MkdirAll(config.Path, os.ModePerm); err != nil {
4445
return nil, err
4546
}

modules/storage/minio.go

+16-10
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"strings"
1414
"time"
1515

16+
"code.gitea.io/gitea/modules/log"
1617
"github.com/minio/minio-go/v7"
1718
"github.com/minio/minio-go/v7/pkg/credentials"
1819
)
@@ -82,16 +83,17 @@ func convertMinioErr(err error) error {
8283
func NewMinioStorage(ctx context.Context, cfg interface{}) (ObjectStorage, error) {
8384
configInterface, err := toConfig(MinioStorageConfig{}, cfg)
8485
if err != nil {
85-
return nil, err
86+
return nil, convertMinioErr(err)
8687
}
8788
config := configInterface.(MinioStorageConfig)
8889

90+
log.Info("Creating Minio storage at %s:%s with base path %s", config.Endpoint, config.Bucket, config.BasePath)
8991
minioClient, err := minio.New(config.Endpoint, &minio.Options{
9092
Creds: credentials.NewStaticV4(config.AccessKeyID, config.SecretAccessKey, ""),
9193
Secure: config.UseSSL,
9294
})
9395
if err != nil {
94-
return nil, err
96+
return nil, convertMinioErr(err)
9597
}
9698

9799
if err := minioClient.MakeBucket(ctx, config.Bucket, minio.MakeBucketOptions{
@@ -100,7 +102,7 @@ func NewMinioStorage(ctx context.Context, cfg interface{}) (ObjectStorage, error
100102
// Check to see if we already own this bucket (which happens if you run this twice)
101103
exists, errBucketExists := minioClient.BucketExists(ctx, config.Bucket)
102104
if !exists || errBucketExists != nil {
103-
return nil, err
105+
return nil, convertMinioErr(err)
104106
}
105107
}
106108

@@ -121,7 +123,7 @@ func (m *MinioStorage) Open(path string) (Object, error) {
121123
var opts = minio.GetObjectOptions{}
122124
object, err := m.client.GetObject(m.ctx, m.bucket, m.buildMinioPath(path), opts)
123125
if err != nil {
124-
return nil, err
126+
return nil, convertMinioErr(err)
125127
}
126128
return &minioObject{object}, nil
127129
}
@@ -137,7 +139,7 @@ func (m *MinioStorage) Save(path string, r io.Reader) (int64, error) {
137139
minio.PutObjectOptions{ContentType: "application/octet-stream"},
138140
)
139141
if err != nil {
140-
return 0, err
142+
return 0, convertMinioErr(err)
141143
}
142144
return uploadInfo.Size, nil
143145
}
@@ -184,22 +186,26 @@ func (m *MinioStorage) Stat(path string) (os.FileInfo, error) {
184186
return nil, os.ErrNotExist
185187
}
186188
}
187-
return nil, err
189+
return nil, convertMinioErr(err)
188190
}
189191
return &minioFileInfo{info}, nil
190192
}
191193

192194
// Delete delete a file
193195
func (m *MinioStorage) Delete(path string) error {
194-
return m.client.RemoveObject(m.ctx, m.bucket, m.buildMinioPath(path), minio.RemoveObjectOptions{})
196+
if err := m.client.RemoveObject(m.ctx, m.bucket, m.buildMinioPath(path), minio.RemoveObjectOptions{}); err != nil {
197+
return convertMinioErr(err)
198+
}
199+
return nil
195200
}
196201

197202
// URL gets the redirect URL to a file. The presigned link is valid for 5 minutes.
198203
func (m *MinioStorage) URL(path, name string) (*url.URL, error) {
199204
reqParams := make(url.Values)
200205
// TODO it may be good to embed images with 'inline' like ServeData does, but we don't want to have to read the file, do we?
201206
reqParams.Set("response-content-disposition", "attachment; filename=\""+quoteEscaper.Replace(name)+"\"")
202-
return m.client.PresignedGetObject(m.ctx, m.bucket, m.buildMinioPath(path), 5*time.Minute, reqParams)
207+
u, err := m.client.PresignedGetObject(m.ctx, m.bucket, m.buildMinioPath(path), 5*time.Minute, reqParams)
208+
return u, convertMinioErr(err)
203209
}
204210

205211
// IterateObjects iterates across the objects in the miniostorage
@@ -213,13 +219,13 @@ func (m *MinioStorage) IterateObjects(fn func(path string, obj Object) error) er
213219
}) {
214220
object, err := m.client.GetObject(lobjectCtx, m.bucket, mObjInfo.Key, opts)
215221
if err != nil {
216-
return err
222+
return convertMinioErr(err)
217223
}
218224
if err := func(object *minio.Object, fn func(path string, obj Object) error) error {
219225
defer object.Close()
220226
return fn(strings.TrimPrefix(m.basePath, mObjInfo.Key), &minioObject{object})
221227
}(object, fn); err != nil {
222-
return err
228+
return convertMinioErr(err)
223229
}
224230
}
225231
return nil

modules/storage/storage.go

+9-4
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"net/url"
1313
"os"
1414

15+
"code.gitea.io/gitea/modules/log"
1516
"code.gitea.io/gitea/modules/setting"
1617
)
1718

@@ -141,21 +142,25 @@ func NewStorage(typStr string, cfg interface{}) (ObjectStorage, error) {
141142
}
142143

143144
func initAvatars() (err error) {
144-
Avatars, err = NewStorage(setting.Avatar.Storage.Type, setting.Avatar.Storage)
145+
log.Info("Initialising Avatar storage with type: %s", setting.Avatar.Storage.Type)
146+
Avatars, err = NewStorage(setting.Avatar.Storage.Type, &setting.Avatar.Storage)
145147
return
146148
}
147149

148150
func initAttachments() (err error) {
149-
Attachments, err = NewStorage(setting.Attachment.Storage.Type, setting.Attachment.Storage)
151+
log.Info("Initialising Attachment storage with type: %s", setting.Attachment.Storage.Type)
152+
Attachments, err = NewStorage(setting.Attachment.Storage.Type, &setting.Attachment.Storage)
150153
return
151154
}
152155

153156
func initLFS() (err error) {
154-
LFS, err = NewStorage(setting.LFS.Storage.Type, setting.LFS.Storage)
157+
log.Info("Initialising LFS storage with type: %s", setting.LFS.Storage.Type)
158+
LFS, err = NewStorage(setting.LFS.Storage.Type, &setting.LFS.Storage)
155159
return
156160
}
157161

158162
func initRepoAvatars() (err error) {
159-
RepoAvatars, err = NewStorage(setting.RepoAvatar.Storage.Type, setting.RepoAvatar.Storage)
163+
log.Info("Initialising Repository Avatar storage with type: %s", setting.RepoAvatar.Storage.Type)
164+
RepoAvatars, err = NewStorage(setting.RepoAvatar.Storage.Type, &setting.RepoAvatar.Storage)
160165
return
161166
}

0 commit comments

Comments
 (0)