@@ -8,10 +8,10 @@ import (
8
8
"context"
9
9
"fmt"
10
10
"io"
11
- "os"
12
11
"path"
13
12
14
13
"code.gitea.io/gitea/modules/setting"
14
+ "code.gitea.io/gitea/modules/storage"
15
15
api "code.gitea.io/gitea/modules/structs"
16
16
"code.gitea.io/gitea/modules/timeutil"
17
17
@@ -77,46 +77,36 @@ func (a *Attachment) DownloadURL() string {
77
77
return fmt .Sprintf ("%sattachments/%s" , setting .AppURL , a .UUID )
78
78
}
79
79
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 ()
93
83
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 (),
101
88
}
102
89
103
- attrs , err := bucket . Attributes ( ctx , a . AttachmentBasePath () )
90
+ fw , err := fs . NewWriter ( )
104
91
if err != nil {
105
- return nil , fmt .Errorf ("failed to read attributes : %v" , err )
92
+ return nil , fmt .Errorf ("Create : %v" , err )
106
93
}
107
- a .Size = attrs .Size
108
94
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 ()
115
103
116
- attach , err = attach .UploadToBucket (buf , file )
104
+ // Update file size
105
+ fi , err := fs .Attributes ()
117
106
if err != nil {
118
- return nil , err
107
+ return nil , fmt . Errorf ( "file size: %v" , err )
119
108
}
109
+ attach .Size = fi .Size
120
110
121
111
if _ , err := x .Insert (attach ); err != nil {
122
112
return nil , err
@@ -204,44 +194,6 @@ func getAttachmentByReleaseIDFileName(e Engine, releaseID int64, fileName string
204
194
return attach , nil
205
195
}
206
196
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
-
245
197
// DeleteAttachment deletes the given attachment and optionally the associated file.
246
198
func DeleteAttachment (a * Attachment , remove bool ) error {
247
199
_ , err := DeleteAttachments ([]* Attachment {a }, remove )
@@ -266,7 +218,12 @@ func DeleteAttachments(attachments []*Attachment, remove bool) (int, error) {
266
218
267
219
if remove {
268
220
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 {
270
227
return i , err
271
228
}
272
229
}
0 commit comments