fix(packages): use file names for generic web downloads (#37514)

Fixes #37511.

Serve Generic package web asset downloads with the stored package filename

Signed-off-by: cyphercodes <cyphercodes@users.noreply.github.com>
Co-authored-by: cyphercodes <cyphercodes@users.noreply.github.com>
Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
This commit is contained in:
Rayan Salhab
2026-05-03 10:19:21 +03:00
committed by GitHub
parent 0385e4783e
commit 7016f7b37f
3 changed files with 29 additions and 2 deletions

View File

@@ -68,7 +68,7 @@ func TryInsertFile(ctx context.Context, pf *PackageFile) (*PackageFile, error) {
// GetFilesByVersionID gets all files of a version
func GetFilesByVersionID(ctx context.Context, versionID int64) ([]*PackageFile, error) {
pfs := make([]*PackageFile, 0, 10)
return pfs, db.GetEngine(ctx).Where("version_id = ?", versionID).Find(&pfs)
return pfs, db.GetEngine(ctx).Where("version_id = ?", versionID).OrderBy("lower_name, created_unix, id").Find(&pfs)
}
// GetFileForVersionByID gets a file of a version by id

View File

@@ -566,7 +566,11 @@ func DownloadPackageFile(ctx *context.Context) {
return
}
packages_helper.ServePackageFile(ctx, s, u, pf)
packages_helper.ServePackageFile(ctx, s, u, pf, httplib.ServeHeaderOptions{
Filename: pf.Name,
LastModified: pf.CreatedUnix.AsLocalTime(),
ContentDisposition: httplib.ContentDispositionAttachment,
})
}
// ActionPackageTerraformLock locks a terraform state

View File

@@ -7,7 +7,9 @@ import (
"bytes"
"fmt"
"io"
"mime"
"net/http"
neturl "net/url"
"testing"
"code.gitea.io/gitea/models/packages"
@@ -173,6 +175,27 @@ func TestPackageGeneric(t *testing.T) {
checkDownloadCount(3)
})
t.Run("WebAssetUsesFilename", func(t *testing.T) {
defer tests.PrintCurrentTest(t)()
pvs, err := packages.GetVersionsByPackageType(t.Context(), user.ID, packages.TypeGeneric)
assert.NoError(t, err)
assert.Len(t, pvs, 1)
pfs, err := packages.GetFilesByVersionID(t.Context(), pvs[0].ID)
assert.NoError(t, err)
assert.NotEmpty(t, pfs)
req = NewRequest(t, "GET", fmt.Sprintf("/%s/-/packages/generic/%s/%s/files/%d", user.Name, neturl.PathEscape(packageName), neturl.PathEscape(packageVersion), pfs[0].ID))
resp = MakeRequest(t, req, http.StatusOK)
assert.Equal(t, content, resp.Body.Bytes())
disposition, params, err := mime.ParseMediaType(resp.Header().Get("Content-Disposition"))
assert.NoError(t, err)
assert.Equal(t, "attachment", disposition)
assert.Equal(t, pfs[0].Name, params["filename"])
})
})
t.Run("Delete", func(t *testing.T) {