Skip to content

Commit fd3bd70

Browse files
authored
Make Release Download URLs predictable
1 parent ca905b8 commit fd3bd70

File tree

5 files changed

+80
-16
lines changed

5 files changed

+80
-16
lines changed

models/repo/attachment.go

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,18 @@ import (
1818

1919
// Attachment represent a attachment of issue/comment/release.
2020
type Attachment struct {
21-
ID int64 `xorm:"pk autoincr"`
22-
UUID string `xorm:"uuid UNIQUE"`
23-
RepoID int64 `xorm:"INDEX"` // this should not be zero
24-
IssueID int64 `xorm:"INDEX"` // maybe zero when creating
25-
ReleaseID int64 `xorm:"INDEX"` // maybe zero when creating
26-
UploaderID int64 `xorm:"INDEX DEFAULT 0"` // Notice: will be zero before this column added
27-
CommentID int64
28-
Name string
29-
DownloadCount int64 `xorm:"DEFAULT 0"`
30-
Size int64 `xorm:"DEFAULT 0"`
31-
CreatedUnix timeutil.TimeStamp `xorm:"created"`
21+
ID int64 `xorm:"pk autoincr"`
22+
UUID string `xorm:"uuid UNIQUE"`
23+
RepoID int64 `xorm:"INDEX"` // this should not be zero
24+
IssueID int64 `xorm:"INDEX"` // maybe zero when creating
25+
ReleaseID int64 `xorm:"INDEX"` // maybe zero when creating
26+
UploaderID int64 `xorm:"INDEX DEFAULT 0"` // Notice: will be zero before this column added
27+
CommentID int64
28+
Name string
29+
DownloadCount int64 `xorm:"DEFAULT 0"`
30+
Size int64 `xorm:"DEFAULT 0"`
31+
CreatedUnix timeutil.TimeStamp `xorm:"created"`
32+
CustomDownloadURL string `xorm:"-"`
3233
}
3334

3435
func init() {
@@ -57,7 +58,11 @@ func (a *Attachment) RelativePath() string {
5758

5859
// DownloadURL returns the download url of the attached file
5960
func (a *Attachment) DownloadURL() string {
60-
return setting.AppURL + "attachments/" + url.PathEscape(a.UUID)
61+
if a.CustomDownloadURL == "" {
62+
return setting.AppURL + "attachments/" + url.PathEscape(a.UUID)
63+
} else {
64+
return a.CustomDownloadURL
65+
}
6166
}
6267

6368
// _____ __ __ .__ __

models/repo/release.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package repo
77
import (
88
"context"
99
"fmt"
10+
"net/url"
1011
"sort"
1112
"strconv"
1213
"strings"
@@ -334,6 +335,20 @@ func (s releaseMetaSearch) Less(i, j int) bool {
334335
return s.ID[i] < s.ID[j]
335336
}
336337

338+
// InitReleaseRepo makes sure the Repo field of releases is not nil
339+
func InitReleaseRepo(ctx context.Context, rels ...*Release) error {
340+
var err error
341+
for _, release := range rels {
342+
if release.Repo == nil {
343+
release.Repo, err = GetRepositoryByID(ctx, release.RepoID)
344+
if err != nil {
345+
return err
346+
}
347+
}
348+
}
349+
return err
350+
}
351+
337352
// GetReleaseAttachments retrieves the attachments for releases
338353
func GetReleaseAttachments(ctx context.Context, rels ...*Release) (err error) {
339354
if len(rels) == 0 {
@@ -372,6 +387,34 @@ func GetReleaseAttachments(ctx context.Context, rels ...*Release) (err error) {
372387
sortedRels.Rel[currentIndex].Attachments = append(sortedRels.Rel[currentIndex].Attachments, attachment)
373388
}
374389

390+
// Makes URL's predictable
391+
for _, release := range rels {
392+
// If we have no Repo, we don't need to execute this loop
393+
if release.Repo == nil {
394+
continue
395+
}
396+
397+
// Check if there are two or more attachments with the same name
398+
isDoubled := false
399+
foundNames := make(map[string]bool)
400+
for _, attachment := range release.Attachments {
401+
_, found := foundNames[attachment.Name]
402+
if found {
403+
isDoubled = true
404+
break
405+
} else {
406+
foundNames[attachment.Name] = true
407+
}
408+
}
409+
410+
// If the names unique, use the URL with the Name instead of the UUID
411+
if !isDoubled {
412+
for _, attachment := range release.Attachments {
413+
attachment.CustomDownloadURL = release.Repo.HTMLURL() + "/releases/download/" + url.PathEscape(release.TagName) + "/" + url.PathEscape(attachment.Name)
414+
}
415+
}
416+
}
417+
375418
return err
376419
}
377420

routers/web/repo/attachment.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,9 @@ func DeleteAttachment(ctx *context.Context) {
8686
})
8787
}
8888

89-
// GetAttachment serve attachments
90-
func GetAttachment(ctx *context.Context) {
91-
attach, err := repo_model.GetAttachmentByUUID(ctx, ctx.Params(":uuid"))
89+
// GetAttachment serve attachments with the given UUID
90+
func ServeAttachment(ctx *context.Context, uuid string) {
91+
attach, err := repo_model.GetAttachmentByUUID(ctx, uuid)
9292
if err != nil {
9393
if repo_model.IsErrAttachmentNotExist(err) {
9494
ctx.Error(http.StatusNotFound)
@@ -153,3 +153,8 @@ func GetAttachment(ctx *context.Context) {
153153
return
154154
}
155155
}
156+
157+
// GetAttachment serve attachments
158+
func GetAttachment(ctx *context.Context) {
159+
ServeAttachment(ctx, ctx.Params(":uuid"))
160+
}

routers/web/repo/release.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,11 @@ func releasesOrTags(ctx *context.Context, isTagList bool) {
142142
return
143143
}
144144

145+
if err = repo_model.InitReleaseRepo(ctx, releases...); err != nil {
146+
ctx.ServerError("InitReleaseRepo", err)
147+
return
148+
}
149+
145150
if err = repo_model.GetReleaseAttachments(ctx, releases...); err != nil {
146151
ctx.ServerError("GetReleaseAttachments", err)
147152
return
@@ -248,6 +253,12 @@ func SingleRelease(ctx *context.Context) {
248253
ctx.Data["Title"] = release.Title
249254
}
250255

256+
err = repo_model.InitReleaseRepo(ctx, release)
257+
if err != nil {
258+
ctx.ServerError("InitReleaseRepo", err)
259+
return
260+
}
261+
251262
err = repo_model.GetReleaseAttachments(ctx, release)
252263
if err != nil {
253264
ctx.ServerError("GetReleaseAttachments", err)

routers/web/repo/repo.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ func RedirectDownload(ctx *context.Context) {
373373
return
374374
}
375375
if att != nil {
376-
ctx.Redirect(att.DownloadURL())
376+
ServeAttachment(ctx, att.UUID)
377377
return
378378
}
379379
}

0 commit comments

Comments
 (0)