Skip to content

Commit e29ba59

Browse files
Implement separate FileStorage for bucket
1 parent 16c1597 commit e29ba59

File tree

17 files changed

+421
-333
lines changed

17 files changed

+421
-333
lines changed

models/admin.go

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

77
import (
8-
"context"
98
"fmt"
109
"os"
1110

1211
"code.gitea.io/gitea/modules/log"
13-
"code.gitea.io/gitea/modules/setting"
1412
"code.gitea.io/gitea/modules/timeutil"
1513

1614
"github.com/Unknwon/com"
@@ -62,23 +60,6 @@ func RemoveAllWithNotice(title, path string) {
6260
removeAllWithNotice(x, title, path)
6361
}
6462

65-
func removeAllFromBucket(bucketPath, objKey string) error {
66-
ctx := context.Background()
67-
bucket, err := setting.OpenBucket(ctx, bucketPath)
68-
if err != nil {
69-
return fmt.Errorf("could not open bucket: %v", err)
70-
}
71-
defer bucket.Close()
72-
73-
exist, err := bucket.Exists(ctx, objKey)
74-
if err != nil {
75-
return err
76-
} else if exist {
77-
return bucket.Delete(ctx, objKey)
78-
}
79-
return nil
80-
}
81-
8263
func removeAllWithNotice(e Engine, title, path string) {
8364
if err := os.RemoveAll(path); err != nil {
8465
desc := fmt.Sprintf("%s [%s]: %v", title, path, err)

models/attachment.go

Lines changed: 28 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ import (
88
"context"
99
"fmt"
1010
"io"
11-
"os"
1211
"path"
1312

1413
"code.gitea.io/gitea/modules/setting"
14+
"code.gitea.io/gitea/modules/storage"
1515
api "code.gitea.io/gitea/modules/structs"
1616
"code.gitea.io/gitea/modules/timeutil"
1717

@@ -77,46 +77,36 @@ func (a *Attachment) DownloadURL() string {
7777
return fmt.Sprintf("%sattachments/%s", setting.AppURL, a.UUID)
7878
}
7979

80-
// UploadToBucket uploads attachments to bucket
81-
func (a *Attachment) UploadToBucket(buf []byte, file io.Reader) (*Attachment, error) {
82-
ctx := context.Background()
83-
bucket, err := setting.OpenBucket(ctx, setting.AttachmentPath)
84-
if err != nil {
85-
return nil, fmt.Errorf("could not open bucket: %v", err)
86-
}
87-
defer bucket.Close()
88-
89-
bw, err := bucket.NewWriter(ctx, a.AttachmentBasePath(), nil)
90-
if err != nil {
91-
return nil, fmt.Errorf("failed to obtain writer: %v", err)
92-
}
80+
// NewAttachment creates a new attachment object.
81+
func NewAttachment(attach *Attachment, buf []byte, file io.Reader) (_ *Attachment, err error) {
82+
attach.UUID = gouuid.NewV4().String()
9383

94-
if _, err = bw.Write(buf); err != nil {
95-
return nil, fmt.Errorf("error occurred while writing: %v", err)
96-
} else if _, err = io.Copy(bw, file); err != nil {
97-
return nil, fmt.Errorf("error occurred while copying: %v", err)
98-
}
99-
if err = bw.Close(); err != nil {
100-
return nil, fmt.Errorf("failed to close: %v", err)
84+
fs := storage.FileStorage{
85+
Ctx: context.Background(),
86+
Path: setting.AttachmentPath,
87+
FileName: attach.AttachmentBasePath(),
10188
}
10289

103-
attrs, err := bucket.Attributes(ctx, a.AttachmentBasePath())
90+
fw, err := fs.NewWriter()
10491
if err != nil {
105-
return nil, fmt.Errorf("failed to read attributes: %v", err)
92+
return nil, fmt.Errorf("Create: %v", err)
10693
}
107-
a.Size = attrs.Size
10894

109-
return a, nil
110-
}
111-
112-
// NewAttachment creates a new attachment object.
113-
func NewAttachment(attach *Attachment, buf []byte, file io.Reader) (_ *Attachment, err error) {
114-
attach.UUID = gouuid.NewV4().String()
95+
if _, err = fw.Write(buf); err != nil {
96+
fw.Close()
97+
return nil, fmt.Errorf("Write: %v", err)
98+
} else if _, err = io.Copy(fw, file); err != nil {
99+
fw.Close()
100+
return nil, fmt.Errorf("Copy: %v", err)
101+
}
102+
fw.Close()
115103

116-
attach, err = attach.UploadToBucket(buf, file)
104+
// Update file size
105+
fi, err := fs.Attributes()
117106
if err != nil {
118-
return nil, err
107+
return nil, fmt.Errorf("file size: %v", err)
119108
}
109+
attach.Size = fi.Size
120110

121111
if _, err := x.Insert(attach); err != nil {
122112
return nil, err
@@ -204,44 +194,6 @@ func getAttachmentByReleaseIDFileName(e Engine, releaseID int64, fileName string
204194
return attach, nil
205195
}
206196

207-
// Open provides attachment reader from bucket
208-
func (a *Attachment) Open() (io.ReadCloser, error) {
209-
ctx := context.Background()
210-
bucket, err := setting.OpenBucket(ctx, setting.AttachmentPath)
211-
if err != nil {
212-
return nil, fmt.Errorf("could not open bucket: %v", err)
213-
}
214-
defer bucket.Close()
215-
216-
exist, err := bucket.Exists(ctx, a.AttachmentBasePath())
217-
if err != nil {
218-
return nil, err
219-
} else if !exist {
220-
return nil, os.ErrNotExist
221-
}
222-
223-
return bucket.NewReader(ctx, a.AttachmentBasePath(), nil)
224-
}
225-
226-
// deleteFromBucket deletes attachments from bucket
227-
func (a *Attachment) deleteFromBucket() error {
228-
ctx := context.Background()
229-
bucket, err := setting.OpenBucket(ctx, setting.AttachmentPath)
230-
if err != nil {
231-
return fmt.Errorf("could not open bucket: %v", err)
232-
}
233-
defer bucket.Close()
234-
235-
exist, err := bucket.Exists(ctx, a.AttachmentBasePath())
236-
if err != nil {
237-
return err
238-
} else if !exist {
239-
return os.ErrNotExist
240-
}
241-
242-
return bucket.Delete(ctx, a.AttachmentBasePath())
243-
}
244-
245197
// DeleteAttachment deletes the given attachment and optionally the associated file.
246198
func DeleteAttachment(a *Attachment, remove bool) error {
247199
_, err := DeleteAttachments([]*Attachment{a}, remove)
@@ -266,7 +218,12 @@ func DeleteAttachments(attachments []*Attachment, remove bool) (int, error) {
266218

267219
if remove {
268220
for i, a := range attachments {
269-
if err := a.deleteFromBucket(); 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 {
270227
return i, err
271228
}
272229
}

models/migrations/v61.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111

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

1516
"github.com/go-xorm/xorm"
1617
)
@@ -30,16 +31,14 @@ func addSizeToAttachment(x *xorm.Engine) error {
3031
return fmt.Errorf("query attachments: %v", err)
3132
}
3233

33-
ctx := context.Background()
34-
bucket, err := setting.OpenBucket(ctx, setting.AttachmentPath)
35-
if err != nil {
36-
return fmt.Errorf("could not open bucket: %v", err)
34+
fs := storage.FileStorage{
35+
Ctx: context.Background(),
36+
Path: setting.AttachmentPath,
3737
}
38-
defer bucket.Close()
3938

4039
for _, attach := range attachments {
41-
basePath := path.Join(attach.UUID[0:1], attach.UUID[1:2], attach.UUID)
42-
attrs, err := bucket.Attributes(ctx, basePath)
40+
fs.FileName = path.Join(attach.UUID[0:1], attach.UUID[1:2], attach.UUID)
41+
attrs, err := fs.Attributes()
4342
if err != nil {
4443
log.Error("calculate file size of attachment[UUID: %s]: %v", attach.UUID, err)
4544
continue

models/org.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@
66
package models
77

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

1415
"code.gitea.io/gitea/modules/log"
1516
"code.gitea.io/gitea/modules/setting"
17+
"code.gitea.io/gitea/modules/storage"
1618
"code.gitea.io/gitea/modules/structs"
1719

1820
"github.com/go-xorm/xorm"
@@ -275,7 +277,12 @@ func deleteOrg(e *xorm.Session, u *User) error {
275277

276278
if len(u.Avatar) > 0 {
277279
avatarPath := u.CustomAvatarPath()
278-
if err := deleteAvatarFromBucket(setting.AvatarUploadPath, u.Avatar); err != nil {
280+
fs := storage.FileStorage{
281+
Ctx: context.Background(),
282+
Path: setting.AvatarUploadPath,
283+
FileName: u.Avatar,
284+
}
285+
if err := fs.Delete(); err != nil {
279286
return fmt.Errorf("Failed to remove %s: %v", avatarPath, err)
280287
}
281288
}

0 commit comments

Comments
 (0)