Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Release Download URLs predictable #23891

Merged
merged 7 commits into from
Apr 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 16 additions & 11 deletions models/repo/attachment.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,18 @@ import (

// Attachment represent a attachment of issue/comment/release.
type Attachment struct {
ID int64 `xorm:"pk autoincr"`
UUID string `xorm:"uuid UNIQUE"`
RepoID int64 `xorm:"INDEX"` // this should not be zero
IssueID int64 `xorm:"INDEX"` // maybe zero when creating
ReleaseID int64 `xorm:"INDEX"` // maybe zero when creating
UploaderID int64 `xorm:"INDEX DEFAULT 0"` // Notice: will be zero before this column added
CommentID int64
Name string
DownloadCount int64 `xorm:"DEFAULT 0"`
Size int64 `xorm:"DEFAULT 0"`
CreatedUnix timeutil.TimeStamp `xorm:"created"`
ID int64 `xorm:"pk autoincr"`
UUID string `xorm:"uuid UNIQUE"`
RepoID int64 `xorm:"INDEX"` // this should not be zero
IssueID int64 `xorm:"INDEX"` // maybe zero when creating
ReleaseID int64 `xorm:"INDEX"` // maybe zero when creating
UploaderID int64 `xorm:"INDEX DEFAULT 0"` // Notice: will be zero before this column added
CommentID int64
Name string
DownloadCount int64 `xorm:"DEFAULT 0"`
Size int64 `xorm:"DEFAULT 0"`
CreatedUnix timeutil.TimeStamp `xorm:"created"`
CustomDownloadURL string `xorm:"-"`
}

func init() {
Expand Down Expand Up @@ -57,6 +58,10 @@ func (a *Attachment) RelativePath() string {

// DownloadURL returns the download url of the attached file
func (a *Attachment) DownloadURL() string {
if a.CustomDownloadURL != "" {
return a.CustomDownloadURL
}

return setting.AppURL + "attachments/" + url.PathEscape(a.UUID)
}

Expand Down
29 changes: 29 additions & 0 deletions models/repo/release.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package repo
import (
"context"
"fmt"
"net/url"
"sort"
"strconv"
"strings"
Expand Down Expand Up @@ -372,6 +373,34 @@ func GetReleaseAttachments(ctx context.Context, rels ...*Release) (err error) {
sortedRels.Rel[currentIndex].Attachments = append(sortedRels.Rel[currentIndex].Attachments, attachment)
}

// Makes URL's predictable
for _, release := range rels {
// If we have no Repo, we don't need to execute this loop
if release.Repo == nil {
continue
}

// Check if there are two or more attachments with the same name
hasDuplicates := false
foundNames := make(map[string]bool)
for _, attachment := range release.Attachments {
_, found := foundNames[attachment.Name]
if found {
hasDuplicates = true
break
} else {
foundNames[attachment.Name] = true
}
}

// If the names unique, use the URL with the Name instead of the UUID
if !hasDuplicates {
for _, attachment := range release.Attachments {
attachment.CustomDownloadURL = release.Repo.HTMLURL() + "/releases/download/" + url.PathEscape(release.TagName) + "/" + url.PathEscape(attachment.Name)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we use a relative URL here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The URL is used in the API, so no.

}
}
}

return err
}

Expand Down
11 changes: 8 additions & 3 deletions routers/web/repo/attachment.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,9 @@ func DeleteAttachment(ctx *context.Context) {
})
}

// GetAttachment serve attachments
func GetAttachment(ctx *context.Context) {
attach, err := repo_model.GetAttachmentByUUID(ctx, ctx.Params(":uuid"))
// GetAttachment serve attachments with the given UUID
func ServeAttachment(ctx *context.Context, uuid string) {
attach, err := repo_model.GetAttachmentByUUID(ctx, uuid)
if err != nil {
if repo_model.IsErrAttachmentNotExist(err) {
ctx.Error(http.StatusNotFound)
Expand Down Expand Up @@ -153,3 +153,8 @@ func GetAttachment(ctx *context.Context) {
return
}
}

// GetAttachment serve attachments
func GetAttachment(ctx *context.Context) {
ServeAttachment(ctx, ctx.Params(":uuid"))
}
6 changes: 6 additions & 0 deletions routers/web/repo/release.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,10 @@ func releasesOrTags(ctx *context.Context, isTagList bool) {
return
}

for _, release := range releases {
release.Repo = ctx.Repo.Repository
}

if err = repo_model.GetReleaseAttachments(ctx, releases...); err != nil {
ctx.ServerError("GetReleaseAttachments", err)
return
Expand Down Expand Up @@ -248,6 +252,8 @@ func SingleRelease(ctx *context.Context) {
ctx.Data["Title"] = release.Title
}

release.Repo = ctx.Repo.Repository

err = repo_model.GetReleaseAttachments(ctx, release)
if err != nil {
ctx.ServerError("GetReleaseAttachments", err)
Expand Down
2 changes: 1 addition & 1 deletion routers/web/repo/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ func RedirectDownload(ctx *context.Context) {
return
}
if att != nil {
ctx.Redirect(att.DownloadURL())
ServeAttachment(ctx, att.UUID)
return
}
}
Expand Down