2024-08-04 09:16:29 +03:00
|
|
|
// Copyright 2023 The Gitea Authors. All rights reserved.
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
|
|
|
package arch
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/base64"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
2024-08-11 13:35:11 +03:00
|
|
|
"regexp"
|
2024-08-04 09:16:29 +03:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
packages_model "code.gitea.io/gitea/models/packages"
|
|
|
|
packages_module "code.gitea.io/gitea/modules/packages"
|
|
|
|
arch_module "code.gitea.io/gitea/modules/packages/arch"
|
|
|
|
"code.gitea.io/gitea/modules/util"
|
|
|
|
"code.gitea.io/gitea/routers/api/packages/helper"
|
|
|
|
"code.gitea.io/gitea/services/context"
|
|
|
|
packages_service "code.gitea.io/gitea/services/packages"
|
|
|
|
arch_service "code.gitea.io/gitea/services/packages/arch"
|
|
|
|
)
|
|
|
|
|
2024-08-11 13:35:11 +03:00
|
|
|
var (
|
|
|
|
archPkgOrSig = regexp.MustCompile(`^.*\.pkg\.tar\.\w+(\.sig)*$`)
|
|
|
|
archDBOrSig = regexp.MustCompile(`^.*.db(\.tar\.gz)*(\.sig)*$`)
|
|
|
|
)
|
|
|
|
|
2024-08-04 09:16:29 +03:00
|
|
|
func apiError(ctx *context.Context, status int, obj any) {
|
|
|
|
helper.LogAndProcessError(ctx, status, obj, func(message string) {
|
|
|
|
ctx.PlainText(status, message)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetRepositoryKey(ctx *context.Context) {
|
|
|
|
_, pub, err := arch_service.GetOrCreateKeyPair(ctx, ctx.Package.Owner.ID)
|
|
|
|
if err != nil {
|
|
|
|
apiError(ctx, http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.ServeContent(strings.NewReader(pub), &context.ServeHeaderOptions{
|
|
|
|
ContentType: "application/pgp-keys",
|
|
|
|
Filename: "repository.key",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func PushPackage(ctx *context.Context) {
|
2024-08-11 13:35:11 +03:00
|
|
|
group := ctx.Params("group")
|
2024-08-04 09:16:29 +03:00
|
|
|
|
|
|
|
upload, needToClose, err := ctx.UploadStream()
|
|
|
|
if err != nil {
|
|
|
|
apiError(ctx, http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if needToClose {
|
|
|
|
defer upload.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
buf, err := packages_module.CreateHashedBufferFromReader(upload)
|
|
|
|
if err != nil {
|
|
|
|
apiError(ctx, http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer buf.Close()
|
|
|
|
|
|
|
|
p, err := arch_module.ParsePackage(buf)
|
|
|
|
if err != nil {
|
2024-08-11 13:35:11 +03:00
|
|
|
apiError(ctx, http.StatusBadRequest, err)
|
2024-08-04 09:16:29 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = buf.Seek(0, io.SeekStart)
|
|
|
|
if err != nil {
|
|
|
|
apiError(ctx, http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
sign, err := arch_service.NewFileSign(ctx, ctx.Package.Owner.ID, buf)
|
|
|
|
if err != nil {
|
|
|
|
apiError(ctx, http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer sign.Close()
|
|
|
|
_, err = buf.Seek(0, io.SeekStart)
|
|
|
|
if err != nil {
|
|
|
|
apiError(ctx, http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// update gpg sign
|
|
|
|
pgp, err := io.ReadAll(sign)
|
|
|
|
if err != nil {
|
|
|
|
apiError(ctx, http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
p.FileMetadata.PgpSigned = base64.StdEncoding.EncodeToString(pgp)
|
|
|
|
_, err = sign.Seek(0, io.SeekStart)
|
|
|
|
if err != nil {
|
|
|
|
apiError(ctx, http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
properties := map[string]string{
|
|
|
|
arch_module.PropertyDescription: p.Desc(),
|
|
|
|
arch_module.PropertyArch: p.FileMetadata.Arch,
|
2024-08-11 13:35:11 +03:00
|
|
|
arch_module.PropertyDistribution: group,
|
2024-08-04 09:16:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
version, _, err := packages_service.CreatePackageOrAddFileToExisting(
|
|
|
|
ctx,
|
|
|
|
&packages_service.PackageCreationInfo{
|
|
|
|
PackageInfo: packages_service.PackageInfo{
|
|
|
|
Owner: ctx.Package.Owner,
|
|
|
|
PackageType: packages_model.TypeArch,
|
|
|
|
Name: p.Name,
|
|
|
|
Version: p.Version,
|
|
|
|
},
|
|
|
|
Creator: ctx.Doer,
|
|
|
|
Metadata: p.VersionMetadata,
|
|
|
|
},
|
|
|
|
&packages_service.PackageFileCreationInfo{
|
|
|
|
PackageFileInfo: packages_service.PackageFileInfo{
|
2024-08-11 13:35:11 +03:00
|
|
|
Filename: fmt.Sprintf("%s-%s-%s.pkg.tar.%s", p.Name, p.Version, p.FileMetadata.Arch, p.CompressType),
|
|
|
|
CompositeKey: group,
|
2024-08-04 09:16:29 +03:00
|
|
|
},
|
|
|
|
OverwriteExisting: false,
|
|
|
|
IsLead: true,
|
|
|
|
Creator: ctx.ContextUser,
|
|
|
|
Data: buf,
|
|
|
|
Properties: properties,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
switch {
|
|
|
|
case errors.Is(err, packages_model.ErrDuplicatePackageVersion), errors.Is(err, packages_model.ErrDuplicatePackageFile):
|
|
|
|
apiError(ctx, http.StatusConflict, err)
|
|
|
|
case errors.Is(err, packages_service.ErrQuotaTotalCount), errors.Is(err, packages_service.ErrQuotaTypeSize), errors.Is(err, packages_service.ErrQuotaTotalSize):
|
|
|
|
apiError(ctx, http.StatusForbidden, err)
|
|
|
|
default:
|
|
|
|
apiError(ctx, http.StatusInternalServerError, err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// add sign file
|
|
|
|
_, err = packages_service.AddFileToPackageVersionInternal(ctx, version, &packages_service.PackageFileCreationInfo{
|
|
|
|
PackageFileInfo: packages_service.PackageFileInfo{
|
2024-08-11 13:35:11 +03:00
|
|
|
CompositeKey: group,
|
|
|
|
Filename: fmt.Sprintf("%s-%s-%s.pkg.tar.%s.sig", p.Name, p.Version, p.FileMetadata.Arch, p.CompressType),
|
2024-08-04 09:16:29 +03:00
|
|
|
},
|
|
|
|
OverwriteExisting: true,
|
|
|
|
IsLead: false,
|
|
|
|
Creator: ctx.Doer,
|
|
|
|
Data: sign,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
apiError(ctx, http.StatusInternalServerError, err)
|
|
|
|
}
|
2024-08-11 13:35:11 +03:00
|
|
|
if err = arch_service.BuildPacmanDB(ctx, ctx.Package.Owner.ID, group, p.FileMetadata.Arch); err != nil {
|
2024-08-04 09:16:29 +03:00
|
|
|
apiError(ctx, http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ctx.Status(http.StatusCreated)
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetPackageOrDB(ctx *context.Context) {
|
|
|
|
var (
|
2024-08-11 13:35:11 +03:00
|
|
|
file = ctx.Params("file")
|
|
|
|
group = ctx.Params("group")
|
|
|
|
arch = ctx.Params("arch")
|
2024-08-04 09:16:29 +03:00
|
|
|
)
|
2024-08-11 13:35:11 +03:00
|
|
|
if archPkgOrSig.MatchString(file) {
|
|
|
|
pkg, err := arch_service.GetPackageFile(ctx, group, file, ctx.Package.Owner.ID)
|
2024-08-04 09:16:29 +03:00
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, util.ErrNotExist) {
|
|
|
|
apiError(ctx, http.StatusNotFound, err)
|
|
|
|
} else {
|
|
|
|
apiError(ctx, http.StatusInternalServerError, err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.ServeContent(pkg, &context.ServeHeaderOptions{
|
|
|
|
Filename: file,
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-08-11 13:35:11 +03:00
|
|
|
if archDBOrSig.MatchString(file) {
|
|
|
|
pkg, err := arch_service.GetPackageDBFile(ctx, group, arch, ctx.Package.Owner.ID,
|
2024-08-04 09:16:29 +03:00
|
|
|
strings.HasSuffix(file, ".sig"))
|
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, util.ErrNotExist) {
|
|
|
|
apiError(ctx, http.StatusNotFound, err)
|
|
|
|
} else {
|
|
|
|
apiError(ctx, http.StatusInternalServerError, err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ctx.ServeContent(pkg, &context.ServeHeaderOptions{
|
|
|
|
Filename: file,
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Status(http.StatusNotFound)
|
|
|
|
}
|
|
|
|
|
|
|
|
func RemovePackage(ctx *context.Context) {
|
|
|
|
var (
|
2024-08-11 13:35:11 +03:00
|
|
|
group = ctx.Params("group")
|
|
|
|
pkg = ctx.Params("package")
|
|
|
|
ver = ctx.Params("version")
|
2024-08-04 09:16:29 +03:00
|
|
|
)
|
|
|
|
pv, err := packages_model.GetVersionByNameAndVersion(
|
|
|
|
ctx, ctx.Package.Owner.ID, packages_model.TypeArch, pkg, ver,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, util.ErrNotExist) {
|
|
|
|
apiError(ctx, http.StatusNotFound, err)
|
|
|
|
} else {
|
|
|
|
apiError(ctx, http.StatusInternalServerError, err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
files, err := packages_model.GetFilesByVersionID(ctx, pv.ID)
|
|
|
|
if err != nil {
|
|
|
|
apiError(ctx, http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
deleted := false
|
|
|
|
for _, file := range files {
|
2024-08-11 13:35:11 +03:00
|
|
|
if file.CompositeKey == group {
|
2024-08-04 09:16:29 +03:00
|
|
|
deleted = true
|
|
|
|
err := packages_service.RemovePackageFileAndVersionIfUnreferenced(ctx, ctx.ContextUser, file)
|
|
|
|
if err != nil {
|
|
|
|
apiError(ctx, http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if deleted {
|
2024-08-11 13:35:11 +03:00
|
|
|
err = arch_service.BuildCustomRepositoryFiles(ctx, ctx.Package.Owner.ID, group)
|
2024-08-04 09:16:29 +03:00
|
|
|
if err != nil {
|
|
|
|
apiError(ctx, http.StatusInternalServerError, err)
|
|
|
|
}
|
|
|
|
ctx.Status(http.StatusNoContent)
|
|
|
|
} else {
|
|
|
|
ctx.Error(http.StatusNotFound)
|
|
|
|
}
|
|
|
|
}
|