Skip to content

Commit d3dc07f

Browse files
al-sabrtechknowlogick
authored andcommitted
Added URL mapping for Release attachments like on github.com (#1707)
1 parent dd006db commit d3dc07f

File tree

5 files changed

+71
-9
lines changed

5 files changed

+71
-9
lines changed

models/attachment.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,11 @@ func GetAttachmentByUUID(uuid string) (*Attachment, error) {
151151
return getAttachmentByUUID(x, uuid)
152152
}
153153

154+
// GetAttachmentByReleaseIDFileName returns attachment by given releaseId and fileName.
155+
func GetAttachmentByReleaseIDFileName(releaseID int64, fileName string) (*Attachment, error) {
156+
return getAttachmentByReleaseIDFileName(x, releaseID, fileName)
157+
}
158+
154159
func getAttachmentsByIssueID(e Engine, issueID int64) ([]*Attachment, error) {
155160
attachments := make([]*Attachment, 0, 10)
156161
return attachments, e.Where("issue_id = ? AND comment_id = 0", issueID).Find(&attachments)
@@ -171,6 +176,18 @@ func getAttachmentsByCommentID(e Engine, commentID int64) ([]*Attachment, error)
171176
return attachments, x.Where("comment_id=?", commentID).Find(&attachments)
172177
}
173178

179+
// getAttachmentByReleaseIDFileName return a file based on the the following infos:
180+
func getAttachmentByReleaseIDFileName(e Engine, releaseID int64, fileName string) (*Attachment, error) {
181+
attach := &Attachment{ReleaseID: releaseID, Name: fileName}
182+
has, err := e.Get(attach)
183+
if err != nil {
184+
return nil, err
185+
} else if !has {
186+
return nil, err
187+
}
188+
return attach, nil
189+
}
190+
174191
// DeleteAttachment deletes the given attachment and optionally the associated file.
175192
func DeleteAttachment(a *Attachment, remove bool) error {
176193
_, err := DeleteAttachments([]*Attachment{a}, remove)

models/release.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"code.gitea.io/gitea/modules/process"
1515
"code.gitea.io/gitea/modules/setting"
1616
"code.gitea.io/gitea/modules/util"
17+
1718
api "code.gitea.io/sdk/gitea"
1819
"github.com/go-xorm/builder"
1920
)
@@ -283,6 +284,15 @@ func GetReleasesByRepoID(repoID int64, opts FindReleasesOptions, page, pageSize
283284
return rels, err
284285
}
285286

287+
// GetReleasesByRepoIDAndNames returns a list of releases of repository according repoID and tagNames.
288+
func GetReleasesByRepoIDAndNames(repoID int64, tagNames []string) (rels []*Release, err error) {
289+
err = x.
290+
Desc("created_unix").
291+
In("tag_name", tagNames).
292+
Find(&rels, Release{RepoID: repoID})
293+
return rels, err
294+
}
295+
286296
// GetReleaseCountByRepoID returns the count of releases of repository
287297
func GetReleaseCountByRepoID(repoID int64, opts FindReleasesOptions) (int64, error) {
288298
return x.Where(opts.toConds(repoID)).Count(&Release{})

routers/repo/repo.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,38 @@ func Action(ctx *context.Context) {
310310
ctx.RedirectToFirst(ctx.Query("redirect_to"), ctx.Repo.RepoLink)
311311
}
312312

313+
// RedirectDownload return a file based on the following infos:
314+
func RedirectDownload(ctx *context.Context) {
315+
var (
316+
vTag = ctx.Params("vTag")
317+
fileName = ctx.Params("fileName")
318+
)
319+
tagNames := []string{vTag}
320+
curRepo := ctx.Repo.Repository
321+
releases, err := models.GetReleasesByRepoIDAndNames(curRepo.ID, tagNames)
322+
if err != nil {
323+
if models.IsErrAttachmentNotExist(err) {
324+
ctx.Error(404)
325+
return
326+
}
327+
ctx.ServerError("RedirectDownload", err)
328+
return
329+
}
330+
if len(releases) == 1 {
331+
release := releases[0]
332+
att, err := models.GetAttachmentByReleaseIDFileName(release.ID, fileName)
333+
if err != nil {
334+
ctx.Error(404)
335+
return
336+
}
337+
if att != nil {
338+
ctx.Redirect(setting.AppSubURL + "/attachments/" + att.UUID)
339+
return
340+
}
341+
}
342+
ctx.Error(404)
343+
}
344+
313345
// Download download an archive of a repository
314346
func Download(ctx *context.Context) {
315347
var (

routers/routes/routes.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,9 @@ func RegisterRoutes(m *macaron.Macaron) {
475475
}, context.RepoIDAssignment(), context.UnitTypes(), reqRepoCodeReader)
476476
}, reqSignIn)
477477

478+
// ***** Release Attachment Download without Signin
479+
m.Get("/:username/:reponame/releases/download/:vTag/:fileName", ignSignIn, context.RepoAssignment(), repo.MustBeNotBare, repo.RedirectDownload)
480+
478481
m.Group("/:username/:reponame", func() {
479482
m.Group("/settings", func() {
480483
m.Combo("").Get(repo.Settings).

templates/repo/release/list.tmpl

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
{{end}}
1515
</h2>
1616
<ul id="release-list">
17-
{{range .Releases}}
17+
{{range $release := .Releases}}
1818
<li class="ui grid">
1919
<div class="ui four wide column meta">
2020
{{if .IsTag}}
@@ -75,14 +75,14 @@
7575
</li>
7676
{{end}}
7777
{{if .Attachments}}
78-
{{range .Attachments}}
79-
<li>
80-
<a target="_blank" rel="noopener noreferrer" href="{{AppSubUrl}}/attachments/{{.UUID}}">
81-
<strong><span class="ui image octicon octicon-package" title='{{.Name}}'></span> {{.Name}}</strong>
82-
<span class="ui text grey right">{{.Size | FileSize}}</span>
83-
</a>
84-
</li>
85-
{{end}}
78+
{{range $attachment := .Attachments}}
79+
<li>
80+
<a target="_blank" rel="noopener noreferrer" href="{{$.RepoLink}}/releases/download/{{$release.TagName}}/{{$attachment.Name}}">
81+
<strong><span class="ui image octicon octicon-package" title='{{$attachment.Name}}'></span> {{$attachment.Name}}</strong>
82+
<span class="ui text grey right">{{$attachment.Size | FileSize}}</span>
83+
</a>
84+
</li>
85+
{{end}}
8686
{{end}}
8787
</ul>
8888
</div>

0 commit comments

Comments
 (0)