Skip to content

Commit e96256a

Browse files
committed
Simplify the codes
1 parent 86cc4d1 commit e96256a

File tree

5 files changed

+79
-169
lines changed

5 files changed

+79
-169
lines changed

docs/content/doc/advanced/config-cheat-sheet.en-us.md

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -565,30 +565,14 @@ Define allowed algorithms and their minimum key length (use -1 to disable a type
565565
- `ENABLE_FEDERATED_AVATAR`: **false**: Enable support for federated avatars (see
566566
[http://www.libravatar.org](http://www.libravatar.org)).
567567

568-
- `AVATAR_STORE_TYPE`: **local**: Storage type for avatars, `local` for local disk or `minio` for s3 compatible object storage service, default is `local`.
569-
- `AVATAR_UPLOAD_PATH`: **data/avatars**: Path to store user avatar image files if `AVATAR_STORE_TYPE` is `local`.
570-
- `AVATAR_SERVE_DIRECT`: **false**: Allows the storage driver to redirect to authenticated URLs to serve files directly. Currently, only Minio/S3 is supported via signed URLs, local does nothing.
571-
- `AVATAR_MINIO_ENDPOINT`: **localhost:9000**: Minio endpoint to connect only available when `AVATAR_STORE_TYPE` is `minio`
572-
- `AVATAR_MINIO_ACCESS_KEY_ID`: Minio accessKeyID to connect only available when `AVATAR_STORE_TYPE` is `minio`
573-
- `AVATAR_MINIO_SECRET_ACCESS_KEY`: Minio secretAccessKey to connect only available when `AVATAR_STORE_TYPE` is `minio`
574-
- `AVATAR_MINIO_BUCKET`: **gitea**: Minio bucket to store the attachments only available when `AVATAR_STORE_TYPE` is `minio`
575-
- `AVATAR_MINIO_LOCATION`: **us-east-1**: Minio location to create bucket only available when `AVATAR_STORE_TYPE` is `minio`
576-
- `AVATAR_MINIO_BASE_PATH`: **avatars/**: Minio base path on the bucket only available when `AVATAR_STORE_TYPE` is `minio`
577-
- `AVATAR_MINIO_USE_SSL`: **false**: Minio enabled ssl only available when `AVATAR_STORE_TYPE` is `minio`
568+
- `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`.
569+
- `AVATAR_UPLOAD_PATH`: **data/avatars**: Path to store user avatar image files.
578570
- `AVATAR_MAX_WIDTH`: **4096**: Maximum avatar image width in pixels.
579571
- `AVATAR_MAX_HEIGHT`: **3072**: Maximum avatar image height in pixels.
580572
- `AVATAR_MAX_FILE_SIZE`: **1048576** (1Mb): Maximum avatar image file size in bytes.
581573

582-
- `REPOSITORY_AVATAR_STORE_TYPE`: **local**: Storage type for avatars, `local` for local disk or `minio` for s3 compatible object storage service, default is `local`.
583-
- `REPOSITORY_AVATAR_UPLOAD_PATH`: **data/repo-avatars**: Path to store repository avatar image files, if `REPOSITORY_AVATAR_STORE_TYPE` is `local`.
584-
- `REPOSITORY_AVATAR_SERVE_DIRECT`: **false**: Allows the storage driver to redirect to authenticated URLs to serve files directly. Currently, only Minio/S3 is supported via signed URLs, local does nothing.
585-
- `REPOSITORY_AVATAR_MINIO_ENDPOINT`: **localhost:9000**: Minio endpoint to connect only available when `REPOSITORY_AVATAR_STORE_TYPE` is `minio`
586-
- `REPOSITORY_AVATAR_MINIO_ACCESS_KEY_ID`: Minio accessKeyID to connect only available when `REPOSITORY_AVATAR_STORE_TYPE` is `minio`
587-
- `REPOSITORY_AVATAR_MINIO_SECRET_ACCESS_KEY`: Minio secretAccessKey to connect only available when `REPOSITORY_AVATAR_STORE_TYPE` is `minio`
588-
- `REPOSITORY_AVATAR_MINIO_BUCKET`: **gitea**: Minio bucket to store the attachments only available when `REPOSITORY_AVATAR_STORE_TYPE` is `minio`
589-
- `REPOSITORY_AVATAR_MINIO_LOCATION`: **us-east-1**: Minio location to create bucket only available when `REPOSITORY_AVATAR_STORE_TYPE` is `minio`
590-
- `REPOSITORY_AVATAR_MINIO_BASE_PATH`: **repo-avatars/**: Minio base path on the bucket only available when `REPOSITORY_AVATAR_STORE_TYPE` is `minio`
591-
- `REPOSITORY_AVATAR_MINIO_USE_SSL`: **false**: Minio enabled ssl only available when `REPOSITORY_AVATAR_STORE_TYPE` is `minio`
574+
- `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`.
575+
- `REPOSITORY_AVATAR_UPLOAD_PATH`: **data/repo-avatars**: Path to store repository avatar image files.
592576
- `REPOSITORY_AVATAR_FALLBACK`: **none**: How Gitea deals with missing repository avatars
593577
- none = no avatar will be displayed
594578
- random = random avatar will be generated

modules/setting/picture.go

Lines changed: 16 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ var (
3636
DisableGravatar bool
3737
EnableFederatedAvatar bool
3838
LibravatarService *libravatar.Libravatar
39-
AvatarMaxFileSize int64
4039

4140
RepoAvatar = struct {
4241
Storage
@@ -53,37 +52,29 @@ var (
5352
func newPictureService() {
5453
sec := Cfg.Section("picture")
5554

56-
Avatar.Storage.Type = sec.Key("AVATAR_STORE_TYPE").MustString("")
55+
Avatar.Storage.Type = sec.Key("AVATAR_STORAGE_TYPE").MustString("")
5756
if Avatar.Storage.Type == "" {
5857
Avatar.Storage.Type = "default"
5958
}
6059

61-
if Avatar.Storage.Type != LocalStorageType && Avatar.Storage.Type != MinioStorageType {
62-
storage, ok := storages[Avatar.Storage.Type]
63-
if !ok {
64-
log.Fatal("Failed to get avatar storage type: %s", Avatar.Storage.Type)
65-
}
66-
Avatar.Storage = storage
60+
storage, ok := storages[Avatar.Storage.Type]
61+
if !ok {
62+
log.Fatal("Failed to get avatar storage type: %s", Avatar.Storage.Type)
6763
}
68-
69-
// Override
70-
Avatar.ServeDirect = sec.Key("AVATAR_SERVE_DIRECT").MustBool(Attachment.ServeDirect)
64+
Avatar.Storage = storage
7165

7266
switch Avatar.Storage.Type {
7367
case LocalStorageType:
74-
Avatar.Path = sec.Key("AVATAR_UPLOAD_PATH").MustString(filepath.Join(AppDataPath, "avatars"))
68+
Avatar.Path = sec.Key("AVATAR_UPLOAD_PATH").MustString(Avatar.Path)
69+
if Avatar.Path == "" {
70+
Avatar.Path = filepath.Join(AppDataPath, "avatars")
71+
}
7572
forcePathSeparator(Avatar.Path)
7673
if !filepath.IsAbs(Avatar.Path) {
7774
Avatar.Path = filepath.Join(AppWorkPath, Avatar.Path)
7875
}
7976
case MinioStorageType:
80-
Avatar.Minio.Endpoint = sec.Key("AVATAR_MINIO_ENDPOINT").MustString(Avatar.Minio.Endpoint)
81-
Avatar.Minio.AccessKeyID = sec.Key("AVATAR_MINIO_ACCESS_KEY_ID").MustString(Avatar.Minio.AccessKeyID)
82-
Avatar.Minio.SecretAccessKey = sec.Key("AVATAR_MINIO_SECRET_ACCESS_KEY").MustString(Avatar.Minio.SecretAccessKey)
83-
Avatar.Minio.Bucket = sec.Key("AVATAR_MINIO_BUCKET").MustString(Avatar.Minio.Bucket)
84-
Avatar.Minio.Location = sec.Key("AVATAR_MINIO_LOCATION").MustString(Avatar.Minio.Location)
85-
Avatar.Minio.UseSSL = sec.Key("AVATAR_MINIO_USE_SSL").MustBool(Avatar.Minio.UseSSL)
86-
Avatar.Minio.BasePath = sec.Key("AVATAR_MINIO_BASE_PATH").MustString("avatars/")
77+
Avatar.Minio.BasePath = sec.Key("AVATAR_UPLOAD_PATH").MustString("avatars/")
8778
}
8879

8980
Avatar.MaxWidth = sec.Key("AVATAR_MAX_WIDTH").MustInt(4096)
@@ -135,21 +126,16 @@ func newPictureService() {
135126
func newRepoAvatarService() {
136127
sec := Cfg.Section("picture")
137128

138-
RepoAvatar.Storage.Type = sec.Key("REPOSITORY_AVATAR_STORE_TYPE").MustString("")
129+
RepoAvatar.Storage.Type = sec.Key("REPOSITORY_AVATAR_STORAGE_TYPE").MustString("")
139130
if RepoAvatar.Storage.Type == "" {
140131
RepoAvatar.Storage.Type = "default"
141132
}
142133

143-
if RepoAvatar.Storage.Type != LocalStorageType && RepoAvatar.Storage.Type != MinioStorageType {
144-
storage, ok := storages[RepoAvatar.Storage.Type]
145-
if !ok {
146-
log.Fatal("Failed to get repo-avatar storage type: %s", RepoAvatar.Storage.Type)
147-
}
148-
RepoAvatar.Storage = storage
134+
storage, ok := storages[RepoAvatar.Storage.Type]
135+
if !ok {
136+
log.Fatal("Failed to get repo-avatar storage type: %s", RepoAvatar.Storage.Type)
149137
}
150-
151-
// Override
152-
RepoAvatar.ServeDirect = sec.Key("REPOSITORY_AVATAR_SERVE_DIRECT").MustBool(Attachment.ServeDirect)
138+
RepoAvatar.Storage = storage
153139

154140
switch RepoAvatar.Storage.Type {
155141
case LocalStorageType:
@@ -159,13 +145,7 @@ func newRepoAvatarService() {
159145
RepoAvatar.Path = filepath.Join(AppWorkPath, RepoAvatar.Path)
160146
}
161147
case MinioStorageType:
162-
RepoAvatar.Minio.Endpoint = sec.Key("REPOSITORY_AVATAR_MINIO_ENDPOINT").MustString(RepoAvatar.Minio.Endpoint)
163-
RepoAvatar.Minio.AccessKeyID = sec.Key("REPOSITORY_AVATAR_MINIO_ACCESS_KEY_ID").MustString(RepoAvatar.Minio.AccessKeyID)
164-
RepoAvatar.Minio.SecretAccessKey = sec.Key("REPOSITORY_AVATAR_MINIO_SECRET_ACCESS_KEY").MustString(RepoAvatar.Minio.SecretAccessKey)
165-
RepoAvatar.Minio.Bucket = sec.Key("REPOSITORY_AVATAR_MINIO_BUCKET").MustString(RepoAvatar.Minio.Bucket)
166-
RepoAvatar.Minio.Location = sec.Key("REPOSITORY_AVATAR_MINIO_LOCATION").MustString(RepoAvatar.Minio.Location)
167-
RepoAvatar.Minio.UseSSL = sec.Key("REPOSITORY_AVATAR_MINIO_USE_SSL").MustBool(RepoAvatar.Minio.UseSSL)
168-
RepoAvatar.Minio.BasePath = sec.Key("REPOSITORY_AVATAR_MINIO_BASE_PATH").MustString("avatars/")
148+
RepoAvatar.Minio.BasePath = sec.Key("REPOSITORY_AVATAR_MINIO_BASE_PATH").MustString("repo-avatars/")
169149
}
170150

171151
RepoAvatar.Fallback = sec.Key("REPOSITORY_AVATAR_FALLBACK").MustString("none")

routers/repo/setting.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -939,7 +939,7 @@ func UpdateAvatarSetting(ctx *context.Context, form auth.AvatarForm) error {
939939
}
940940
defer r.Close()
941941

942-
if form.Avatar.Size > setting.AvatarMaxFileSize {
942+
if form.Avatar.Size > setting.Avatar.MaxFileSize {
943943
return errors.New(ctx.Tr("settings.uploaded_avatar_is_too_big"))
944944
}
945945

routers/routes/routes.go

Lines changed: 57 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,61 @@ func RouterHandler(level log.Level) func(ctx *macaron.Context) {
110110
}
111111
}
112112

113+
func storageHandler(storageSetting setting.Storage, prefix string, objStore storage.ObjectStorage) macaron.Handler {
114+
if storageSetting.ServeDirect {
115+
return func(ctx *macaron.Context) {
116+
req := ctx.Req.Request
117+
if req.Method != "GET" && req.Method != "HEAD" {
118+
return
119+
}
120+
121+
if !strings.HasPrefix(req.RequestURI, "/"+prefix) {
122+
return
123+
}
124+
125+
rPath := strings.TrimPrefix(req.RequestURI, "/"+prefix)
126+
u, err := objStore.URL(rPath, path.Base(rPath))
127+
if err != nil {
128+
ctx.Error(500, err.Error())
129+
return
130+
}
131+
http.Redirect(
132+
ctx.Resp,
133+
req,
134+
u.String(),
135+
301,
136+
)
137+
}
138+
}
139+
140+
return func(ctx *macaron.Context) {
141+
req := ctx.Req.Request
142+
if req.Method != "GET" && req.Method != "HEAD" {
143+
return
144+
}
145+
146+
if !strings.HasPrefix(req.RequestURI, "/"+prefix) {
147+
return
148+
}
149+
150+
rPath := strings.TrimPrefix(req.RequestURI, "/"+prefix)
151+
rPath = strings.TrimPrefix(rPath, "/")
152+
//If we have matched and access to release or issue
153+
fr, err := objStore.Open(rPath)
154+
if err != nil {
155+
ctx.Error(500, err.Error())
156+
return
157+
}
158+
defer fr.Close()
159+
160+
_, err = io.Copy(ctx.Resp, fr)
161+
if err != nil {
162+
ctx.Error(500, err.Error())
163+
return
164+
}
165+
}
166+
}
167+
113168
// NewMacaron initializes Macaron instance.
114169
func NewMacaron() *macaron.Macaron {
115170
gob.Register(&u2f.Challenge{})
@@ -153,117 +208,8 @@ func NewMacaron() *macaron.Macaron {
153208
},
154209
))
155210

156-
switch setting.Avatar.Storage.Type {
157-
case "local":
158-
m.Use(public.StaticHandler(
159-
setting.Avatar.Path,
160-
&public.Options{
161-
Prefix: "avatars",
162-
SkipLogging: setting.DisableRouterLog,
163-
ExpiresAfter: setting.StaticCacheTime,
164-
},
165-
))
166-
case "minio":
167-
m.Use(func(ctx *macaron.Context) {
168-
req := ctx.Req.Request
169-
if req.Method != "GET" && req.Method != "HEAD" {
170-
return
171-
}
172-
173-
var prefix = "/avatars"
174-
if !strings.HasPrefix(req.RequestURI, prefix) {
175-
return
176-
}
177-
178-
rPath := strings.TrimPrefix(req.RequestURI, prefix)
179-
if setting.Avatar.ServeDirect {
180-
u, err := storage.Avatars.URL(rPath, path.Base(rPath))
181-
if err != nil {
182-
ctx.Error(500, err.Error())
183-
return
184-
}
185-
http.Redirect(
186-
ctx.Resp,
187-
req,
188-
u.String(),
189-
301,
190-
)
191-
} else {
192-
rPath = strings.TrimPrefix(rPath, "/")
193-
//If we have matched and access to release or issue
194-
fr, err := storage.Avatars.Open(rPath)
195-
if err != nil {
196-
ctx.Error(500, err.Error())
197-
return
198-
}
199-
defer fr.Close()
200-
201-
_, err = io.Copy(ctx.Resp, fr)
202-
if err != nil {
203-
ctx.Error(500, err.Error())
204-
return
205-
}
206-
}
207-
})
208-
default:
209-
log.Fatal("Unsupported avatar store type")
210-
}
211-
212-
switch setting.RepoAvatar.Storage.Type {
213-
case "local":
214-
m.Use(public.StaticHandler(
215-
setting.RepoAvatar.Path,
216-
&public.Options{
217-
Prefix: "repo-avatars",
218-
SkipLogging: setting.DisableRouterLog,
219-
ExpiresAfter: setting.StaticCacheTime,
220-
},
221-
))
222-
case "minio":
223-
m.Use(func(ctx *macaron.Context) {
224-
req := ctx.Req.Request
225-
if req.Method != "GET" && req.Method != "HEAD" {
226-
return
227-
}
228-
229-
var prefix = "/repo-avatars"
230-
if !strings.HasPrefix(req.RequestURI, prefix) {
231-
return
232-
}
233-
234-
rPath := strings.TrimPrefix(req.RequestURI, prefix)
235-
if setting.RepoAvatar.ServeDirect {
236-
u, err := storage.RepoAvatars.URL(rPath, path.Base(rPath))
237-
if err != nil {
238-
ctx.Error(500, err.Error())
239-
return
240-
}
241-
http.Redirect(
242-
ctx.Resp,
243-
req,
244-
u.String(),
245-
301,
246-
)
247-
} else {
248-
rPath = strings.TrimPrefix(rPath, "/")
249-
//If we have matched and access to release or issue
250-
fr, err := storage.RepoAvatars.Open(rPath)
251-
if err != nil {
252-
ctx.Error(500, err.Error())
253-
return
254-
}
255-
defer fr.Close()
256-
257-
_, err = io.Copy(ctx.Resp, fr)
258-
if err != nil {
259-
ctx.Error(500, err.Error())
260-
return
261-
}
262-
}
263-
})
264-
default:
265-
log.Fatal("Unsupported repo-avatar store type")
266-
}
211+
m.Use(storageHandler(setting.Avatar.Storage, "avatars", storage.Avatars))
212+
m.Use(storageHandler(setting.RepoAvatar.Storage, "repo-avatars", storage.RepoAvatars))
267213

268214
m.Use(templates.HTMLRenderer())
269215
mailer.InitMailRender(templates.Mailer())

routers/user/setting/profile.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ func UpdateAvatarSetting(ctx *context.Context, form auth.AvatarForm, ctxUser *mo
132132
}
133133
defer fr.Close()
134134

135-
if form.Avatar.Size > setting.AvatarMaxFileSize {
135+
if form.Avatar.Size > setting.Avatar.MaxFileSize {
136136
return errors.New(ctx.Tr("settings.uploaded_avatar_is_too_big"))
137137
}
138138

0 commit comments

Comments
 (0)