2022-03-30 14:12:47 +05:30
|
|
|
// Copyright 2021 The Gitea Authors. All rights reserved.
|
2022-11-27 23:50:29 +05:30
|
|
|
// SPDX-License-Identifier: MIT
|
2022-03-30 14:12:47 +05:30
|
|
|
|
|
|
|
package composer
|
|
|
|
|
|
|
|
import (
|
2022-12-31 17:19:37 +05:30
|
|
|
"errors"
|
2022-03-30 14:12:47 +05:30
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"code.gitea.io/gitea/models/db"
|
|
|
|
packages_model "code.gitea.io/gitea/models/packages"
|
2024-03-02 21:12:31 +05:30
|
|
|
"code.gitea.io/gitea/modules/optional"
|
2022-03-30 14:12:47 +05:30
|
|
|
packages_module "code.gitea.io/gitea/modules/packages"
|
|
|
|
composer_module "code.gitea.io/gitea/modules/packages/composer"
|
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2022-07-27 07:29:10 +05:30
|
|
|
"code.gitea.io/gitea/modules/util"
|
2022-03-30 14:12:47 +05:30
|
|
|
"code.gitea.io/gitea/routers/api/packages/helper"
|
2024-02-27 12:42:22 +05:30
|
|
|
"code.gitea.io/gitea/services/context"
|
2022-12-29 08:27:15 +05:30
|
|
|
"code.gitea.io/gitea/services/convert"
|
2022-03-30 14:12:47 +05:30
|
|
|
packages_service "code.gitea.io/gitea/services/packages"
|
|
|
|
|
|
|
|
"github.com/hashicorp/go-version"
|
|
|
|
)
|
|
|
|
|
2023-07-05 00:06:08 +05:30
|
|
|
func apiError(ctx *context.Context, status int, obj any) {
|
2022-03-30 14:12:47 +05:30
|
|
|
helper.LogAndProcessError(ctx, status, obj, func(message string) {
|
|
|
|
type Error struct {
|
|
|
|
Status int `json:"status"`
|
|
|
|
Message string `json:"message"`
|
|
|
|
}
|
|
|
|
ctx.JSON(status, struct {
|
|
|
|
Errors []Error `json:"errors"`
|
|
|
|
}{
|
|
|
|
Errors: []Error{
|
|
|
|
{Status: status, Message: message},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// ServiceIndex displays registry endpoints
|
|
|
|
func ServiceIndex(ctx *context.Context) {
|
|
|
|
resp := createServiceIndexResponse(setting.AppURL + "api/packages/" + ctx.Package.Owner.Name + "/composer")
|
|
|
|
|
|
|
|
ctx.JSON(http.StatusOK, resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SearchPackages searches packages, only "q" is supported
|
|
|
|
// https://packagist.org/apidoc#search-packages
|
|
|
|
func SearchPackages(ctx *context.Context) {
|
|
|
|
page := ctx.FormInt("page")
|
|
|
|
if page < 1 {
|
|
|
|
page = 1
|
|
|
|
}
|
|
|
|
perPage := ctx.FormInt("per_page")
|
|
|
|
paginator := db.ListOptions{
|
|
|
|
Page: page,
|
|
|
|
PageSize: convert.ToCorrectPageSize(perPage),
|
|
|
|
}
|
|
|
|
|
|
|
|
opts := &packages_model.PackageSearchOptions{
|
2022-07-27 07:29:10 +05:30
|
|
|
OwnerID: ctx.Package.Owner.ID,
|
|
|
|
Type: packages_model.TypeComposer,
|
|
|
|
Name: packages_model.SearchValue{Value: ctx.FormTrim("q")},
|
2024-03-02 21:12:31 +05:30
|
|
|
IsInternal: optional.Some(false),
|
2022-07-27 07:29:10 +05:30
|
|
|
Paginator: &paginator,
|
2022-03-30 14:12:47 +05:30
|
|
|
}
|
|
|
|
if ctx.FormTrim("type") != "" {
|
|
|
|
opts.Properties = map[string]string{
|
|
|
|
composer_module.TypeProperty: ctx.FormTrim("type"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pvs, total, err := packages_model.SearchLatestVersions(ctx, opts)
|
|
|
|
if err != nil {
|
|
|
|
apiError(ctx, http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
nextLink := ""
|
|
|
|
if len(pvs) == paginator.PageSize {
|
|
|
|
u, err := url.Parse(setting.AppURL + "api/packages/" + ctx.Package.Owner.Name + "/composer/search.json")
|
|
|
|
if err != nil {
|
|
|
|
apiError(ctx, http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
q := u.Query()
|
|
|
|
q.Set("q", ctx.FormTrim("q"))
|
|
|
|
q.Set("type", ctx.FormTrim("type"))
|
|
|
|
q.Set("page", strconv.Itoa(page+1))
|
|
|
|
if perPage != 0 {
|
|
|
|
q.Set("per_page", strconv.Itoa(perPage))
|
|
|
|
}
|
|
|
|
u.RawQuery = q.Encode()
|
|
|
|
|
|
|
|
nextLink = u.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
pds, err := packages_model.GetPackageDescriptors(ctx, pvs)
|
|
|
|
if err != nil {
|
|
|
|
apiError(ctx, http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
resp := createSearchResultResponse(total, pds, nextLink)
|
|
|
|
|
|
|
|
ctx.JSON(http.StatusOK, resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
// EnumeratePackages lists all package names
|
|
|
|
// https://packagist.org/apidoc#list-packages
|
|
|
|
func EnumeratePackages(ctx *context.Context) {
|
2023-07-24 09:17:27 +05:30
|
|
|
ps, err := packages_model.GetPackagesByType(ctx, ctx.Package.Owner.ID, packages_model.TypeComposer)
|
2022-03-30 14:12:47 +05:30
|
|
|
if err != nil {
|
|
|
|
apiError(ctx, http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
names := make([]string, 0, len(ps))
|
|
|
|
for _, p := range ps {
|
|
|
|
names = append(names, p.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.JSON(http.StatusOK, map[string][]string{
|
|
|
|
"packageNames": names,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// PackageMetadata returns the metadata for a single package
|
|
|
|
// https://packagist.org/apidoc#get-package-data
|
|
|
|
func PackageMetadata(ctx *context.Context) {
|
|
|
|
vendorName := ctx.Params("vendorname")
|
|
|
|
projectName := ctx.Params("projectname")
|
|
|
|
|
|
|
|
pvs, err := packages_model.GetVersionsByPackageName(ctx, ctx.Package.Owner.ID, packages_model.TypeComposer, vendorName+"/"+projectName)
|
|
|
|
if err != nil {
|
|
|
|
apiError(ctx, http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if len(pvs) == 0 {
|
|
|
|
apiError(ctx, http.StatusNotFound, packages_model.ErrPackageNotExist)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
pds, err := packages_model.GetPackageDescriptors(ctx, pvs)
|
|
|
|
if err != nil {
|
|
|
|
apiError(ctx, http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
resp := createPackageMetadataResponse(
|
|
|
|
setting.AppURL+"api/packages/"+ctx.Package.Owner.Name+"/composer",
|
|
|
|
pds,
|
|
|
|
)
|
|
|
|
|
|
|
|
ctx.JSON(http.StatusOK, resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
// DownloadPackageFile serves the content of a package
|
|
|
|
func DownloadPackageFile(ctx *context.Context) {
|
2023-07-03 19:03:28 +05:30
|
|
|
s, u, pf, err := packages_service.GetFileStreamByPackageNameAndVersion(
|
2022-03-30 14:12:47 +05:30
|
|
|
ctx,
|
|
|
|
&packages_service.PackageInfo{
|
|
|
|
Owner: ctx.Package.Owner,
|
|
|
|
PackageType: packages_model.TypeComposer,
|
|
|
|
Name: ctx.Params("package"),
|
|
|
|
Version: ctx.Params("version"),
|
|
|
|
},
|
|
|
|
&packages_service.PackageFileInfo{
|
|
|
|
Filename: ctx.Params("filename"),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
if err == packages_model.ErrPackageNotExist || err == packages_model.ErrPackageFileNotExist {
|
|
|
|
apiError(ctx, http.StatusNotFound, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
apiError(ctx, http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-07-03 19:03:28 +05:30
|
|
|
helper.ServePackageFile(ctx, s, u, pf)
|
2022-03-30 14:12:47 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// UploadPackage creates a new package
|
|
|
|
func UploadPackage(ctx *context.Context) {
|
2023-05-02 22:01:35 +05:30
|
|
|
buf, err := packages_module.CreateHashedBufferFromReader(ctx.Req.Body)
|
2022-03-30 14:12:47 +05:30
|
|
|
if err != nil {
|
|
|
|
apiError(ctx, http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer buf.Close()
|
|
|
|
|
|
|
|
cp, err := composer_module.ParsePackage(buf, buf.Size())
|
|
|
|
if err != nil {
|
2022-12-31 17:19:37 +05:30
|
|
|
if errors.Is(err, util.ErrInvalidArgument) {
|
|
|
|
apiError(ctx, http.StatusBadRequest, err)
|
|
|
|
} else {
|
|
|
|
apiError(ctx, http.StatusInternalServerError, err)
|
|
|
|
}
|
2022-03-30 14:12:47 +05:30
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := buf.Seek(0, io.SeekStart); err != nil {
|
|
|
|
apiError(ctx, http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if cp.Version == "" {
|
|
|
|
v, err := version.NewVersion(ctx.FormTrim("version"))
|
|
|
|
if err != nil {
|
|
|
|
apiError(ctx, http.StatusBadRequest, composer_module.ErrInvalidVersion)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
cp.Version = v.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
_, _, err = packages_service.CreatePackageAndAddFile(
|
2023-09-25 18:47:37 +05:30
|
|
|
ctx,
|
2022-03-30 14:12:47 +05:30
|
|
|
&packages_service.PackageCreationInfo{
|
|
|
|
PackageInfo: packages_service.PackageInfo{
|
|
|
|
Owner: ctx.Package.Owner,
|
|
|
|
PackageType: packages_model.TypeComposer,
|
|
|
|
Name: cp.Name,
|
|
|
|
Version: cp.Version,
|
|
|
|
},
|
|
|
|
SemverCompatible: true,
|
|
|
|
Creator: ctx.Doer,
|
|
|
|
Metadata: cp.Metadata,
|
2022-07-28 09:29:39 +05:30
|
|
|
VersionProperties: map[string]string{
|
2022-03-30 14:12:47 +05:30
|
|
|
composer_module.TypeProperty: cp.Type,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
&packages_service.PackageFileCreationInfo{
|
|
|
|
PackageFileInfo: packages_service.PackageFileInfo{
|
|
|
|
Filename: strings.ToLower(fmt.Sprintf("%s.%s.zip", strings.ReplaceAll(cp.Name, "/", "-"), cp.Version)),
|
|
|
|
},
|
2022-11-09 12:04:27 +05:30
|
|
|
Creator: ctx.Doer,
|
|
|
|
Data: buf,
|
|
|
|
IsLead: true,
|
2022-03-30 14:12:47 +05:30
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
2022-11-09 12:04:27 +05:30
|
|
|
switch err {
|
|
|
|
case packages_model.ErrDuplicatePackageVersion:
|
2023-10-10 19:09:58 +05:30
|
|
|
apiError(ctx, http.StatusConflict, err)
|
2022-11-09 12:04:27 +05:30
|
|
|
case packages_service.ErrQuotaTotalCount, packages_service.ErrQuotaTypeSize, packages_service.ErrQuotaTotalSize:
|
|
|
|
apiError(ctx, http.StatusForbidden, err)
|
|
|
|
default:
|
|
|
|
apiError(ctx, http.StatusInternalServerError, err)
|
2022-03-30 14:12:47 +05:30
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Status(http.StatusCreated)
|
|
|
|
}
|