Files
mstore/app/imageoper/getman.go
T
2026-03-13 19:02:42 +02:00

98 lines
2.6 KiB
Go

/*
* Copyright 2026 Oleg Borodin <onborodin@gmail.com>
*
* This work is published and licensed under a Creative Commons
* Attribution-NonCommercial-NoDerivatives 4.0 International License.
*
* Distribution of this work is permitted, but commercial use and
* modifications are strictly prohibited.
*/
package imageoper
import (
"context"
"fmt"
"net/http"
"strconv"
"mstore/pkg/descr"
ocidigest "github.com/opencontainers/go-digest"
)
type GetManifestParams struct {
Name string
Reference string
}
type GetManifestResult struct {
ContentLength string
ContentType string
DockerContentDigest string
Payload string
}
func (oper *Operator) GetManifest(ctx context.Context, params *GetManifestParams) (*GetManifestResult, int, error) {
var err error
res := &GetManifestResult{}
if params.Name == "" {
err = fmt.Errorf("Empty name")
return res, http.StatusBadRequest, err
}
if params.Reference == "" {
err = fmt.Errorf("Empty reference")
return res, http.StatusBadRequest, err
}
resName := params.Name
oper.iLock.WaitAndLock(resName)
defer oper.iLock.Done(resName)
manDescr := descr.Manifest{}
var exists bool
digobj, err := ocidigest.Parse(params.Reference)
if err == nil {
exists, manDescr, err = oper.mdb.GetManifestByDigest(ctx, params.Name, digobj.String())
if err != nil {
return res, http.StatusInternalServerError, err
}
if !exists {
return res, http.StatusNotFound, err
}
res.DockerContentDigest = manDescr.Digest
size := int64(len(manDescr.Payload))
res.ContentLength = strconv.FormatInt(size, 10)
res.ContentType = manDescr.ContentType
res.Payload = manDescr.Payload
} else {
// Create index of manifests. Or not.
exists, manDescrs, err := oper.mdb.GetManifestsByReference(ctx, params.Name, params.Reference)
if err != nil {
return res, http.StatusInternalServerError, err
}
if !exists {
return res, http.StatusNotFound, err
}
if len(manDescrs) == 1 {
res.DockerContentDigest = manDescr.Digest
size := int64(len(manDescr.Payload))
res.ContentLength = strconv.FormatInt(size, 10)
res.ContentType = manDescr.ContentType
res.Payload = manDescr.Payload
} else {
_, indexdata, err := indexFromManigestDescrs(manDescrs)
if err != nil {
return res, http.StatusInternalServerError, err
}
digobj := ocidigest.SHA256.FromBytes(indexdata)
res.DockerContentDigest = digobj.String()
size := int64(len(indexdata))
res.ContentLength = strconv.FormatInt(size, 10)
res.ContentType = oiiMediaType
res.Payload = string(indexdata)
}
}
return res, http.StatusOK, err
}