@@ -12,17 +12,19 @@ import (
12
12
"path"
13
13
"time"
14
14
15
+ api "code.gitea.io/sdk/gitea"
15
16
gouuid "github.com/satori/go.uuid"
16
17
18
+ "code.gitea.io/gitea/modules/log"
17
19
"code.gitea.io/gitea/modules/setting"
18
20
)
19
21
20
22
// Attachment represent a attachment of issue/comment/release.
21
23
type Attachment struct {
22
- ID int64 `xorm:"pk autoincr"`
23
- UUID string `xorm:"uuid UNIQUE"`
24
- IssueID int64 `xorm:"INDEX"`
25
- ReleaseID int64 `xorm:"INDEX"`
24
+ ID int64 `xorm:"pk autoincr"`
25
+ UUID string `xorm:"uuid UNIQUE"`
26
+ IssueID int64 `xorm:"INDEX"`
27
+ ReleaseID int64 `xorm:"INDEX"`
26
28
CommentID int64
27
29
Name string
28
30
DownloadCount int64 `xorm:"DEFAULT 0"`
@@ -49,6 +51,24 @@ func (a *Attachment) IncreaseDownloadCount() error {
49
51
return nil
50
52
}
51
53
54
+ // APIFormat converts a Attachment to an api.Attachment
55
+ func (a * Attachment ) APIFormat () * api.Attachment {
56
+ apiAttachment := & api.Attachment {
57
+ ID : a .ID ,
58
+ Created : a .Created ,
59
+ Name : a .Name ,
60
+ UUID : a .UUID ,
61
+ DownloadURL : setting .AppURL + "attachments/" + a .UUID ,
62
+ DownloadCount : a .DownloadCount ,
63
+ }
64
+ fileSize , err := a .getSize ()
65
+ log .Warn ("Error getting the file size for attachment %s. " , a .UUID , err )
66
+ if err == nil {
67
+ apiAttachment .Size = fileSize
68
+ }
69
+ return apiAttachment
70
+ }
71
+
52
72
// AttachmentLocalPath returns where attachment is stored in local file
53
73
// system based on given UUID.
54
74
func AttachmentLocalPath (uuid string ) string {
@@ -112,11 +132,32 @@ func getAttachmentsByUUIDs(e Engine, uuids []string) ([]*Attachment, error) {
112
132
return attachments , e .In ("uuid" , uuids ).Find (& attachments )
113
133
}
114
134
135
+ // getSize gets the size of the attachment in bytes
136
+ func (a * Attachment ) getSize () (int64 , error ) {
137
+ info , err := os .Stat (a .LocalPath ())
138
+ if err != nil {
139
+ return 0 , err
140
+ }
141
+ return info .Size (), nil
142
+ }
143
+
115
144
// GetAttachmentByUUID returns attachment by given UUID.
116
145
func GetAttachmentByUUID (uuid string ) (* Attachment , error ) {
117
146
return getAttachmentByUUID (x , uuid )
118
147
}
119
148
149
+ // GetAttachmentByID returns attachment by given ID.
150
+ func GetAttachmentByID (id int64 ) (* Attachment , error ) {
151
+ attach := & Attachment {ID : id }
152
+ has , err := x .Get (attach )
153
+ if err != nil {
154
+ return nil , err
155
+ } else if ! has {
156
+ return nil , ErrAttachmentNotExist {id , "" }
157
+ }
158
+ return attach , nil
159
+ }
160
+
120
161
func getAttachmentsByIssueID (e Engine , issueID int64 ) ([]* Attachment , error ) {
121
162
attachments := make ([]* Attachment , 0 , 10 )
122
163
return attachments , e .Where ("issue_id = ? AND comment_id = 0" , issueID ).Find (& attachments )
@@ -137,6 +178,17 @@ func getAttachmentsByCommentID(e Engine, commentID int64) ([]*Attachment, error)
137
178
return attachments , x .Where ("comment_id=?" , commentID ).Find (& attachments )
138
179
}
139
180
181
+ // GetAttachmentsByReleaseID returns all attachments of a release
182
+ func GetAttachmentsByReleaseID (releaseID int64 ) ([]* Attachment , error ) {
183
+ return getAttachmentsByReleaseID (x , releaseID )
184
+ }
185
+
186
+ // getAttachmentsByReleaseID returns all attachments of a release
187
+ func getAttachmentsByReleaseID (e Engine , releaseID int64 ) ([]* Attachment , error ) {
188
+ attachments := make ([]* Attachment , 0 , 10 )
189
+ return attachments , e .Where ("release_id=?" , releaseID ).Find (& attachments )
190
+ }
191
+
140
192
// DeleteAttachment deletes the given attachment and optionally the associated file.
141
193
func DeleteAttachment (a * Attachment , remove bool ) error {
142
194
_ , err := DeleteAttachments ([]* Attachment {a }, remove )
0 commit comments