working commit

This commit is contained in:
2026-02-04 21:43:26 +02:00
parent 55e8abcdd3
commit 3900d93559
7 changed files with 340 additions and 68 deletions
+4 -4
View File
@@ -97,6 +97,7 @@ func (oper *Operator) PostUpload(ctx context.Context, params *PostUploadParams)
type PatchUploadParams struct {
ContentType string
ContentLength string
ContentRange string
Name string
Reference string
Reader io.Reader
@@ -129,7 +130,8 @@ func (oper *Operator) PatchUpload(ctx context.Context, params *PatchUploadParams
return res, http.StatusBadRequest, err
}
var contentLength int64
// podman & github.com/containers/image don't set Content-length header for docker transport
// Unfortunately, podman & github.com/containers/image don't set Content-length header for docker transport
if params.ContentLength != "" {
contentLength, err = strconv.ParseInt(params.ContentLength, 10, 64)
if err != nil {
@@ -138,7 +140,7 @@ func (oper *Operator) PatchUpload(ctx context.Context, params *PatchUploadParams
}
}
recsize, err := oper.store.WriteUpload(params.Reference, params.Reader)
recsize, _, err := oper.store.WriteUpload(params.Reference, params.Reader)
if err != nil {
return res, http.StatusInternalServerError, err
}
@@ -200,8 +202,6 @@ func (oper *Operator) PutUpload(ctx context.Context, params *PutUploadParams) (*
// TODO
err = fmt.Errorf("Unexpected Content-Length header: %s", params.ContentLength)
return res, http.StatusInternalServerError, err
Content - Range
}
err = oper.store.LinkUpload(params.Reference, params.Digest)
+119 -3
View File
@@ -3,12 +3,14 @@ package operator
import (
"bytes"
"context"
"errors"
"fmt"
"io"
"net/http"
"strconv"
"mstore/app/descr"
"mstore/pkg/auxoci"
ocidigest "github.com/opencontainers/go-digest"
)
@@ -94,8 +96,16 @@ func (oper *Operator) PutManifest(ctx context.Context, params *PutManifestParams
res := &PutManifestResult{}
oper.logg.Debugf("Put manifest %s:%s", params.Name, params.Reference)
if params.Reference == "" {
err = fmt.Errorf("Empty reference")
return res, http.StatusBadRequest, err
}
if params.Name == "" {
err = fmt.Errorf("Empty name")
return res, http.StatusBadRequest, err
}
// Check Content-Type
if params.ContentType != ddmMimeType && params.ContentType != oimMimeType {
if params.ContentType != oimMimeType {
err = fmt.Errorf("Unknown or empty Content-Type: %s", params.ContentType)
return res, http.StatusNotFound, err
}
@@ -106,8 +116,114 @@ func (oper *Operator) PutManifest(ctx context.Context, params *PutManifestParams
if err != nil {
return res, http.StatusInternalServerError, err
}
inBytes := buffer.Bytes()
oper.logg.Debugf("Manifest data: [%s]", string(inBytes))
incomingManifestBytes := buffer.Bytes()
oper.logg.Debugf("Manifest data: [%s]", string(incomingManifestBytes))
incomingManifest, err := auxoci.ParseOCIManifest(incomingManifestBytes)
if err != nil {
err = fmt.Errorf("Parsing OCI manifest error: %v", err)
return res, http.StatusInternalServerError, err
}
if incomingManifest.MediaType != params.ContentType {
err := fmt.Errorf("Mismatch Content-Type header and manifest MediaType: %s vs %s",
params.ContentType, incomingManifest.MediaType)
return res, http.StatusInternalServerError, err
}
manifestExists, _, err := oper.mdb.GetManifestByReference(ctx, params.Name, params.Reference)
if err != nil {
return res, http.StatusInternalServerError, err
}
if !manifestExists {
name := params.Name
reference := params.Reference
manifestDescr, layerDescrs, err := descrsFromManifest(name, reference, incomingManifest, incomingManifestBytes)
// Check layers
var blobError error
for _, layer := range layerDescrs {
layerExists, _, err := oper.store.BlobFileExists(layer.Digest)
if err != nil {
return res, http.StatusInternalServerError, err
}
if !layerExists {
err := fmt.Errorf("Layer %s not found", layer.Digest)
blobError = errors.Join(blobError, err)
}
}
if blobError != nil {
return res, http.StatusInternalServerError, blobError
}
// Store manifest and layesrs data
err = oper.mdb.InsertManifestWithLayers(ctx, &manifestDescr, layerDescrs)
if err != nil {
return res, http.StatusInternalServerError, err
}
}
/*
exists, existingManifestDescr, err := lg.mdb.GetManifestByReference(params.Name, params.Reference)
if err != nil {
return res, http.StatusInternalServerError, err
}
if exists {
existingManifestBytes := []byte(existingManifestDescr.Payload)
// Exist if incoming and existing manyfest is equal
if bytes.Equal(existingManifestBytes, incomingManifestBytes) {
return res, http.StatusCreated, err
}
name := params.Name
reference := params.Reference
manifestDescr, newBlobDescrs, delBlobDescrs, err := blobsDiff(name, reference, existingManifestBytes, incomingManifestBytes)
if err != nil {
return res, http.StatusInternalServerError, err
}
err = lg.maindb.UpdateManifest(ctx, &manifestDescr, newBlobDescrs, delBlobDescrs)
if err != nil {
return res, http.StatusInternalServerError, err
}
// Clean blobs
for _, blob := range delBlobDescrs {
exists, _, err = lg.st.BlobFileExists(blob.Digest)
if err != nil {
return res, http.StatusInternalServerError, err
}
blobUsage, err := lg.maindb.GetBlobUsage(blob.Digest)
if err != nil {
return res, http.StatusInternalServerError, err
}
if exists && blobUsage == 0 {
lg.log.Debugf("Delete file %s:%s blob %s", params.Name, params.Reference, blob.Digest)
err = lg.st.DeleteBlobFile(blob.Digest)
if err != nil {
return res, http.StatusInternalServerError, err
}
}
}
} else {
name := params.Name
reference := params.Reference
manifestDescr, configDescr, layerDescrs, err := descrsFromManifestBytes(name, reference, incomingManifestBytes)
// Check layer blobs
var blobError error
for _, layer := range layerDescrs {
exists, _, err = lg.st.BlobFileExists(layer.Digest)
if !exists {
err := fmt.Errorf("Blob %s not exists", layer.Digest)
blobError = errors.Join(blobError, err)
}
}
if blobError != nil {
return res, http.StatusInternalServerError, blobError
}
// Store manifest and blobs data
err = lg.maindb.InsertManifest(ctx, &manifestDescr, &configDescr, layerDescrs)
if err != nil {
return res, http.StatusInternalServerError, err
}
}
*/
res.Location = fmt.Sprintf(`/v2/%s/manifests/%s`, params.Name, params.Reference)
return res, http.StatusCreated, err
+65
View File
@@ -0,0 +1,65 @@
package operator
import (
"mstore/app/descr"
"mstore/pkg/auxtool"
"mstore/pkg/auxuuid"
ocidigest "github.com/opencontainers/go-digest"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
)
func descrsFromManifest(name, reference string, manifest *ocispec.Manifest, rawManifest []byte) (descr.Manifest, []descr.Blob, error) {
var err error
manifestDescr := descr.Manifest{}
//configDescr := descr.Blob{}
blobDescrs := make([]descr.Blob, 0)
timestamp := auxtool.TimeNow()
// Make manifest descriptor
manifestDigest := ocidigest.SHA256.FromBytes(rawManifest).String()
manifestDescr = descr.Manifest{
ID: auxuuid.NewUUID(),
Name: name,
Reference: reference,
Digest: manifestDigest,
ContentType: manifest.MediaType,
Payload: string(rawManifest),
CreatedAt: timestamp,
UpdatedAt: timestamp,
}
// Make config descriptor
ociConfig := manifest.Config
configDescr := descr.Blob{
ID: auxuuid.NewUUID(),
Name: name,
Reference: reference,
MediaType: ociConfig.MediaType,
Digest: string(ociConfig.Digest),
Size: ociConfig.Size,
CreatedAt: timestamp,
UpdatedAt: timestamp,
}
blobDescrs = append(blobDescrs, configDescr)
// Make blob descriptions
layerMap := make(map[string]bool)
for _, layer := range manifest.Layers {
blobDescr := descr.Blob{
ID: auxuuid.NewUUID(),
Name: name,
Reference: reference,
MediaType: layer.MediaType,
Digest: string(layer.Digest),
Size: layer.Size,
CreatedAt: timestamp,
UpdatedAt: timestamp,
}
_, alreadyAdded := layerMap[string(layer.Digest)]
if !alreadyAdded {
blobDescrs = append(blobDescrs, blobDescr)
layerMap[string(layer.Digest)] = true
}
}
return manifestDescr, blobDescrs, err
}