Skip to content

Commit a9f3b32

Browse files
Add Bucket Storage Support for files
Signed-off-by: Masudur Rahman <[email protected]>
1 parent d151503 commit a9f3b32

File tree

631 files changed

+195825
-189
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

631 files changed

+195825
-189
lines changed

custom/conf/app.ini.sample

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -869,3 +869,9 @@ QUEUE_LENGTH = 1000
869869
; Task queue connction string, available only when `QUEUE_TYPE` is `redis`.
870870
; If there is a password of redis, use `addrs=127.0.0.1:6379 password=123 db=0`.
871871
QUEUE_CONN_STR = "addrs=127.0.0.1:6379 db=0"
872+
873+
[storage]
874+
; URL of bucket where files are stored (such as attachments, avatars, etc.).
875+
; If unset, `BUCKET_URL` defaults to `file:// <AppWorkPath>`.
876+
; Consult https://gocloud.dev/howto/blob for URL format for various cloud providers and their credential paths and env variables.
877+
BUCKET_URL =

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,10 @@ Two special environment variables are passed to the render command:
560560
- `QUEUE_LENGTH`: **1000**: Task queue length, available only when `QUEUE_TYPE` is `channel`.
561561
- `QUEUE_CONN_STR`: **addrs=127.0.0.1:6379 db=0**: Task queue connection string, available only when `QUEUE_TYPE` is `redis`. If there redis needs a password, use `addrs=127.0.0.1:6379 password=123 db=0`.
562562

563+
## Storage (`storage`)
564+
565+
- `BUCKET_URL`: URL of bucket where files are stored (such as attachments, avatars, etc.). If unset, `BUCKET_URL` defaults to `file:// <AppWorkPath>`. Consult https://gocloud.dev/howto/blob for URL format for various cloud providers and their credential paths and env variables.
566+
563567
## Other (`other`)
564568

565569
- `SHOW_FOOTER_BRANDING`: **false**: Show Gitea branding in the footer.

docs/content/doc/advanced/config-cheat-sheet.zh-cn.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,10 @@ IS_INPUT_FILE = false
256256
- `QUEUE_LENGTH`: **1000**: 任务队列长度,当 `QUEUE_TYPE``channel` 时有效。
257257
- `QUEUE_CONN_STR`: **addrs=127.0.0.1:6379 db=0**: 任务队列连接字符串,当 `QUEUE_TYPE``redis` 时有效。如果redis有密码,则可以 `addrs=127.0.0.1:6379 password=123 db=0`
258258

259+
## Storage (`storage`)
260+
261+
- `BUCKET_URL`: 存储文件的存储桶的URL(例如附件,头像等). 如果未设置,`BUCKET_URL`默认为`file:// <AppWorkPath>`. 有关各种云提供商及其凭证路径和env变量的URL格式,请参阅https://gocloud.dev/howto/blob .
262+
259263
## Other (`other`)
260264

261265
- `SHOW_FOOTER_BRANDING`: 为真则在页面底部显示Gitea的字样。

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ require (
5353
github.com/issue9/identicon v0.0.0-20160320065130-d36b54562f4c
5454
github.com/jaytaylor/html2text v0.0.0-20160923191438-8fb95d837f7d
5555
github.com/jmhodges/levigo v1.0.0 // indirect
56-
github.com/joho/godotenv v1.3.0 // indirect
5756
github.com/kballard/go-shellquote v0.0.0-20170619183022-cd60e84ee657
5857
github.com/keybase/go-crypto v0.0.0-20170605145657-00ac4db533f6
5958
github.com/klauspost/compress v0.0.0-20161025140425-8df558b6cb6f
@@ -65,7 +64,7 @@ require (
6564
github.com/lunny/levelqueue v0.0.0-20190217115915-02b525a4418e
6665
github.com/mailru/easyjson v0.7.0 // indirect
6766
github.com/markbates/goth v1.56.0
68-
github.com/mattn/go-isatty v0.0.7
67+
github.com/mattn/go-isatty v0.0.8
6968
github.com/mattn/go-oci8 v0.0.0-20190320171441-14ba190cf52d // indirect
7069
github.com/mattn/go-sqlite3 v1.11.0
7170
github.com/mcuadros/go-version v0.0.0-20190308113854-92cdf37c5b75
@@ -99,6 +98,7 @@ require (
9998
github.com/urfave/cli v1.20.0
10099
github.com/willf/bitset v0.0.0-20180426185212-8ce1146b8621 // indirect
101100
github.com/yohcop/openid-go v0.0.0-20160914080427-2c050d2dae53
101+
gocloud.dev v0.16.0
102102
golang.org/x/crypto v0.0.0-20190927123631-a832865fa7ad
103103
golang.org/x/net v0.0.0-20190909003024-a7b16738d86b
104104
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45

go.sum

Lines changed: 77 additions & 35 deletions
Large diffs are not rendered by default.

models/attachment.go

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@
55
package models
66

77
import (
8+
"context"
89
"fmt"
910
"io"
10-
"os"
1111
"path"
1212

1313
"code.gitea.io/gitea/modules/setting"
14+
"code.gitea.io/gitea/modules/storage"
1415
api "code.gitea.io/gitea/modules/structs"
1516
"code.gitea.io/gitea/modules/timeutil"
1617

@@ -66,6 +67,11 @@ func (a *Attachment) LocalPath() string {
6667
return AttachmentLocalPath(a.UUID)
6768
}
6869

70+
// AttachmentBasePath returns the file name of attachment
71+
func (a *Attachment) AttachmentBasePath() string {
72+
return path.Join(a.UUID[0:1], a.UUID[1:2], a.UUID)
73+
}
74+
6975
// DownloadURL returns the download url of the attached file
7076
func (a *Attachment) DownloadURL() string {
7177
return fmt.Sprintf("%sattachments/%s", setting.AppURL, a.UUID)
@@ -75,29 +81,32 @@ func (a *Attachment) DownloadURL() string {
7581
func NewAttachment(attach *Attachment, buf []byte, file io.Reader) (_ *Attachment, err error) {
7682
attach.UUID = gouuid.NewV4().String()
7783

78-
localPath := attach.LocalPath()
79-
if err = os.MkdirAll(path.Dir(localPath), os.ModePerm); err != nil {
80-
return nil, fmt.Errorf("MkdirAll: %v", err)
84+
fs := storage.FileStorage{
85+
Ctx: context.Background(),
86+
Path: setting.AttachmentPath,
87+
FileName: attach.AttachmentBasePath(),
8188
}
8289

83-
fw, err := os.Create(localPath)
90+
fw, err := fs.NewWriter()
8491
if err != nil {
8592
return nil, fmt.Errorf("Create: %v", err)
8693
}
87-
defer fw.Close()
8894

8995
if _, err = fw.Write(buf); err != nil {
96+
fw.Close()
9097
return nil, fmt.Errorf("Write: %v", err)
9198
} else if _, err = io.Copy(fw, file); err != nil {
99+
fw.Close()
92100
return nil, fmt.Errorf("Copy: %v", err)
93101
}
102+
fw.Close()
94103

95104
// Update file size
96-
var fi os.FileInfo
97-
if fi, err = fw.Stat(); err != nil {
105+
fi, err := fs.Attributes()
106+
if err != nil {
98107
return nil, fmt.Errorf("file size: %v", err)
99108
}
100-
attach.Size = fi.Size()
109+
attach.Size = fi.Size
101110

102111
if _, err := x.Insert(attach); err != nil {
103112
return nil, err
@@ -209,7 +218,12 @@ func DeleteAttachments(attachments []*Attachment, remove bool) (int, error) {
209218

210219
if remove {
211220
for i, a := range attachments {
212-
if err := os.Remove(a.LocalPath()); err != nil {
221+
fs := storage.FileStorage{
222+
Ctx: context.Background(),
223+
Path: setting.AttachmentPath,
224+
FileName: a.AttachmentBasePath(),
225+
}
226+
if err := fs.Delete(); err != nil {
213227
return i, err
214228
}
215229
}

models/migrations/v61.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@
55
package migrations
66

77
import (
8+
"context"
89
"fmt"
9-
"os"
1010
"path"
1111

1212
"code.gitea.io/gitea/modules/log"
1313
"code.gitea.io/gitea/modules/setting"
14+
"code.gitea.io/gitea/modules/storage"
1415

1516
"xorm.io/xorm"
1617
)
@@ -29,14 +30,20 @@ func addSizeToAttachment(x *xorm.Engine) error {
2930
if err := x.Find(&attachments); err != nil {
3031
return fmt.Errorf("query attachments: %v", err)
3132
}
33+
34+
fs := storage.FileStorage{
35+
Ctx: context.Background(),
36+
Path: setting.AttachmentPath,
37+
}
38+
3239
for _, attach := range attachments {
33-
localPath := path.Join(setting.AttachmentPath, attach.UUID[0:1], attach.UUID[1:2], attach.UUID)
34-
fi, err := os.Stat(localPath)
40+
fs.FileName = path.Join(attach.UUID[0:1], attach.UUID[1:2], attach.UUID)
41+
attrs, err := fs.Attributes()
3542
if err != nil {
3643
log.Error("calculate file size of attachment[UUID: %s]: %v", attach.UUID, err)
3744
continue
3845
}
39-
attach.Size = fi.Size()
46+
attach.Size = attrs.Size
4047
if _, err := x.ID(attach.ID).Cols("size").Update(attach); err != nil {
4148
return fmt.Errorf("update size column: %v", err)
4249
}

models/org.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,16 @@
66
package models
77

88
import (
9+
"context"
910
"fmt"
1011
"os"
1112
"strings"
1213

1314
"code.gitea.io/gitea/modules/log"
1415
"code.gitea.io/gitea/modules/setting"
16+
"code.gitea.io/gitea/modules/storage"
1517
"code.gitea.io/gitea/modules/structs"
1618

17-
"github.com/unknwon/com"
1819
"xorm.io/builder"
1920
"xorm.io/xorm"
2021
)
@@ -270,10 +271,13 @@ func deleteOrg(e *xorm.Session, u *User) error {
270271

271272
if len(u.Avatar) > 0 {
272273
avatarPath := u.CustomAvatarPath()
273-
if com.IsExist(avatarPath) {
274-
if err := os.Remove(avatarPath); err != nil {
275-
return fmt.Errorf("Failed to remove %s: %v", avatarPath, err)
276-
}
274+
fs := storage.FileStorage{
275+
Ctx: context.Background(),
276+
Path: setting.AvatarUploadPath,
277+
FileName: u.Avatar,
278+
}
279+
if err := fs.Delete(); err != nil {
280+
return fmt.Errorf("Failed to remove %s: %v", avatarPath, err)
277281
}
278282
}
279283

0 commit comments

Comments
 (0)