127 lines
3.6 KiB
Go
127 lines
3.6 KiB
Go
package handler
|
|
|
|
import (
|
|
"mstore/app/operator"
|
|
"mstore/app/router"
|
|
|
|
"sigs.k8s.io/yaml"
|
|
)
|
|
|
|
func (hand *Handler) DumpHeaders(message string, rctx *router.Context) {
|
|
headers := rctx.GetHeaders()
|
|
yamlData, _ := yaml.Marshal(headers)
|
|
hand.logg.Debugf("%s:\n%s\n", message, string(yamlData))
|
|
}
|
|
|
|
// 1 HEAD /v2/<name>/blobs/<digest> 200 404
|
|
func (hand *Handler) BlobExists(rctx *router.Context) {
|
|
name, _ := rctx.GetSubpath("name")
|
|
digest, _ := rctx.GetSubpath("digest")
|
|
|
|
hand.logg.Debugf("Handle BlobExists with name=[%s] digest=[%s]", name, digest)
|
|
params := &operator.BlobExistsParams{
|
|
Name: name,
|
|
Digest: digest,
|
|
}
|
|
ctx := rctx.GetContext()
|
|
res, code, err := hand.oper.BlobExists(ctx, params)
|
|
if err != nil {
|
|
hand.logg.Errorf("BlobExist error: %v", err)
|
|
} else if res.Exists {
|
|
rctx.SetHeader("Docker-Content-Digest", res.DockerContentDigest)
|
|
rctx.SetHeader("Content-Length", res.ContentLength)
|
|
}
|
|
rctx.SetStatus(code)
|
|
}
|
|
|
|
// 2 POST /v2/<name>/blobs/uploads/ 202 404
|
|
func (hand *Handler) PostUpload(rctx *router.Context) {
|
|
name, _ := rctx.GetSubpath("name")
|
|
digest := rctx.GetQuery("digest")
|
|
mount := rctx.GetQuery("mount")
|
|
from := rctx.GetQuery("from")
|
|
|
|
hand.logg.Debugf("Handle PostUpload with name=[%s] digest=[%s]", name, digest)
|
|
params := &operator.PostUploadParams{
|
|
Name: name,
|
|
Digest: digest,
|
|
Mount: mount,
|
|
From: from,
|
|
}
|
|
res, code, err := hand.oper.PostUpload(rctx.Ctx, params)
|
|
if err != nil {
|
|
hand.logg.Errorf("PostUpload error: %v", err)
|
|
} else {
|
|
hand.logg.Debugf("PostUpload send location=[%s] code=%d", res.Location, code)
|
|
rctx.SetHeader("Location", res.Location)
|
|
rctx.SetHeader("Content-Length", res.ContentLength)
|
|
rctx.SetHeader("Docker-Upload-UUID", res.DockerUploadUUID)
|
|
}
|
|
rctx.SetStatus(code)
|
|
}
|
|
|
|
// POST /v2/<name>/blobs/uploads/?digest=<digest> 201/202 404/400
|
|
// POST /v2/<name>/blobs/uploads/?mount=<digest>&from=<other_name> 201 404
|
|
|
|
// 3 PATCH /v2/<name>/blobs/uploads/<reference> 202 404/416
|
|
func (hand *Handler) PatchUpload(rctx *router.Context) {
|
|
|
|
hand.DumpHeaders("PatchUpload headers", rctx)
|
|
|
|
contentLength := rctx.GetHeader("Content-Length")
|
|
contentType := rctx.GetHeader("Content-Type")
|
|
contentRange := rctx.GetHeader("Content-Range")
|
|
|
|
name, _ := rctx.GetSubpath("name")
|
|
reference, _ := rctx.GetSubpath("reference")
|
|
reader := rctx.Request.Body
|
|
|
|
params := &operator.PatchUploadParams{
|
|
ContentLength: contentLength,
|
|
ContentType: contentType,
|
|
ContentRange: contentRange,
|
|
Name: name,
|
|
Reference: reference,
|
|
Reader: reader,
|
|
}
|
|
ctx := rctx.GetContext()
|
|
res, code, err := hand.oper.PatchUpload(ctx, params)
|
|
if err != nil {
|
|
hand.logg.Errorf("PatchUpload error: %v", err)
|
|
}
|
|
rctx.SetHeader("Location", res.Location)
|
|
rctx.SetStatus(code)
|
|
}
|
|
|
|
// 4 PUT /v2/<name>/blobs/uploads/<reference>?digest=<digest> 202 404/416
|
|
//
|
|
// PUT /v2/<name>/uploads/<reference>?digest=<digest> 202 404/416
|
|
func (hand *Handler) PutUpload(rctx *router.Context) {
|
|
|
|
hand.DumpHeaders("PutUpload headers", rctx)
|
|
|
|
contentType := rctx.GetHeader("Content-Type")
|
|
contentLength := rctx.GetHeader("Content-Length")
|
|
contentRange := rctx.GetHeader("Content-Range")
|
|
|
|
name, _ := rctx.GetSubpath("name")
|
|
reference, _ := rctx.GetSubpath("reference")
|
|
|
|
digest := rctx.GetQuery("digest")
|
|
|
|
params := &operator.PutUploadParams{
|
|
ContentLength: contentLength,
|
|
ContentType: contentType,
|
|
ContentRange: contentRange,
|
|
Name: name,
|
|
Reference: reference,
|
|
Digest: digest,
|
|
}
|
|
res, code, err := hand.oper.PutUpload(rctx.Ctx, params)
|
|
if err != nil {
|
|
hand.logg.Errorf("PutUpload error: %v", err)
|
|
}
|
|
rctx.SetHeader("Location", res.Location)
|
|
rctx.SetStatus(code)
|
|
}
|