Count downloads for tag archives

This commit is contained in:
JakobDev 2024-04-02 16:34:57 +02:00
parent f8a5d6872c
commit 613e5387c5
22 changed files with 469 additions and 95 deletions

View file

@ -302,7 +302,7 @@ func GetArchive(ctx *context.APIContext) {
func archiveDownload(ctx *context.APIContext) {
uri := ctx.Params("*")
aReq, err := archiver_service.NewRequest(ctx.Repo.Repository.ID, ctx.Repo.GitRepo, uri)
aReq, err := archiver_service.NewRequest(ctx, ctx.Repo.Repository.ID, ctx.Repo.GitRepo, uri)
if err != nil {
if errors.Is(err, archiver_service.ErrUnknownArchiveFormat{}) {
ctx.Error(http.StatusBadRequest, "unknown archive format", err)

View file

@ -60,6 +60,12 @@ func ListTags(ctx *context.APIContext) {
apiTags := make([]*api.Tag, len(tags))
for i := range tags {
tags[i].ArchiveDownloadCount, err = repo_model.GetArchiveDownloadCountForTagName(ctx, ctx.Repo.Repository.ID, tags[i].Name)
if err != nil {
ctx.Error(http.StatusInternalServerError, "GetTagArchiveDownloadCountForName", err)
return
}
apiTags[i] = convert.ToTag(ctx.Repo.Repository, tags[i])
}
@ -111,6 +117,13 @@ func GetAnnotatedTag(ctx *context.APIContext) {
if err != nil {
ctx.Error(http.StatusBadRequest, "GetAnnotatedTag", err)
}
tag.ArchiveDownloadCount, err = repo_model.GetArchiveDownloadCountForTagName(ctx, ctx.Repo.Repository.ID, tag.Name)
if err != nil {
ctx.Error(http.StatusInternalServerError, "GetTagArchiveDownloadCountForName", err)
return
}
ctx.JSON(http.StatusOK, convert.ToAnnotatedTag(ctx, ctx.Repo.Repository, tag, commit))
}
}
@ -150,6 +163,13 @@ func GetTag(ctx *context.APIContext) {
ctx.NotFound(tagName)
return
}
tag.ArchiveDownloadCount, err = repo_model.GetArchiveDownloadCountForTagName(ctx, ctx.Repo.Repository.ID, tag.Name)
if err != nil {
ctx.Error(http.StatusInternalServerError, "GetTagArchiveDownloadCountForName", err)
return
}
ctx.JSON(http.StatusOK, convert.ToTag(ctx.Repo.Repository, tag))
}
@ -218,6 +238,13 @@ func CreateTag(ctx *context.APIContext) {
ctx.InternalServerError(err)
return
}
tag.ArchiveDownloadCount, err = repo_model.GetArchiveDownloadCountForTagName(ctx, ctx.Repo.Repository.ID, tag.Name)
if err != nil {
ctx.Error(http.StatusInternalServerError, "GetTagArchiveDownloadCountForName", err)
return
}
ctx.JSON(http.StatusCreated, convert.ToTag(ctx.Repo.Repository, tag))
}

View file

@ -127,6 +127,11 @@ func getReleaseInfos(ctx *context.Context, opts *repo_model.FindReleasesOptions)
return nil, err
}
err = r.LoadArchiveDownloadCount(ctx)
if err != nil {
return nil, err
}
if !r.IsDraft {
if err := calReleaseNumCommitsBehind(ctx.Repo, r, countCache); err != nil {
return nil, err
@ -355,6 +360,12 @@ func SingleRelease(ctx *context.Context) {
ctx.Data["Title"] = release.Title
}
err = release.LoadArchiveDownloadCount(ctx)
if err != nil {
ctx.ServerError("LoadArchiveDownloadCount", err)
return
}
ctx.Data["Releases"] = releases
ctx.HTML(http.StatusOK, tplReleasesList)
}

View file

@ -456,7 +456,7 @@ func RedirectDownload(ctx *context.Context) {
// Download an archive of a repository
func Download(ctx *context.Context) {
uri := ctx.Params("*")
aReq, err := archiver_service.NewRequest(ctx.Repo.Repository.ID, ctx.Repo.GitRepo, uri)
aReq, err := archiver_service.NewRequest(ctx, ctx.Repo.Repository.ID, ctx.Repo.GitRepo, uri)
if err != nil {
if errors.Is(err, archiver_service.ErrUnknownArchiveFormat{}) {
ctx.Error(http.StatusBadRequest, err.Error())
@ -485,6 +485,14 @@ func download(ctx *context.Context, archiveName string, archiver *repo_model.Rep
// If we have a signed url (S3, object storage), redirect to this directly.
u, err := storage.RepoArchives.URL(rPath, downloadName)
if u != nil && err == nil {
if archiver.ReleaseID != 0 {
err = repo_model.CountArchiveDownload(ctx, ctx.Repo.Repository.ID, archiver.ReleaseID, archiver.Type)
if err != nil {
ctx.ServerError("CountArchiveDownload", err)
return
}
}
ctx.Redirect(u.String())
return
}
@ -498,6 +506,14 @@ func download(ctx *context.Context, archiveName string, archiver *repo_model.Rep
}
defer fr.Close()
if archiver.ReleaseID != 0 {
err = repo_model.CountArchiveDownload(ctx, ctx.Repo.Repository.ID, archiver.ReleaseID, archiver.Type)
if err != nil {
ctx.ServerError("CountArchiveDownload", err)
return
}
}
ctx.ServeContent(fr, &context.ServeHeaderOptions{
Filename: downloadName,
LastModified: archiver.CreatedUnix.AsLocalTime(),
@ -509,7 +525,7 @@ func download(ctx *context.Context, archiveName string, archiver *repo_model.Rep
// kind of drop it on the floor if this is the case.
func InitiateDownload(ctx *context.Context) {
uri := ctx.Params("*")
aReq, err := archiver_service.NewRequest(ctx.Repo.Repository.ID, ctx.Repo.GitRepo, uri)
aReq, err := archiver_service.NewRequest(ctx, ctx.Repo.Repository.ID, ctx.Repo.GitRepo, uri)
if err != nil {
ctx.ServerError("archiver_service.NewRequest", err)
return