@@ -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.
246198func 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 }
0 commit comments