Skip to content

Commit 041b64e

Browse files
Model changes to support attachments in apis
1 parent 6d99840 commit 041b64e

File tree

2 files changed

+83
-14
lines changed

2 files changed

+83
-14
lines changed

models/attachment.go

Lines changed: 56 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,19 @@ import (
1212
"path"
1313
"time"
1414

15+
api "code.gitea.io/sdk/gitea"
1516
gouuid "github.com/satori/go.uuid"
1617

18+
"code.gitea.io/gitea/modules/log"
1719
"code.gitea.io/gitea/modules/setting"
1820
)
1921

2022
// Attachment represent a attachment of issue/comment/release.
2123
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"`
2628
CommentID int64
2729
Name string
2830
DownloadCount int64 `xorm:"DEFAULT 0"`
@@ -49,6 +51,24 @@ func (a *Attachment) IncreaseDownloadCount() error {
4951
return nil
5052
}
5153

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+
5272
// AttachmentLocalPath returns where attachment is stored in local file
5373
// system based on given UUID.
5474
func AttachmentLocalPath(uuid string) string {
@@ -112,11 +132,32 @@ func getAttachmentsByUUIDs(e Engine, uuids []string) ([]*Attachment, error) {
112132
return attachments, e.In("uuid", uuids).Find(&attachments)
113133
}
114134

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+
115144
// GetAttachmentByUUID returns attachment by given UUID.
116145
func GetAttachmentByUUID(uuid string) (*Attachment, error) {
117146
return getAttachmentByUUID(x, uuid)
118147
}
119148

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+
120161
func getAttachmentsByIssueID(e Engine, issueID int64) ([]*Attachment, error) {
121162
attachments := make([]*Attachment, 0, 10)
122163
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)
137178
return attachments, x.Where("comment_id=?", commentID).Find(&attachments)
138179
}
139180

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+
140192
// DeleteAttachment deletes the given attachment and optionally the associated file.
141193
func DeleteAttachment(a *Attachment, remove bool) error {
142194
_, err := DeleteAttachments([]*Attachment{a}, remove)

models/release.go

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,13 @@ type Release struct {
2828
LowerTagName string
2929
Target string
3030
Title string
31-
Sha1 string `xorm:"VARCHAR(40)"`
31+
Sha1 string `xorm:"VARCHAR(40)"`
3232
NumCommits int64
33-
NumCommitsBehind int64 `xorm:"-"`
34-
Note string `xorm:"TEXT"`
35-
IsDraft bool `xorm:"NOT NULL DEFAULT false"`
36-
IsPrerelease bool `xorm:"NOT NULL DEFAULT false"`
37-
IsTag bool `xorm:"NOT NULL DEFAULT false"`
33+
NumCommitsBehind int64 `xorm:"-"`
34+
Note string `xorm:"TEXT"`
35+
IsDraft bool `xorm:"NOT NULL DEFAULT false"`
36+
IsPrerelease bool `xorm:"NOT NULL DEFAULT false"`
37+
IsTag bool `xorm:"NOT NULL DEFAULT false"`
3838

3939
Attachments []*Attachment `xorm:"-"`
4040

@@ -68,6 +68,14 @@ func (r *Release) loadAttributes(e Engine) error {
6868
return err
6969
}
7070
}
71+
// load the attachments of this release
72+
if r.Attachments == nil {
73+
attachments, err := GetAttachmentsByReleaseID(r.ID)
74+
if err != nil {
75+
return err
76+
}
77+
r.Attachments = attachments
78+
}
7179
return nil
7280
}
7381

@@ -94,6 +102,10 @@ func (r *Release) TarURL() string {
94102

95103
// APIFormat convert a Release to api.Release
96104
func (r *Release) APIFormat() *api.Release {
105+
apiAttachments := make([]*api.Attachment, len(r.Attachments))
106+
for i := range r.Attachments {
107+
apiAttachments[i] = r.Attachments[i].APIFormat()
108+
}
97109
return &api.Release{
98110
ID: r.ID,
99111
TagName: r.TagName,
@@ -107,6 +119,7 @@ func (r *Release) APIFormat() *api.Release {
107119
CreatedAt: r.Created,
108120
PublishedAt: r.Created,
109121
Publisher: r.Publisher.APIFormat(),
122+
Attachments: apiAttachments,
110123
}
111124
}
112125

@@ -233,9 +246,10 @@ func GetReleaseByID(id int64) (*Release, error) {
233246

234247
// FindReleasesOptions describes the conditions to Find releases
235248
type FindReleasesOptions struct {
236-
IncludeDrafts bool
237-
IncludeTags bool
238-
TagNames []string
249+
IncludeDrafts bool
250+
IncludeTags bool
251+
ExcludePrereleases bool
252+
TagNames []string
239253
}
240254

241255
func (opts *FindReleasesOptions) toConds(repoID int64) builder.Cond {
@@ -248,13 +262,16 @@ func (opts *FindReleasesOptions) toConds(repoID int64) builder.Cond {
248262
if !opts.IncludeTags {
249263
cond = cond.And(builder.Eq{"is_tag": false})
250264
}
265+
if opts.ExcludePrereleases {
266+
cond = cond.And(builder.Eq{"is_prerelease": false})
267+
}
251268
if len(opts.TagNames) > 0 {
252269
cond = cond.And(builder.In("tag_name", opts.TagNames))
253270
}
254271
return cond
255272
}
256273

257-
// GetReleasesByRepoID returns a list of releases of repository.
274+
// GetReleasesByRepoID returns a list of releases of repository. The results are sorted by created date and id descending
258275
func GetReleasesByRepoID(repoID int64, opts FindReleasesOptions, page, pageSize int) (rels []*Release, err error) {
259276
if page <= 0 {
260277
page = 1

0 commit comments

Comments
 (0)