2023-05-13 01:27:50 +08:00
|
|
|
// Copyright 2023 The Gitea Authors. All rights reserved.
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
|
|
|
package alpine
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/x509"
|
|
|
|
"encoding/hex"
|
|
|
|
"encoding/pem"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
packages_model "code.gitea.io/gitea/models/packages"
|
|
|
|
"code.gitea.io/gitea/modules/json"
|
|
|
|
packages_module "code.gitea.io/gitea/modules/packages"
|
|
|
|
alpine_module "code.gitea.io/gitea/modules/packages/alpine"
|
|
|
|
"code.gitea.io/gitea/modules/util"
|
|
|
|
"code.gitea.io/gitea/routers/api/packages/helper"
|
2024-02-27 15:12:22 +08:00
|
|
|
"code.gitea.io/gitea/services/context"
|
2023-05-13 01:27:50 +08:00
|
|
|
packages_service "code.gitea.io/gitea/services/packages"
|
|
|
|
alpine_service "code.gitea.io/gitea/services/packages/alpine"
|
|
|
|
)
|
|
|
|
|
2023-07-05 02:36:08 +08:00
|
|
|
func apiError(ctx *context.Context, status int, obj any) {
|
2023-05-13 01:27:50 +08:00
|
|
|
helper.LogAndProcessError(ctx, status, obj, func(message string) {
|
|
|
|
ctx.PlainText(status, message)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetRepositoryKey(ctx *context.Context) {
|
2023-09-15 14:13:19 +08:00
|
|
|
_, pub, err := alpine_service.GetOrCreateKeyPair(ctx, ctx.Package.Owner.ID)
|
2023-05-13 01:27:50 +08:00
|
|
|
if err != nil {
|
|
|
|
apiError(ctx, http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
pubPem, _ := pem.Decode([]byte(pub))
|
|
|
|
if pubPem == nil {
|
|
|
|
apiError(ctx, http.StatusInternalServerError, "failed to decode private key pem")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
pubKey, err := x509.ParsePKIXPublicKey(pubPem.Bytes)
|
|
|
|
if err != nil {
|
|
|
|
apiError(ctx, http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
fingerprint, err := util.CreatePublicKeyFingerprint(pubKey)
|
|
|
|
if err != nil {
|
|
|
|
apiError(ctx, http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.ServeContent(strings.NewReader(pub), &context.ServeHeaderOptions{
|
|
|
|
ContentType: "application/x-pem-file",
|
|
|
|
Filename: fmt.Sprintf("%s@%s.rsa.pub", ctx.Package.Owner.LowerName, hex.EncodeToString(fingerprint)),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetRepositoryFile(ctx *context.Context) {
|
2023-09-25 21:17:37 +08:00
|
|
|
pv, err := alpine_service.GetOrCreateRepositoryVersion(ctx, ctx.Package.Owner.ID)
|
2023-05-13 01:27:50 +08:00
|
|
|
if err != nil {
|
|
|
|
apiError(ctx, http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-07-03 21:33:28 +08:00
|
|
|
s, u, pf, err := packages_service.GetFileStreamByPackageVersion(
|
2023-05-13 01:27:50 +08:00
|
|
|
ctx,
|
|
|
|
pv,
|
|
|
|
&packages_service.PackageFileInfo{
|
2024-02-25 23:10:55 +08:00
|
|
|
Filename: alpine_service.IndexArchiveFilename,
|
2024-06-19 06:32:45 +08:00
|
|
|
CompositeKey: fmt.Sprintf("%s|%s|%s", ctx.PathParam("branch"), ctx.PathParam("repository"), ctx.PathParam("architecture")),
|
2023-05-13 01:27:50 +08: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 21:33:28 +08:00
|
|
|
helper.ServePackageFile(ctx, s, u, pf)
|
2023-05-13 01:27:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func UploadPackageFile(ctx *context.Context) {
|
2024-06-19 06:32:45 +08:00
|
|
|
branch := strings.TrimSpace(ctx.PathParam("branch"))
|
|
|
|
repository := strings.TrimSpace(ctx.PathParam("repository"))
|
2023-05-13 01:27:50 +08:00
|
|
|
if branch == "" || repository == "" {
|
|
|
|
apiError(ctx, http.StatusBadRequest, "invalid branch or repository")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-04-22 19:48:42 +08:00
|
|
|
upload, needToClose, err := ctx.UploadStream()
|
2023-05-13 01:27:50 +08:00
|
|
|
if err != nil {
|
|
|
|
apiError(ctx, http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
2024-04-22 19:48:42 +08:00
|
|
|
if needToClose {
|
2023-05-13 01:27:50 +08:00
|
|
|
defer upload.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
buf, err := packages_module.CreateHashedBufferFromReader(upload)
|
|
|
|
if err != nil {
|
|
|
|
apiError(ctx, http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer buf.Close()
|
|
|
|
|
|
|
|
pck, err := alpine_module.ParsePackage(buf)
|
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, util.ErrInvalidArgument) || err == io.EOF {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
_, _, err = packages_service.CreatePackageOrAddFileToExisting(
|
2023-09-25 21:17:37 +08:00
|
|
|
ctx,
|
2023-05-13 01:27:50 +08:00
|
|
|
&packages_service.PackageCreationInfo{
|
|
|
|
PackageInfo: packages_service.PackageInfo{
|
|
|
|
Owner: ctx.Package.Owner,
|
|
|
|
PackageType: packages_model.TypeAlpine,
|
|
|
|
Name: pck.Name,
|
|
|
|
Version: pck.Version,
|
|
|
|
},
|
|
|
|
Creator: ctx.Doer,
|
|
|
|
Metadata: pck.VersionMetadata,
|
|
|
|
},
|
|
|
|
&packages_service.PackageFileCreationInfo{
|
|
|
|
PackageFileInfo: packages_service.PackageFileInfo{
|
|
|
|
Filename: fmt.Sprintf("%s-%s.apk", pck.Name, pck.Version),
|
|
|
|
CompositeKey: fmt.Sprintf("%s|%s|%s", branch, repository, pck.FileMetadata.Architecture),
|
|
|
|
},
|
|
|
|
Creator: ctx.Doer,
|
|
|
|
Data: buf,
|
|
|
|
IsLead: true,
|
|
|
|
Properties: map[string]string{
|
|
|
|
alpine_module.PropertyBranch: branch,
|
|
|
|
alpine_module.PropertyRepository: repository,
|
|
|
|
alpine_module.PropertyArchitecture: pck.FileMetadata.Architecture,
|
|
|
|
alpine_module.PropertyMetadata: string(fileMetadataRaw),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
switch err {
|
|
|
|
case packages_model.ErrDuplicatePackageVersion, packages_model.ErrDuplicatePackageFile:
|
2023-10-10 21:39:58 +08:00
|
|
|
apiError(ctx, http.StatusConflict, err)
|
2023-05-13 01:27:50 +08:00
|
|
|
case packages_service.ErrQuotaTotalCount, packages_service.ErrQuotaTypeSize, packages_service.ErrQuotaTotalSize:
|
|
|
|
apiError(ctx, http.StatusForbidden, err)
|
|
|
|
default:
|
|
|
|
apiError(ctx, http.StatusInternalServerError, err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := alpine_service.BuildSpecificRepositoryFiles(ctx, ctx.Package.Owner.ID, branch, repository, pck.FileMetadata.Architecture); err != nil {
|
|
|
|
apiError(ctx, http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Status(http.StatusCreated)
|
|
|
|
}
|
|
|
|
|
|
|
|
func DownloadPackageFile(ctx *context.Context) {
|
2024-06-19 06:32:45 +08:00
|
|
|
branch := ctx.PathParam("branch")
|
|
|
|
repository := ctx.PathParam("repository")
|
|
|
|
architecture := ctx.PathParam("architecture")
|
2024-02-25 23:10:55 +08:00
|
|
|
|
|
|
|
opts := &packages_model.PackageFileSearchOptions{
|
2023-05-13 01:27:50 +08:00
|
|
|
OwnerID: ctx.Package.Owner.ID,
|
|
|
|
PackageType: packages_model.TypeAlpine,
|
2024-06-19 06:32:45 +08:00
|
|
|
Query: ctx.PathParam("filename"),
|
2024-02-25 23:10:55 +08:00
|
|
|
CompositeKey: fmt.Sprintf("%s|%s|%s", branch, repository, architecture),
|
|
|
|
}
|
|
|
|
pfs, _, err := packages_model.SearchFiles(ctx, opts)
|
2023-05-13 01:27:50 +08:00
|
|
|
if err != nil {
|
|
|
|
apiError(ctx, http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
2024-02-25 23:10:55 +08:00
|
|
|
if len(pfs) == 0 {
|
|
|
|
// Try again with architecture 'noarch'
|
|
|
|
if architecture == alpine_module.NoArch {
|
|
|
|
apiError(ctx, http.StatusNotFound, nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
opts.CompositeKey = fmt.Sprintf("%s|%s|%s", branch, repository, alpine_module.NoArch)
|
|
|
|
if pfs, _, err = packages_model.SearchFiles(ctx, opts); err != nil {
|
|
|
|
apiError(ctx, http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(pfs) == 0 {
|
|
|
|
apiError(ctx, http.StatusNotFound, nil)
|
|
|
|
return
|
|
|
|
}
|
2023-05-13 01:27:50 +08:00
|
|
|
}
|
|
|
|
|
2023-07-03 21:33:28 +08:00
|
|
|
s, u, pf, err := packages_service.GetPackageFileStream(ctx, pfs[0])
|
2023-05-13 01:27:50 +08: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 21:33:28 +08:00
|
|
|
helper.ServePackageFile(ctx, s, u, pf)
|
2023-05-13 01:27:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func DeletePackageFile(ctx *context.Context) {
|
2024-06-19 06:32:45 +08:00
|
|
|
branch, repository, architecture := ctx.PathParam("branch"), ctx.PathParam("repository"), ctx.PathParam("architecture")
|
2023-05-13 01:27:50 +08:00
|
|
|
|
|
|
|
pfs, _, err := packages_model.SearchFiles(ctx, &packages_model.PackageFileSearchOptions{
|
|
|
|
OwnerID: ctx.Package.Owner.ID,
|
|
|
|
PackageType: packages_model.TypeAlpine,
|
2024-06-19 06:32:45 +08:00
|
|
|
Query: ctx.PathParam("filename"),
|
2023-05-13 01:27:50 +08:00
|
|
|
CompositeKey: fmt.Sprintf("%s|%s|%s", branch, repository, architecture),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
apiError(ctx, http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if len(pfs) != 1 {
|
|
|
|
apiError(ctx, http.StatusNotFound, nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-09-25 21:17:37 +08:00
|
|
|
if err := packages_service.RemovePackageFileAndVersionIfUnreferenced(ctx, ctx.Doer, pfs[0]); err != nil {
|
2023-05-13 01:27:50 +08:00
|
|
|
if errors.Is(err, util.ErrNotExist) {
|
|
|
|
apiError(ctx, http.StatusNotFound, err)
|
|
|
|
} else {
|
|
|
|
apiError(ctx, http.StatusInternalServerError, err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := alpine_service.BuildSpecificRepositoryFiles(ctx, ctx.Package.Owner.ID, branch, repository, architecture); err != nil {
|
|
|
|
apiError(ctx, http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Status(http.StatusNoContent)
|
|
|
|
}
|