0
Fork 0
mirror of https://codeberg.org/forgejo/forgejo.git synced 2025-02-21 13:26:24 -05:00
forgejo/models/repo/archive_download_count.go
0ko 8178b4e98b chore: fix typos, decap a few i18n strings (#6666)
A few typos, some from spell checker, some from grepping `the the`, `the a `, ` a the`. Some in comments and some in changelogs.

Decapped a few remaining annoying strings, and one new from https://codeberg.org/forgejo/forgejo/pulls/6351 which was introduced by copy-pasting on a slightly out-of-date branch, causing Weblate to issue "reused translation" alerts again. Ref #6439.

Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/6666
Reviewed-by: Earl Warren <earl-warren@noreply.codeberg.org>
Reviewed-by: Gusted <gusted@noreply.codeberg.org>
Co-authored-by: 0ko <0ko@noreply.codeberg.org>
Co-committed-by: 0ko <0ko@noreply.codeberg.org>
2025-01-24 05:41:59 +00:00

90 lines
2.7 KiB
Go

// Copyright 2024 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package repo
import (
"context"
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/modules/git"
api "code.gitea.io/gitea/modules/structs"
)
// RepoArchiveDownloadCount counts all archive downloads for a tag
type RepoArchiveDownloadCount struct { //nolint:revive
ID int64 `xorm:"pk autoincr"`
RepoID int64 `xorm:"index unique(s)"`
ReleaseID int64 `xorm:"index unique(s)"`
Type git.ArchiveType `xorm:"unique(s)"`
Count int64
}
func init() {
db.RegisterModel(new(RepoArchiveDownloadCount))
}
// CountArchiveDownload adds one download the given archive
func CountArchiveDownload(ctx context.Context, repoID, releaseID int64, tp git.ArchiveType) error {
updateCount, err := db.GetEngine(ctx).Where("repo_id = ?", repoID).And("release_id = ?", releaseID).And("`type` = ?", tp).Incr("count").Update(new(RepoArchiveDownloadCount))
if err != nil {
return err
}
if updateCount != 0 {
// The count was updated, so we can exit
return nil
}
// The archive does not esxists in the database, so let's add it
newCounter := &RepoArchiveDownloadCount{
RepoID: repoID,
ReleaseID: releaseID,
Type: tp,
Count: 1,
}
_, err = db.GetEngine(ctx).Insert(newCounter)
return err
}
// GetArchiveDownloadCount returns the download count of a tag
func GetArchiveDownloadCount(ctx context.Context, repoID, releaseID int64) (*api.TagArchiveDownloadCount, error) {
downloadCountList := make([]RepoArchiveDownloadCount, 0)
err := db.GetEngine(ctx).Where("repo_id = ?", repoID).And("release_id = ?", releaseID).Find(&downloadCountList)
if err != nil {
return nil, err
}
tagCounter := new(api.TagArchiveDownloadCount)
for _, singleCount := range downloadCountList {
switch singleCount.Type {
case git.ZIP:
tagCounter.Zip = singleCount.Count
case git.TARGZ:
tagCounter.TarGz = singleCount.Count
}
}
return tagCounter, nil
}
// GetDownloadCountForTagName returns the download count of a tag with the given name
func GetArchiveDownloadCountForTagName(ctx context.Context, repoID int64, tagName string) (*api.TagArchiveDownloadCount, error) {
release, err := GetRelease(ctx, repoID, tagName)
if err != nil {
if IsErrReleaseNotExist(err) {
return new(api.TagArchiveDownloadCount), nil
}
return nil, err
}
return GetArchiveDownloadCount(ctx, repoID, release.ID)
}
// DeleteArchiveDownloadCountForRelease deletes the release from the repo_archive_download_count table
func DeleteArchiveDownloadCountForRelease(ctx context.Context, releaseID int64) error {
_, err := db.GetEngine(ctx).Delete(&RepoArchiveDownloadCount{ReleaseID: releaseID})
return err
}