2023-06-20 20:08:48 +00:00
|
|
|
// Copyright 2023 The Gitea Authors. All rights reserved.
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
|
|
|
package arch
|
|
|
|
|
|
|
|
import (
|
2023-08-08 16:40:54 +00:00
|
|
|
"encoding/hex"
|
2023-06-20 20:08:48 +00:00
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
|
2023-08-03 17:43:13 +00:00
|
|
|
pkg_model "code.gitea.io/gitea/models/packages"
|
2023-06-20 20:08:48 +00:00
|
|
|
"code.gitea.io/gitea/modules/context"
|
2023-07-28 13:32:01 +00:00
|
|
|
pkg_module "code.gitea.io/gitea/modules/packages"
|
2023-06-20 20:08:48 +00:00
|
|
|
arch_module "code.gitea.io/gitea/modules/packages/arch"
|
|
|
|
"code.gitea.io/gitea/routers/api/packages/helper"
|
2023-08-03 17:43:13 +00:00
|
|
|
pkg_service "code.gitea.io/gitea/services/packages"
|
2023-06-21 20:30:07 +00:00
|
|
|
arch_service "code.gitea.io/gitea/services/packages/arch"
|
2023-06-20 20:08:48 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Push new package to arch package registry.
|
|
|
|
func Push(ctx *context.Context) {
|
2023-06-21 20:30:07 +00:00
|
|
|
var (
|
2023-07-27 10:21:11 +00:00
|
|
|
filename = ctx.Params("filename")
|
|
|
|
distro = ctx.Params("distro")
|
|
|
|
sign = ctx.Params("sign")
|
2023-06-21 20:30:07 +00:00
|
|
|
)
|
2023-06-20 20:08:48 +00:00
|
|
|
|
2023-07-28 13:32:01 +00:00
|
|
|
upload, close, err := ctx.UploadStream()
|
2023-06-20 20:08:48 +00:00
|
|
|
if err != nil {
|
|
|
|
apiError(ctx, http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
2023-07-28 13:32:01 +00:00
|
|
|
if close {
|
|
|
|
defer upload.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
buf, err := pkg_module.CreateHashedBufferFromReader(upload)
|
|
|
|
if err != nil {
|
|
|
|
apiError(ctx, http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer buf.Close()
|
2023-06-20 20:08:48 +00:00
|
|
|
|
2023-08-04 11:52:36 +00:00
|
|
|
desc, err := arch_module.EjectMetadata(&arch_module.EjectParams{
|
|
|
|
Filename: filename,
|
|
|
|
Distribution: distro,
|
|
|
|
Buffer: buf,
|
|
|
|
})
|
2023-07-04 17:02:32 +00:00
|
|
|
if err != nil {
|
|
|
|
apiError(ctx, http.StatusBadRequest, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-08-08 13:21:17 +00:00
|
|
|
_, err = buf.Seek(0, io.SeekStart)
|
|
|
|
if err != nil {
|
2023-07-28 13:32:01 +00:00
|
|
|
apiError(ctx, http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-08-08 16:40:54 +00:00
|
|
|
props := map[string]string{
|
2023-10-02 22:12:06 +00:00
|
|
|
"desc": desc.String(),
|
2023-08-08 16:40:54 +00:00
|
|
|
}
|
|
|
|
if sign != "" {
|
|
|
|
_, err := hex.DecodeString(sign)
|
|
|
|
if err != nil {
|
|
|
|
apiError(ctx, http.StatusBadRequest, err)
|
2023-10-02 22:12:06 +00:00
|
|
|
return
|
2023-08-08 16:40:54 +00:00
|
|
|
}
|
2023-10-02 22:12:06 +00:00
|
|
|
props["sign"] = sign
|
2023-07-28 13:32:01 +00:00
|
|
|
}
|
|
|
|
|
2023-09-28 20:13:40 +00:00
|
|
|
_, _, err = pkg_service.CreatePackageOrAddFileToExisting(
|
2023-09-28 20:25:22 +00:00
|
|
|
ctx, &pkg_service.PackageCreationInfo{
|
2023-08-08 13:21:17 +00:00
|
|
|
PackageInfo: pkg_service.PackageInfo{
|
|
|
|
Owner: ctx.Package.Owner,
|
|
|
|
PackageType: pkg_model.TypeArch,
|
|
|
|
Name: desc.Name,
|
|
|
|
Version: desc.Version,
|
|
|
|
},
|
2023-09-25 21:54:27 +00:00
|
|
|
Creator: ctx.Doer,
|
2023-08-08 16:40:54 +00:00
|
|
|
Metadata: &arch_module.Metadata{
|
|
|
|
URL: desc.URL,
|
|
|
|
Description: desc.Description,
|
|
|
|
Provides: desc.Provides,
|
|
|
|
License: desc.License,
|
|
|
|
Depends: desc.Depends,
|
|
|
|
OptDepends: desc.OptDepends,
|
|
|
|
MakeDepends: desc.MakeDepends,
|
|
|
|
CheckDepends: desc.CheckDepends,
|
|
|
|
Backup: desc.Backup,
|
|
|
|
},
|
2023-08-08 13:21:17 +00:00
|
|
|
},
|
|
|
|
&pkg_service.PackageFileCreationInfo{
|
|
|
|
PackageFileInfo: pkg_service.PackageFileInfo{
|
|
|
|
Filename: filename,
|
2023-10-02 17:33:37 +00:00
|
|
|
CompositeKey: distro,
|
2023-08-08 13:21:17 +00:00
|
|
|
},
|
|
|
|
OverwriteExisting: true,
|
|
|
|
IsLead: true,
|
|
|
|
Creator: ctx.ContextUser,
|
|
|
|
Data: buf,
|
2023-08-08 16:40:54 +00:00
|
|
|
Properties: props,
|
2023-08-08 13:21:17 +00:00
|
|
|
},
|
|
|
|
)
|
2023-07-28 13:32:01 +00:00
|
|
|
if err != nil {
|
|
|
|
apiError(ctx, http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-06-20 20:08:48 +00:00
|
|
|
ctx.Status(http.StatusOK)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get file from arch package registry.
|
|
|
|
func Get(ctx *context.Context) {
|
2023-06-21 20:30:07 +00:00
|
|
|
var (
|
|
|
|
file = ctx.Params("file")
|
2023-06-24 15:08:18 +00:00
|
|
|
owner = ctx.Params("username")
|
2023-06-21 20:30:07 +00:00
|
|
|
distro = ctx.Params("distro")
|
|
|
|
arch = ctx.Params("arch")
|
|
|
|
)
|
2023-06-20 20:08:48 +00:00
|
|
|
|
2023-08-08 13:21:17 +00:00
|
|
|
if strings.HasSuffix(file, ".pkg.tar.zst") {
|
2023-09-05 17:51:29 +00:00
|
|
|
pkg, err := arch_service.GetPackageFile(ctx, distro, file)
|
2023-06-20 20:08:48 +00:00
|
|
|
if err != nil {
|
2023-06-21 20:30:07 +00:00
|
|
|
apiError(ctx, http.StatusNotFound, err)
|
2023-06-20 20:08:48 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-07-17 17:30:36 +00:00
|
|
|
ctx.ServeContent(pkg, &context.ServeHeaderOptions{
|
2023-07-27 10:21:11 +00:00
|
|
|
Filename: file,
|
2023-07-02 20:03:34 +00:00
|
|
|
})
|
2023-06-20 20:08:48 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-08-08 13:21:17 +00:00
|
|
|
if strings.HasSuffix(file, ".pkg.tar.zst.sig") {
|
2023-09-05 17:51:29 +00:00
|
|
|
sig, err := arch_service.GetPackageSignature(ctx, distro, file)
|
2023-08-08 13:21:17 +00:00
|
|
|
if err != nil {
|
|
|
|
apiError(ctx, http.StatusNotFound, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-09-05 17:51:29 +00:00
|
|
|
ctx.ServeContent(sig, &context.ServeHeaderOptions{
|
2023-08-08 13:21:17 +00:00
|
|
|
Filename: file,
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-06-21 20:30:07 +00:00
|
|
|
if strings.HasSuffix(file, ".db.tar.gz") || strings.HasSuffix(file, ".db") {
|
2023-09-24 16:32:53 +00:00
|
|
|
db, err := arch_service.CreatePacmanDb(ctx, owner, arch, distro)
|
2023-06-20 21:15:56 +00:00
|
|
|
if err != nil {
|
|
|
|
apiError(ctx, http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
2023-06-24 10:37:33 +00:00
|
|
|
|
2023-09-25 21:42:48 +00:00
|
|
|
ctx.ServeContent(db, &context.ServeHeaderOptions{
|
2023-07-27 10:21:11 +00:00
|
|
|
Filename: file,
|
2023-07-02 20:03:34 +00:00
|
|
|
})
|
2023-06-24 10:37:33 +00:00
|
|
|
return
|
2023-06-20 20:08:48 +00:00
|
|
|
}
|
2023-06-24 10:37:33 +00:00
|
|
|
|
2023-07-27 09:35:35 +00:00
|
|
|
ctx.Status(http.StatusNotFound)
|
2023-06-20 20:08:48 +00:00
|
|
|
}
|
|
|
|
|
2023-08-18 15:09:08 +00:00
|
|
|
// Remove specific package version, related files with properties.
|
2023-06-25 10:15:29 +00:00
|
|
|
func Remove(ctx *context.Context) {
|
|
|
|
var (
|
2023-07-27 10:21:11 +00:00
|
|
|
pkg = ctx.Params("package")
|
|
|
|
ver = ctx.Params("version")
|
2023-06-25 10:15:29 +00:00
|
|
|
)
|
|
|
|
|
2023-08-04 11:52:36 +00:00
|
|
|
version, err := pkg_model.GetVersionByNameAndVersion(
|
|
|
|
ctx, ctx.Package.Owner.ID, pkg_model.TypeArch, pkg, ver,
|
|
|
|
)
|
2023-08-03 17:43:13 +00:00
|
|
|
if err != nil {
|
|
|
|
apiError(ctx, http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-09-28 20:25:22 +00:00
|
|
|
err = pkg_service.RemovePackageVersion(ctx, ctx.Package.Owner, version)
|
2023-06-25 10:15:29 +00:00
|
|
|
if err != nil {
|
|
|
|
apiError(ctx, http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-07-27 09:35:17 +00:00
|
|
|
ctx.Status(http.StatusOK)
|
2023-06-25 10:15:29 +00:00
|
|
|
}
|
|
|
|
|
2023-06-20 20:08:48 +00:00
|
|
|
func apiError(ctx *context.Context, status int, obj interface{}) {
|
|
|
|
helper.LogAndProcessError(ctx, status, obj, func(message string) {
|
|
|
|
ctx.PlainText(status, message)
|
|
|
|
})
|
|
|
|
}
|