summaryrefslogtreecommitdiffstats
path: root/models/repo/archive_download_count.go
blob: 31f0399d53ae24839a69577ae791399063e0b355 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// 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 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
}