2023-05-05 22:33:37 +02:00
|
|
|
// Copyright 2023 The Gitea Authors. All rights reserved.
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
|
|
|
package rpm
|
|
|
|
|
|
|
|
import (
|
|
|
|
stdctx "context"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"code.gitea.io/gitea/models/db"
|
|
|
|
packages_model "code.gitea.io/gitea/models/packages"
|
|
|
|
"code.gitea.io/gitea/modules/json"
|
|
|
|
packages_module "code.gitea.io/gitea/modules/packages"
|
|
|
|
rpm_module "code.gitea.io/gitea/modules/packages/rpm"
|
|
|
|
"code.gitea.io/gitea/modules/setting"
|
|
|
|
"code.gitea.io/gitea/modules/util"
|
|
|
|
"code.gitea.io/gitea/routers/api/packages/helper"
|
2024-02-27 08:12:22 +01:00
|
|
|
"code.gitea.io/gitea/services/context"
|
2023-09-05 20:37:47 +02:00
|
|
|
notify_service "code.gitea.io/gitea/services/notify"
|
2023-05-05 22:33:37 +02:00
|
|
|
packages_service "code.gitea.io/gitea/services/packages"
|
|
|
|
rpm_service "code.gitea.io/gitea/services/packages/rpm"
|
|
|
|
)
|
|
|
|
|
2023-07-04 20:36:08 +02:00
|
|
|
func apiError(ctx *context.Context, status int, obj any) {
|
2023-05-05 22:33:37 +02:00
|
|
|
helper.LogAndProcessError(ctx, status, obj, func(message string) {
|
|
|
|
ctx.PlainText(status, message)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// https://dnf.readthedocs.io/en/latest/conf_ref.html
|
|
|
|
func GetRepositoryConfig(ctx *context.Context) {
|
2024-01-12 04:16:05 +01:00
|
|
|
group := ctx.Params("group")
|
2024-01-19 12:37:10 +01:00
|
|
|
|
|
|
|
var groupParts []string
|
2024-01-12 04:16:05 +01:00
|
|
|
if group != "" {
|
2024-01-19 12:37:10 +01:00
|
|
|
groupParts = strings.Split(group, "/")
|
2024-01-12 04:16:05 +01:00
|
|
|
}
|
2024-01-19 12:37:10 +01:00
|
|
|
|
2023-05-05 22:33:37 +02:00
|
|
|
url := fmt.Sprintf("%sapi/packages/%s/rpm", setting.AppURL, ctx.Package.Owner.Name)
|
2024-01-19 12:37:10 +01:00
|
|
|
|
|
|
|
ctx.PlainText(http.StatusOK, `[gitea-`+strings.Join(append([]string{ctx.Package.Owner.LowerName}, groupParts...), "-")+`]
|
|
|
|
name=`+strings.Join(append([]string{ctx.Package.Owner.Name, setting.AppName}, groupParts...), " - ")+`
|
|
|
|
baseurl=`+strings.Join(append([]string{url}, groupParts...), "/")+`
|
2023-05-05 22:33:37 +02:00
|
|
|
enabled=1
|
|
|
|
gpgcheck=1
|
|
|
|
gpgkey=`+url+`/repository.key`)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Gets or creates the PGP public key used to sign repository metadata files
|
|
|
|
func GetRepositoryKey(ctx *context.Context) {
|
2023-09-15 08:13:19 +02:00
|
|
|
_, pub, err := rpm_service.GetOrCreateKeyPair(ctx, ctx.Package.Owner.ID)
|
2023-05-05 22:33:37 +02:00
|
|
|
if err != nil {
|
|
|
|
apiError(ctx, http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.ServeContent(strings.NewReader(pub), &context.ServeHeaderOptions{
|
|
|
|
ContentType: "application/pgp-keys",
|
|
|
|
Filename: "repository.key",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-12-05 09:01:02 +01:00
|
|
|
func CheckRepositoryFileExistence(ctx *context.Context) {
|
|
|
|
pv, err := rpm_service.GetOrCreateRepositoryVersion(ctx, ctx.Package.Owner.ID)
|
|
|
|
if err != nil {
|
|
|
|
apiError(ctx, http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-01-12 04:16:05 +01:00
|
|
|
pf, err := packages_model.GetFileForVersionByName(ctx, pv.ID, ctx.Params("filename"), ctx.Params("group"))
|
2023-12-05 09:01:02 +01:00
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, util.ErrNotExist) {
|
|
|
|
ctx.Status(http.StatusNotFound)
|
|
|
|
} else {
|
|
|
|
apiError(ctx, http.StatusInternalServerError, err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.SetServeHeaders(&context.ServeHeaderOptions{
|
|
|
|
Filename: pf.Name,
|
|
|
|
LastModified: pf.CreatedUnix.AsLocalTime(),
|
|
|
|
})
|
|
|
|
ctx.Status(http.StatusOK)
|
|
|
|
}
|
|
|
|
|
2023-05-05 22:33:37 +02:00
|
|
|
// Gets a pre-generated repository metadata file
|
|
|
|
func GetRepositoryFile(ctx *context.Context) {
|
2023-09-25 15:17:37 +02:00
|
|
|
pv, err := rpm_service.GetOrCreateRepositoryVersion(ctx, ctx.Package.Owner.ID)
|
2023-05-05 22:33:37 +02:00
|
|
|
if err != nil {
|
|
|
|
apiError(ctx, http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-07-03 15:33:28 +02:00
|
|
|
s, u, pf, err := packages_service.GetFileStreamByPackageVersion(
|
2023-05-05 22:33:37 +02:00
|
|
|
ctx,
|
|
|
|
pv,
|
|
|
|
&packages_service.PackageFileInfo{
|
2024-01-12 04:16:05 +01:00
|
|
|
Filename: ctx.Params("filename"),
|
|
|
|
CompositeKey: ctx.Params("group"),
|
2023-05-05 22:33:37 +02:00
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, util.ErrNotExist) {
|
|
|
|
apiError(ctx, http.StatusNotFound, err)
|
|
|
|
} else {
|
|
|
|
apiError(ctx, http.StatusInternalServerError, err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-07-03 15:33:28 +02:00
|
|
|
helper.ServePackageFile(ctx, s, u, pf)
|
2023-05-05 22:33:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func UploadPackageFile(ctx *context.Context) {
|
2024-04-22 13:48:42 +02:00
|
|
|
upload, needToClose, err := ctx.UploadStream()
|
2023-05-05 22:33:37 +02:00
|
|
|
if err != nil {
|
|
|
|
apiError(ctx, http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
2024-04-22 13:48:42 +02:00
|
|
|
if needToClose {
|
2023-05-05 22:33:37 +02:00
|
|
|
defer upload.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
buf, err := packages_module.CreateHashedBufferFromReader(upload)
|
|
|
|
if err != nil {
|
|
|
|
apiError(ctx, http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer buf.Close()
|
|
|
|
|
|
|
|
pck, err := rpm_module.ParsePackage(buf)
|
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, util.ErrInvalidArgument) {
|
|
|
|
apiError(ctx, http.StatusBadRequest, err)
|
|
|
|
} else {
|
|
|
|
apiError(ctx, http.StatusInternalServerError, err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := buf.Seek(0, io.SeekStart); err != nil {
|
|
|
|
apiError(ctx, http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
fileMetadataRaw, err := json.Marshal(pck.FileMetadata)
|
|
|
|
if err != nil {
|
|
|
|
apiError(ctx, http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
2024-01-12 04:16:05 +01:00
|
|
|
group := ctx.Params("group")
|
2023-05-05 22:33:37 +02:00
|
|
|
_, _, err = packages_service.CreatePackageOrAddFileToExisting(
|
2023-09-25 15:17:37 +02:00
|
|
|
ctx,
|
2023-05-05 22:33:37 +02:00
|
|
|
&packages_service.PackageCreationInfo{
|
|
|
|
PackageInfo: packages_service.PackageInfo{
|
|
|
|
Owner: ctx.Package.Owner,
|
|
|
|
PackageType: packages_model.TypeRpm,
|
|
|
|
Name: pck.Name,
|
2024-01-19 12:37:10 +01:00
|
|
|
Version: pck.Version,
|
2023-05-05 22:33:37 +02:00
|
|
|
},
|
|
|
|
Creator: ctx.Doer,
|
|
|
|
Metadata: pck.VersionMetadata,
|
|
|
|
},
|
|
|
|
&packages_service.PackageFileCreationInfo{
|
|
|
|
PackageFileInfo: packages_service.PackageFileInfo{
|
2024-01-12 04:16:05 +01:00
|
|
|
Filename: fmt.Sprintf("%s-%s.%s.rpm", pck.Name, pck.Version, pck.FileMetadata.Architecture),
|
|
|
|
CompositeKey: group,
|
2023-05-05 22:33:37 +02:00
|
|
|
},
|
|
|
|
Creator: ctx.Doer,
|
|
|
|
Data: buf,
|
|
|
|
IsLead: true,
|
|
|
|
Properties: map[string]string{
|
2024-01-19 12:37:10 +01:00
|
|
|
rpm_module.PropertyGroup: group,
|
|
|
|
rpm_module.PropertyArchitecture: pck.FileMetadata.Architecture,
|
|
|
|
rpm_module.PropertyMetadata: string(fileMetadataRaw),
|
2023-05-05 22:33:37 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
switch err {
|
|
|
|
case packages_model.ErrDuplicatePackageVersion, packages_model.ErrDuplicatePackageFile:
|
|
|
|
apiError(ctx, http.StatusConflict, err)
|
|
|
|
case packages_service.ErrQuotaTotalCount, packages_service.ErrQuotaTypeSize, packages_service.ErrQuotaTotalSize:
|
|
|
|
apiError(ctx, http.StatusForbidden, err)
|
|
|
|
default:
|
|
|
|
apiError(ctx, http.StatusInternalServerError, err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-01-19 12:37:10 +01:00
|
|
|
if err := rpm_service.BuildSpecificRepositoryFiles(ctx, ctx.Package.Owner.ID, group); err != nil {
|
2023-05-05 22:33:37 +02:00
|
|
|
apiError(ctx, http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Status(http.StatusCreated)
|
|
|
|
}
|
|
|
|
|
|
|
|
func DownloadPackageFile(ctx *context.Context) {
|
|
|
|
name := ctx.Params("name")
|
|
|
|
version := ctx.Params("version")
|
2024-01-19 12:37:10 +01:00
|
|
|
|
2023-07-03 15:33:28 +02:00
|
|
|
s, u, pf, err := packages_service.GetFileStreamByPackageNameAndVersion(
|
2023-05-05 22:33:37 +02:00
|
|
|
ctx,
|
|
|
|
&packages_service.PackageInfo{
|
|
|
|
Owner: ctx.Package.Owner,
|
|
|
|
PackageType: packages_model.TypeRpm,
|
|
|
|
Name: name,
|
2024-01-19 12:37:10 +01:00
|
|
|
Version: version,
|
2023-05-05 22:33:37 +02:00
|
|
|
},
|
|
|
|
&packages_service.PackageFileInfo{
|
2024-01-12 04:16:05 +01:00
|
|
|
Filename: fmt.Sprintf("%s-%s.%s.rpm", name, version, ctx.Params("architecture")),
|
2024-01-19 12:37:10 +01:00
|
|
|
CompositeKey: ctx.Params("group"),
|
2023-05-05 22:33:37 +02:00
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, util.ErrNotExist) {
|
|
|
|
apiError(ctx, http.StatusNotFound, err)
|
|
|
|
} else {
|
|
|
|
apiError(ctx, http.StatusInternalServerError, err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-07-03 15:33:28 +02:00
|
|
|
helper.ServePackageFile(ctx, s, u, pf)
|
2023-05-05 22:33:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func DeletePackageFile(webctx *context.Context) {
|
2024-01-12 04:16:05 +01:00
|
|
|
group := webctx.Params("group")
|
2023-05-05 22:33:37 +02:00
|
|
|
name := webctx.Params("name")
|
|
|
|
version := webctx.Params("version")
|
|
|
|
architecture := webctx.Params("architecture")
|
2024-01-19 12:37:10 +01:00
|
|
|
|
2023-05-05 22:33:37 +02:00
|
|
|
var pd *packages_model.PackageDescriptor
|
|
|
|
|
|
|
|
err := db.WithTx(webctx, func(ctx stdctx.Context) error {
|
2024-01-12 04:16:05 +01:00
|
|
|
pv, err := packages_model.GetVersionByNameAndVersion(ctx,
|
|
|
|
webctx.Package.Owner.ID,
|
|
|
|
packages_model.TypeRpm,
|
|
|
|
name,
|
2024-01-19 12:37:10 +01:00
|
|
|
version,
|
2024-01-12 04:16:05 +01:00
|
|
|
)
|
2023-05-05 22:33:37 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
pf, err := packages_model.GetFileForVersionByName(
|
|
|
|
ctx,
|
|
|
|
pv.ID,
|
|
|
|
fmt.Sprintf("%s-%s.%s.rpm", name, version, architecture),
|
2024-01-12 04:16:05 +01:00
|
|
|
group,
|
2023-05-05 22:33:37 +02:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := packages_service.DeletePackageFile(ctx, pf); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
has, err := packages_model.HasVersionFileReferences(ctx, pv.ID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !has {
|
|
|
|
pd, err = packages_model.GetPackageDescriptor(ctx, pv)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := packages_service.DeletePackageVersionAndReferences(ctx, pv); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, util.ErrNotExist) {
|
|
|
|
apiError(webctx, http.StatusNotFound, err)
|
|
|
|
} else {
|
|
|
|
apiError(webctx, http.StatusInternalServerError, err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if pd != nil {
|
2023-09-05 20:37:47 +02:00
|
|
|
notify_service.PackageDelete(webctx, webctx.Doer, pd)
|
2023-05-05 22:33:37 +02:00
|
|
|
}
|
|
|
|
|
2024-01-19 12:37:10 +01:00
|
|
|
if err := rpm_service.BuildSpecificRepositoryFiles(webctx, webctx.Package.Owner.ID, group); err != nil {
|
2023-05-05 22:33:37 +02:00
|
|
|
apiError(webctx, http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
webctx.Status(http.StatusNoContent)
|
|
|
|
}
|