allow "latest" to be used in release vTag when downloading file (#26748)

Hello,

In the discord I saw
[someone](https://discord.com/channels/322538954119184384/1069795723178160168/1145061200644800514)
complaining that you can't use the "latest" keyword as release tag to
download a specific file:
In his example:

https://www.uberwald.me/gitea/public/fvtt-ecryme/releases/latest/system.json

However the latest keyword works for the release page, so I think it's a
good thing to implement this on the release attachment download url too.

---------

Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
This commit is contained in:
merlleu 2023-09-06 13:06:04 +02:00 committed by GitHub
parent 9b0743ae33
commit a7d9a70552
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -378,10 +378,6 @@ func RedirectDownload(ctx *context.Context) {
curRepo := ctx.Repo.Repository curRepo := ctx.Repo.Repository
releases, err := repo_model.GetReleasesByRepoIDAndNames(ctx, curRepo.ID, tagNames) releases, err := repo_model.GetReleasesByRepoIDAndNames(ctx, curRepo.ID, tagNames)
if err != nil { if err != nil {
if repo_model.IsErrAttachmentNotExist(err) {
ctx.Error(http.StatusNotFound)
return
}
ctx.ServerError("RedirectDownload", err) ctx.ServerError("RedirectDownload", err)
return return
} }
@ -396,6 +392,23 @@ func RedirectDownload(ctx *context.Context) {
ServeAttachment(ctx, att.UUID) ServeAttachment(ctx, att.UUID)
return return
} }
} else if len(releases) == 0 && vTag == "latest" {
// GitHub supports the alias "latest" for the latest release
// We only fetch the latest release if the tag is "latest" and no release with the tag "latest" exists
release, err := repo_model.GetLatestReleaseByRepoID(ctx.Repo.Repository.ID)
if err != nil {
ctx.Error(http.StatusNotFound)
return
}
att, err := repo_model.GetAttachmentByReleaseIDFileName(ctx, release.ID, fileName)
if err != nil {
ctx.Error(http.StatusNotFound)
return
}
if att != nil {
ServeAttachment(ctx, att.UUID)
return
}
} }
ctx.Error(http.StatusNotFound) ctx.Error(http.StatusNotFound)
} }