mirror of
https://github.com/go-gitea/gitea.git
synced 2024-09-01 14:56:30 +00:00
reduced amount of logic, removed unnecessary fields
This commit is contained in:
parent
e86bdfcb0e
commit
0d08eaa556
@ -25,6 +25,8 @@ var (
|
||||
ErrDuplicatePackageFile = util.NewAlreadyExistErrorf("package file already exists")
|
||||
// ErrPackageFileNotExist indicates a package file not exist error
|
||||
ErrPackageFileNotExist = util.NewNotExistErrorf("package file does not exist")
|
||||
// ErrPackagePropertyNotExist indicates a package property not exist error
|
||||
ErrPackagePropertyNotExist = util.NewNotExistErrorf("package property does not exist")
|
||||
)
|
||||
|
||||
// EmptyFileKey is a named constant for an empty file key
|
||||
|
@ -60,6 +60,19 @@ func GetPropertiesByName(ctx context.Context, refType PropertyType, refID int64,
|
||||
return pps, db.GetEngine(ctx).Where("ref_type = ? AND ref_id = ? AND name = ?", refType, refID, name).Find(&pps)
|
||||
}
|
||||
|
||||
// GetPropertieWithUniqueName gets propertie with unique name
|
||||
func GetPropertieWithUniqueName(ctx context.Context, name string) (*PackageProperty, error) {
|
||||
p := &PackageProperty{Name: name}
|
||||
has, err := db.GetEngine(ctx).Get(p)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if !has {
|
||||
return nil, ErrPackagePropertyNotExist
|
||||
}
|
||||
return p, nil
|
||||
}
|
||||
|
||||
// UpdateProperty updates a property
|
||||
func UpdateProperty(ctx context.Context, pp *PackageProperty) error {
|
||||
_, err := db.GetEngine(ctx).ID(pp.ID).Update(pp)
|
||||
|
@ -32,7 +32,6 @@ type Metadata struct {
|
||||
MakeDepends []string `json:"make-depends,omitempty"`
|
||||
CheckDepends []string `json:"check-depends,omitempty"`
|
||||
Backup []string `json:"backup,omitempty"`
|
||||
DistroArch []string `json:"distro-arch,omitempty"`
|
||||
}
|
||||
|
||||
// Package description file that will be saved as .desc file in object storage.
|
||||
|
@ -5,6 +5,7 @@ package arch
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/hex"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
@ -59,17 +60,15 @@ func Push(ctx *context.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
md := &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,
|
||||
DistroArch: []string{distro + "-" + desc.Arch[0]},
|
||||
props := map[string]string{
|
||||
distro + "-" + filename + ".desc": desc.String(),
|
||||
}
|
||||
if sign != "" {
|
||||
_, err := hex.DecodeString(sign)
|
||||
if err != nil {
|
||||
apiError(ctx, http.StatusBadRequest, err)
|
||||
}
|
||||
props[distro+"-"+filename+".sig"] = sign
|
||||
}
|
||||
|
||||
ver, _, err := pkg_service.CreatePackageOrAddFileToExisting(
|
||||
@ -81,7 +80,17 @@ func Push(ctx *context.Context) {
|
||||
Version: desc.Version,
|
||||
},
|
||||
Creator: ctx.ContextUser,
|
||||
Metadata: md,
|
||||
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,
|
||||
},
|
||||
},
|
||||
&pkg_service.PackageFileCreationInfo{
|
||||
PackageFileInfo: pkg_service.PackageFileInfo{
|
||||
@ -92,6 +101,7 @@ func Push(ctx *context.Context) {
|
||||
IsLead: true,
|
||||
Creator: ctx.ContextUser,
|
||||
Data: buf,
|
||||
Properties: props,
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
@ -99,28 +109,6 @@ func Push(ctx *context.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
_, err = pkg_model.InsertProperty(ctx, 0, ver.ID, distro+"-"+filename+".desc", desc.String())
|
||||
if err != nil {
|
||||
apiError(ctx, http.StatusInternalServerError, err)
|
||||
return
|
||||
}
|
||||
|
||||
_, err = pkg_model.InsertProperty(ctx, 0, ver.ID, distro+"-"+filename+".sig", sign)
|
||||
if err != nil {
|
||||
apiError(ctx, http.StatusInternalServerError, err)
|
||||
return
|
||||
}
|
||||
|
||||
err = arch_service.UpdateMetadata(ctx, &arch_service.UpdateMetadataParams{
|
||||
User: ctx.Package.Owner,
|
||||
Metadata: md,
|
||||
DbDesc: desc,
|
||||
})
|
||||
if err != nil {
|
||||
apiError(ctx, http.StatusInternalServerError, err)
|
||||
return
|
||||
}
|
||||
|
||||
err = arch_service.RepoConnect(ctx, owner, desc.Name, ver.ID)
|
||||
if err != nil {
|
||||
apiError(ctx, http.StatusInternalServerError, err)
|
||||
@ -153,15 +141,21 @@ func Get(ctx *context.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
// Signatures are loaded from package properties in SQL db.
|
||||
// Signatures are loaded from package file properties in SQL db.
|
||||
if strings.HasSuffix(file, ".pkg.tar.zst.sig") {
|
||||
sign, err := arch_service.GetProperty(ctx, owner, distro+"-"+file)
|
||||
p, err := pkg_model.GetPropertieWithUniqueName(ctx, distro+"-"+file)
|
||||
if err != nil {
|
||||
apiError(ctx, http.StatusNotFound, err)
|
||||
return
|
||||
}
|
||||
|
||||
ctx.ServeContent(bytes.NewReader(sign), &context.ServeHeaderOptions{
|
||||
b, err := hex.DecodeString(p.Value)
|
||||
if err != nil {
|
||||
apiError(ctx, http.StatusInternalServerError, err)
|
||||
return
|
||||
}
|
||||
|
||||
ctx.ServeContent(bytes.NewReader(b), &context.ServeHeaderOptions{
|
||||
Filename: file,
|
||||
})
|
||||
return
|
||||
|
@ -4,8 +4,6 @@
|
||||
package arch
|
||||
|
||||
import (
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"fmt"
|
||||
"sort"
|
||||
"strings"
|
||||
@ -14,57 +12,12 @@ import (
|
||||
pkg_model "code.gitea.io/gitea/models/packages"
|
||||
repository "code.gitea.io/gitea/models/repo"
|
||||
"code.gitea.io/gitea/models/user"
|
||||
"code.gitea.io/gitea/modules/container"
|
||||
"code.gitea.io/gitea/modules/context"
|
||||
"code.gitea.io/gitea/modules/json"
|
||||
"code.gitea.io/gitea/modules/packages"
|
||||
"code.gitea.io/gitea/modules/packages/arch"
|
||||
"code.gitea.io/gitea/modules/storage"
|
||||
)
|
||||
|
||||
type UpdateMetadataParams struct {
|
||||
User *user.User
|
||||
Metadata *arch.Metadata
|
||||
DbDesc *arch.DbDesc
|
||||
}
|
||||
|
||||
// Update package metadata stored in SQL database with new combination of
|
||||
// distribution and architecture.
|
||||
func UpdateMetadata(ctx *context.Context, p *UpdateMetadataParams) error {
|
||||
ver, err := pkg_model.GetVersionByNameAndVersion(
|
||||
ctx, p.User.ID, pkg_model.TypeArch,
|
||||
p.DbDesc.Name, p.DbDesc.Version,
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var currmd arch.Metadata
|
||||
err = json.Unmarshal([]byte(ver.MetadataJSON), &currmd)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
currmd.DistroArch = uniqueSlice(currmd.DistroArch, p.Metadata.DistroArch)
|
||||
|
||||
b, err := json.Marshal(&currmd)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
ver.MetadataJSON = string(b)
|
||||
|
||||
return pkg_model.UpdateVersion(ctx, ver)
|
||||
}
|
||||
|
||||
// Creates a list containing unique values formed of 2 passed slices.
|
||||
func uniqueSlice(first, second []string) []string {
|
||||
set := make(container.Set[string], len(first)+len(second))
|
||||
set.AddMultiple(first...)
|
||||
set.AddMultiple(second...)
|
||||
return set.Values()
|
||||
}
|
||||
|
||||
// Get data related to provided filename and distribution, for package files
|
||||
// update download counter.
|
||||
func GetFileObject(ctx *context.Context, distro, file string) (storage.Object, error) {
|
||||
@ -94,40 +47,6 @@ func GetFileObject(ctx *context.Context, distro, file string) (storage.Object, e
|
||||
return cs.Get(packages.BlobHash256Key(blob.HashSHA256))
|
||||
}
|
||||
|
||||
// Get package property and transform it to string.
|
||||
func GetProperty(ctx *context.Context, owner, key string) ([]byte, error) {
|
||||
u, err := user.GetUserByName(ctx, owner)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
k := strings.Split(key, "-")
|
||||
|
||||
ver, err := pkg_model.GetVersionByNameAndVersion(
|
||||
ctx, u.ID, pkg_model.TypeArch, strings.Join(k[1:len(k)-3], "-"),
|
||||
strings.Join(k[len(k)-3:len(k)-1], "-"),
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
pp, err := pkg_model.GetPropertiesByName(ctx, 0, ver.ID, key)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for _, property := range pp {
|
||||
switch {
|
||||
case strings.HasSuffix(key, ".desc"):
|
||||
return []byte(property.Value), nil
|
||||
case strings.HasSuffix(key, ".sig"):
|
||||
return hex.DecodeString(property.Value)
|
||||
}
|
||||
}
|
||||
|
||||
return nil, errors.New("unable to find package signature")
|
||||
}
|
||||
|
||||
// Automatically connect repository with source code to published package, if
|
||||
// repository with the same name exists in user/organization scope.
|
||||
func RepoConnect(ctx *context.Context, owner, repo string, pkgid int64) error {
|
||||
@ -178,62 +97,18 @@ func CreatePacmanDb(ctx *context.Context, p *DbParams) ([]byte, error) {
|
||||
})
|
||||
|
||||
for _, version := range versions {
|
||||
desc, err := LoadDbDescFile(ctx, &DescParams{
|
||||
Version: version,
|
||||
Arch: p.Architecture,
|
||||
Distro: p.Distribution,
|
||||
PkgName: pkg.Name,
|
||||
Owner: p.Owner,
|
||||
})
|
||||
p, err := pkg_model.GetPropertieWithUniqueName(ctx, fmt.Sprintf(
|
||||
"%s-%s-%s-%s.pkg.tar.zst.desc",
|
||||
p.Distribution, pkg.Name, version.Version, p.Architecture,
|
||||
))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if desc == nil {
|
||||
continue
|
||||
}
|
||||
entries[pkg.Name+"-"+version.Version+"/desc"] = desc
|
||||
|
||||
entries[pkg.Name+"-"+version.Version+"/desc"] = []byte(p.Value)
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return arch.CreatePacmanDb(entries)
|
||||
}
|
||||
|
||||
type DescParams struct {
|
||||
Version *pkg_model.PackageVersion
|
||||
Arch string
|
||||
Distro string
|
||||
PkgName string
|
||||
Owner string
|
||||
}
|
||||
|
||||
// Get pacman desc file from object storage if combination of distribution and
|
||||
// architecture is supported (checked in metadata).
|
||||
func LoadDbDescFile(ctx *context.Context, p *DescParams) ([]byte, error) {
|
||||
var md arch.Metadata
|
||||
err := json.Unmarshal([]byte(p.Version.MetadataJSON), &md)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for _, distroarch := range md.DistroArch {
|
||||
var arch string
|
||||
|
||||
if distroarch == p.Distro+"-"+p.Arch {
|
||||
arch = p.Arch
|
||||
}
|
||||
if distroarch == p.Distro+"-any" {
|
||||
arch = "any"
|
||||
}
|
||||
|
||||
if arch == "" {
|
||||
continue
|
||||
}
|
||||
|
||||
return GetProperty(ctx, p.Owner, fmt.Sprintf(
|
||||
"%s-%s-%s-%s.pkg.tar.zst.desc",
|
||||
p.Distro, p.PkgName, p.Version.Version, arch,
|
||||
))
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
|
@ -71,13 +71,6 @@ Server = <gitea-origin-url data-url="{{AppSubUrl}}/api/packages/{{.PackageDescri
|
||||
<td>{{StringUtils.Join $.PackageDescriptor.Metadata.Backup ", "}}</td>
|
||||
</tr>
|
||||
{{end}}
|
||||
|
||||
{{if .PackageDescriptor.Metadata.DistroArch}}
|
||||
<tr>
|
||||
<td class="collapsing"><h5>Available for</h5></td>
|
||||
<td>{{StringUtils.Join $.PackageDescriptor.Metadata.DistroArch ", "}}</td>
|
||||
</tr>
|
||||
{{end}}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
|
Loading…
Reference in New Issue
Block a user