Skip to content

Commit dbe2c41

Browse files
committed
refactor the codes
1 parent 522fe05 commit dbe2c41

File tree

4 files changed

+81
-64
lines changed

4 files changed

+81
-64
lines changed

integrations/sqlite.ini.tmpl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,12 @@ PROVIDER = file
6666
PROVIDER_CONFIG = integrations/gitea-integration-sqlite/data/sessions
6767

6868
[log]
69-
MODE = test,file,console
69+
MODE = test,file
7070
ROOT_PATH = sqlite-log
7171
REDIRECT_MACARON_LOG = true
7272
ROUTER = ,
7373
MACARON = ,
74-
XORM = file,console
74+
XORM = file
7575

7676
[log.test]
7777
LEVEL = Info
@@ -86,4 +86,4 @@ SECRET_KEY = 9pCviYTWSb
8686
INTERNAL_TOKEN = eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYmYiOjE0OTI3OTU5ODN9.OQkH5UmzID2XBdwQ9TAI6Jj2t1X-wElVTjbE7aoN4I8
8787

8888
[oauth2]
89-
JWT_SECRET = KZb_QLUd4fYVyxetjxC4eZkrBgWM2SndOOWDNtgUUko
89+
JWT_SECRET = KZb_QLUd4fYVyxetjxC4eZkrBgWM2SndOOWDNtgUUko

models/attachment.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,6 @@ func (a *Attachment) APIFormat() *api.Attachment {
5656
}
5757
}
5858

59-
// DownloadURL returns the download url of the attached file
60-
func (a *Attachment) DownloadURL() string {
61-
return fmt.Sprintf("%sattachments/%s", setting.AppURL, a.UUID)
62-
}
63-
6459
// AttachmentRelativePath returns the relative path
6560
func AttachmentRelativePath(uuid string) string {
6661
return path.Join(uuid[0:1], uuid[1:2], uuid)
@@ -71,6 +66,11 @@ func (a *Attachment) RelativePath() string {
7166
return AttachmentRelativePath(a.UUID)
7267
}
7368

69+
// DownloadURL returns the download url of the attached file
70+
func (a *Attachment) DownloadURL() string {
71+
return fmt.Sprintf("%sattachments/%s", setting.AppURL, a.UUID)
72+
}
73+
7474
// LinkedRepository returns the linked repo if any
7575
func (a *Attachment) LinkedRepository() (*Repository, UnitType, error) {
7676
if a.IssueID != 0 {

modules/setting/attachment.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// Copyright 2020 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package setting
6+
7+
import (
8+
"path"
9+
"path/filepath"
10+
"strings"
11+
)
12+
13+
var (
14+
// Attachment settings
15+
Attachment = struct {
16+
StoreType string
17+
Path string
18+
Minio struct {
19+
Endpoint string
20+
AccessKeyID string
21+
SecretAccessKey string
22+
UseSSL bool
23+
Bucket string
24+
Location string
25+
BasePath string
26+
}
27+
AllowedTypes string
28+
MaxSize int64
29+
MaxFiles int
30+
Enabled bool
31+
}{
32+
StoreType: "local",
33+
Minio: struct {
34+
Endpoint string
35+
AccessKeyID string
36+
SecretAccessKey string
37+
UseSSL bool
38+
Bucket string
39+
Location string
40+
BasePath string
41+
}{},
42+
AllowedTypes: "image/jpeg,image/png,application/zip,application/gzip",
43+
MaxSize: 4,
44+
MaxFiles: 5,
45+
Enabled: true,
46+
}
47+
)
48+
49+
func newAttachmentService() {
50+
sec := Cfg.Section("attachment")
51+
Attachment.StoreType = sec.Key("STORE_TYPE").MustString("local")
52+
switch Attachment.StoreType {
53+
case "local":
54+
Attachment.Path = sec.Key("PATH").MustString(path.Join(AppDataPath, "attachments"))
55+
if !filepath.IsAbs(Attachment.Path) {
56+
Attachment.Path = path.Join(AppWorkPath, Attachment.Path)
57+
}
58+
case "minio":
59+
Attachment.Minio.Endpoint = sec.Key("MINIO_ENDPOINT").MustString("localhost:9000")
60+
Attachment.Minio.AccessKeyID = sec.Key("MINIO_ACCESS_KEY_ID").MustString("")
61+
Attachment.Minio.SecretAccessKey = sec.Key("MINIO_SECRET_ACCESS_KEY").MustString("")
62+
Attachment.Minio.Bucket = sec.Key("MINIO_BUCKET").MustString("gitea")
63+
Attachment.Minio.Location = sec.Key("MINIO_LOCATION").MustString("us-east-1")
64+
Attachment.Minio.BasePath = sec.Key("MINIO_BASE_PATH").MustString("attachments/")
65+
Attachment.Minio.UseSSL = sec.Key("MINIO_USE_SSL").MustBool(false)
66+
}
67+
68+
Attachment.AllowedTypes = strings.Replace(sec.Key("ALLOWED_TYPES").MustString("image/jpeg,image/png,application/zip,application/gzip"), "|", ",", -1)
69+
Attachment.MaxSize = sec.Key("MAX_SIZE").MustInt64(4)
70+
Attachment.MaxFiles = sec.Key("MAX_FILES").MustInt(5)
71+
Attachment.Enabled = sec.Key("ENABLED").MustBool(true)
72+
}

modules/setting/setting.go

Lines changed: 1 addition & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -298,40 +298,6 @@ var (
298298
AccessLogTemplate string
299299
EnableXORMLog bool
300300

301-
// Attachment settings
302-
Attachment = struct {
303-
StoreType string
304-
Path string
305-
Minio struct {
306-
Endpoint string
307-
AccessKeyID string
308-
SecretAccessKey string
309-
UseSSL bool
310-
Bucket string
311-
Location string
312-
BasePath string
313-
}
314-
AllowedTypes string
315-
MaxSize int64
316-
MaxFiles int
317-
Enabled bool
318-
}{
319-
StoreType: "local",
320-
Minio: struct {
321-
Endpoint string
322-
AccessKeyID string
323-
SecretAccessKey string
324-
UseSSL bool
325-
Bucket string
326-
Location string
327-
BasePath string
328-
}{},
329-
AllowedTypes: "image/jpeg,image/png,application/zip,application/gzip",
330-
MaxSize: 4,
331-
MaxFiles: 5,
332-
Enabled: true,
333-
}
334-
335301
// Time settings
336302
TimeFormat string
337303
// UILocation is the location on the UI, so that we can display the time on UI.
@@ -864,28 +830,7 @@ func NewContext() {
864830
}
865831
}
866832

867-
sec = Cfg.Section("attachment")
868-
Attachment.StoreType = sec.Key("STORE_TYPE").MustString("local")
869-
switch Attachment.StoreType {
870-
case "local":
871-
Attachment.Path = sec.Key("PATH").MustString(path.Join(AppDataPath, "attachments"))
872-
if !filepath.IsAbs(Attachment.Path) {
873-
Attachment.Path = path.Join(AppWorkPath, Attachment.Path)
874-
}
875-
case "minio":
876-
Attachment.Minio.Endpoint = sec.Key("MINIO_ENDPOINT").MustString("localhost:9000")
877-
Attachment.Minio.AccessKeyID = sec.Key("MINIO_ACCESS_KEY_ID").MustString("")
878-
Attachment.Minio.SecretAccessKey = sec.Key("MINIO_SECRET_ACCESS_KEY").MustString("")
879-
Attachment.Minio.Bucket = sec.Key("MINIO_BUCKET").MustString("gitea")
880-
Attachment.Minio.Location = sec.Key("MINIO_LOCATION").MustString("us-east-1")
881-
Attachment.Minio.BasePath = sec.Key("MINIO_BASE_PATH").MustString("attachments/")
882-
Attachment.Minio.UseSSL = sec.Key("MINIO_USE_SSL").MustBool(false)
883-
}
884-
885-
Attachment.AllowedTypes = strings.Replace(sec.Key("ALLOWED_TYPES").MustString("image/jpeg,image/png,application/zip,application/gzip"), "|", ",", -1)
886-
Attachment.MaxSize = sec.Key("MAX_SIZE").MustInt64(4)
887-
Attachment.MaxFiles = sec.Key("MAX_FILES").MustInt(5)
888-
Attachment.Enabled = sec.Key("ENABLED").MustBool(true)
833+
newAttachmentService()
889834

890835
timeFormatKey := Cfg.Section("time").Key("FORMAT").MustString("")
891836
if timeFormatKey != "" {

0 commit comments

Comments
 (0)