2022-03-30 11:42:47 +03:00
|
|
|
// Copyright 2014 The Gogs Authors. All rights reserved.
|
2022-11-27 21:20:29 +03:00
|
|
|
// SPDX-License-Identifier: MIT
|
2022-03-30 11:42:47 +03:00
|
|
|
|
|
|
|
package admin
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2023-08-08 03:46:10 +03:00
|
|
|
"time"
|
2022-03-30 11:42:47 +03:00
|
|
|
|
|
|
|
"code.gitea.io/gitea/models/db"
|
|
|
|
packages_model "code.gitea.io/gitea/models/packages"
|
|
|
|
"code.gitea.io/gitea/modules/base"
|
2024-03-02 18:42:31 +03:00
|
|
|
"code.gitea.io/gitea/modules/optional"
|
2022-03-30 11:42:47 +03:00
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2024-02-27 10:12:22 +03:00
|
|
|
"code.gitea.io/gitea/services/context"
|
2022-03-30 11:42:47 +03:00
|
|
|
packages_service "code.gitea.io/gitea/services/packages"
|
2023-08-08 03:46:10 +03:00
|
|
|
packages_cleanup_service "code.gitea.io/gitea/services/packages/cleanup"
|
2022-03-30 11:42:47 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
tplPackagesList base.TplName = "admin/packages/list"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Packages shows all packages
|
|
|
|
func Packages(ctx *context.Context) {
|
|
|
|
page := ctx.FormInt("page")
|
|
|
|
if page <= 1 {
|
|
|
|
page = 1
|
|
|
|
}
|
|
|
|
query := ctx.FormTrim("q")
|
|
|
|
packageType := ctx.FormTrim("type")
|
|
|
|
sort := ctx.FormTrim("sort")
|
|
|
|
|
|
|
|
pvs, total, err := packages_model.SearchVersions(ctx, &packages_model.PackageSearchOptions{
|
2022-07-27 04:59:10 +03:00
|
|
|
Type: packages_model.Type(packageType),
|
|
|
|
Name: packages_model.SearchValue{Value: query},
|
|
|
|
Sort: sort,
|
2024-03-02 18:42:31 +03:00
|
|
|
IsInternal: optional.Some(false),
|
2022-03-30 11:42:47 +03:00
|
|
|
Paginator: &db.ListOptions{
|
|
|
|
PageSize: setting.UI.PackagesPagingNum,
|
|
|
|
Page: page,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
ctx.ServerError("SearchVersions", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
pds, err := packages_model.GetPackageDescriptors(ctx, pvs)
|
|
|
|
if err != nil {
|
|
|
|
ctx.ServerError("GetPackageDescriptors", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-01-18 18:52:04 +03:00
|
|
|
totalBlobSize, err := packages_model.GetTotalBlobSize(ctx)
|
2022-03-30 11:42:47 +03:00
|
|
|
if err != nil {
|
|
|
|
ctx.ServerError("GetTotalBlobSize", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-01-18 18:52:04 +03:00
|
|
|
totalUnreferencedBlobSize, err := packages_model.GetTotalUnreferencedBlobSize(ctx)
|
|
|
|
if err != nil {
|
|
|
|
ctx.ServerError("CalculateBlobSize", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-03-30 11:42:47 +03:00
|
|
|
ctx.Data["Title"] = ctx.Tr("packages.title")
|
|
|
|
ctx.Data["PageIsAdminPackages"] = true
|
|
|
|
ctx.Data["Query"] = query
|
|
|
|
ctx.Data["PackageType"] = packageType
|
2022-12-29 02:31:54 +03:00
|
|
|
ctx.Data["AvailableTypes"] = packages_model.TypeList
|
2022-03-30 11:42:47 +03:00
|
|
|
ctx.Data["SortType"] = sort
|
|
|
|
ctx.Data["PackageDescriptors"] = pds
|
2023-01-18 18:52:04 +03:00
|
|
|
ctx.Data["TotalCount"] = total
|
|
|
|
ctx.Data["TotalBlobSize"] = totalBlobSize - totalUnreferencedBlobSize
|
|
|
|
ctx.Data["TotalUnreferencedBlobSize"] = totalUnreferencedBlobSize
|
2022-03-30 11:42:47 +03:00
|
|
|
|
|
|
|
pager := context.NewPagination(int(total), setting.UI.PackagesPagingNum, page, 5)
|
|
|
|
pager.AddParamString("q", query)
|
|
|
|
pager.AddParamString("type", packageType)
|
|
|
|
pager.AddParamString("sort", sort)
|
|
|
|
ctx.Data["Page"] = pager
|
|
|
|
|
|
|
|
ctx.HTML(http.StatusOK, tplPackagesList)
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeletePackageVersion deletes a package version
|
|
|
|
func DeletePackageVersion(ctx *context.Context) {
|
2023-07-24 06:47:27 +03:00
|
|
|
pv, err := packages_model.GetVersionByID(ctx, ctx.FormInt64("id"))
|
2022-03-30 11:42:47 +03:00
|
|
|
if err != nil {
|
|
|
|
ctx.ServerError("GetRepositoryByID", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-09-25 16:17:37 +03:00
|
|
|
if err := packages_service.RemovePackageVersion(ctx, ctx.Doer, pv); err != nil {
|
2022-03-30 11:42:47 +03:00
|
|
|
ctx.ServerError("RemovePackageVersion", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Flash.Success(ctx.Tr("packages.settings.delete.success"))
|
2023-07-26 09:04:01 +03:00
|
|
|
ctx.JSONRedirect(setting.AppSubURL + "/admin/packages?page=" + url.QueryEscape(ctx.FormString("page")) + "&q=" + url.QueryEscape(ctx.FormString("q")) + "&type=" + url.QueryEscape(ctx.FormString("type")))
|
2022-03-30 11:42:47 +03:00
|
|
|
}
|
2023-08-08 03:46:10 +03:00
|
|
|
|
|
|
|
func CleanupExpiredData(ctx *context.Context) {
|
|
|
|
if err := packages_cleanup_service.CleanupExpiredData(ctx, time.Duration(0)); err != nil {
|
|
|
|
ctx.ServerError("CleanupExpiredData", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-01-19 14:37:10 +03:00
|
|
|
ctx.Flash.Success(ctx.Tr("admin.packages.cleanup.success"))
|
2023-08-08 03:46:10 +03:00
|
|
|
ctx.Redirect(setting.AppSubURL + "/admin/packages")
|
|
|
|
}
|