Files
cluster/handler/blob.go
2025-11-09 15:19:11 +08:00

77 lines
1.7 KiB
Go

package handler
import (
"strconv"
"strings"
"gitea.loveuer.com/loveuer/cluster/controller"
"gitea.loveuer.com/loveuer/cluster/internal/registry/storage"
"github.com/gofiber/fiber/v3"
)
// GetBlob 获取 blob
func GetBlob(store storage.Storage) fiber.Handler {
return func(c fiber.Ctx) error {
repo := c.Locals("repo_name")
repoStr := ""
if repo != nil {
repoStr = repo.(string)
}
if repoStr == "" {
repoStr = strings.TrimPrefix(c.Params("name"), "/")
}
digest := c.Locals("digest")
digestStr := ""
if digest != nil {
digestStr = digest.(string)
}
reader, size, err := controller.GetBlob(store, repoStr, digestStr)
if err != nil {
return c.Status(fiber.StatusNotFound).JSON(fiber.Map{
"errors": []fiber.Map{
{
"code": "BLOB_UNKNOWN",
"message": err.Error(),
},
},
})
}
defer reader.Close()
c.Set("Content-Type", "application/octet-stream")
c.Set("Content-Length", strconv.FormatInt(size, 10))
c.Set("Docker-Content-Digest", digestStr)
return c.SendStream(reader, int(size))
}
}
// HeadBlob 检查 blob 是否存在
func HeadBlob(store storage.Storage) fiber.Handler {
return func(c fiber.Ctx) error {
repo := c.Locals("repo_name")
repoStr := ""
if repo != nil {
repoStr = repo.(string)
}
if repoStr == "" {
repoStr = strings.TrimPrefix(c.Params("name"), "/")
}
digest := c.Locals("digest")
digestStr := ""
if digest != nil {
digestStr = digest.(string)
}
exists, size, err := controller.HeadBlob(store, repoStr, digestStr)
if err != nil || !exists {
return c.SendStatus(fiber.StatusNotFound)
}
c.Set("Content-Length", strconv.FormatInt(size, 10))
c.Set("Docker-Content-Digest", digestStr)
return c.SendStatus(fiber.StatusOK)
}
}