wip: oci image management
This commit is contained in:
192
internal/registry/handlers/upload.go
Normal file
192
internal/registry/handlers/upload.go
Normal file
@@ -0,0 +1,192 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"gitea.loveuer.com/loveuer/cluster/internal/registry/storage"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// StartBlobUpload 开始 blob 上传
|
||||
func StartBlobUpload(store storage.Storage) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
repo := c.GetString("repo_name")
|
||||
if repo == "" {
|
||||
repo = strings.TrimPrefix(c.Param("name"), "/")
|
||||
}
|
||||
|
||||
uuid, err := store.StartBlobUpload(repo)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{
|
||||
"errors": []gin.H{
|
||||
{
|
||||
"code": "INTERNAL_ERROR",
|
||||
"message": err.Error(),
|
||||
},
|
||||
},
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// 返回上传 URL
|
||||
location := c.Request.URL.Path + "/" + uuid
|
||||
c.Header("Location", location)
|
||||
c.Header("Docker-Upload-UUID", uuid)
|
||||
c.Header("Range", "0-0")
|
||||
c.Status(http.StatusAccepted)
|
||||
}
|
||||
}
|
||||
|
||||
// PatchBlobUpload 上传 blob 数据块
|
||||
func PatchBlobUpload(store storage.Storage) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
uuid := c.Param("uuid")
|
||||
|
||||
// 获取 Range header
|
||||
rangeHeader := c.GetHeader("Content-Range")
|
||||
if rangeHeader == "" {
|
||||
c.JSON(http.StatusBadRequest, gin.H{
|
||||
"errors": []gin.H{
|
||||
{
|
||||
"code": "BAD_REQUEST",
|
||||
"message": "Content-Range header required",
|
||||
},
|
||||
},
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// 读取数据
|
||||
data := c.Request.Body
|
||||
if err := store.PutBlobUploadChunk(uuid, data); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{
|
||||
"errors": []gin.H{
|
||||
{
|
||||
"code": "INTERNAL_ERROR",
|
||||
"message": err.Error(),
|
||||
},
|
||||
},
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// 获取当前上传大小
|
||||
size, err := store.GetBlobUpload(uuid)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusNotFound, gin.H{
|
||||
"errors": []gin.H{
|
||||
{
|
||||
"code": "UPLOAD_UNKNOWN",
|
||||
"message": err.Error(),
|
||||
},
|
||||
},
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
location := c.Request.URL.Path
|
||||
c.Header("Location", location)
|
||||
c.Header("Docker-Upload-UUID", uuid)
|
||||
c.Header("Range", "0-"+strconv.FormatInt(size-1, 10))
|
||||
c.Status(http.StatusNoContent)
|
||||
}
|
||||
}
|
||||
|
||||
// PutBlobUpload 完成 blob 上传
|
||||
func PutBlobUpload(store storage.Storage) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
repo := c.GetString("repo_name")
|
||||
if repo == "" {
|
||||
repo = strings.TrimPrefix(c.Param("name"), "/")
|
||||
}
|
||||
uuid := c.Param("uuid")
|
||||
|
||||
// 获取 digest
|
||||
digest := c.Query("digest")
|
||||
if digest == "" {
|
||||
c.JSON(http.StatusBadRequest, gin.H{
|
||||
"errors": []gin.H{
|
||||
{
|
||||
"code": "BAD_REQUEST",
|
||||
"message": "digest query parameter required",
|
||||
},
|
||||
},
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// 如果有请求体,先追加数据
|
||||
if c.Request.ContentLength > 0 {
|
||||
if err := store.PutBlobUploadChunk(uuid, c.Request.Body); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{
|
||||
"errors": []gin.H{
|
||||
{
|
||||
"code": "INTERNAL_ERROR",
|
||||
"message": err.Error(),
|
||||
},
|
||||
},
|
||||
})
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// 完成上传
|
||||
if err := store.CompleteBlobUpload(repo, uuid, digest); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{
|
||||
"errors": []gin.H{
|
||||
{
|
||||
"code": "BAD_REQUEST",
|
||||
"message": err.Error(),
|
||||
},
|
||||
},
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// 清理上传文件
|
||||
store.DeleteBlobUpload(uuid)
|
||||
|
||||
// 返回 blob 位置
|
||||
// 从 /v2/{name}/blobs/uploads/{uuid} 转换为 /v2/{name}/blobs/{digest}
|
||||
pathParts := strings.Split(c.Request.URL.Path, "/")
|
||||
if len(pathParts) >= 4 {
|
||||
// 构建新的路径: /v2/{name}/blobs/{digest}
|
||||
location := "/v2/" + pathParts[2] + "/blobs/" + digest
|
||||
c.Header("Location", location)
|
||||
} else {
|
||||
c.Header("Location", c.Request.URL.Path)
|
||||
}
|
||||
c.Header("Content-Length", "0")
|
||||
c.Header("Docker-Content-Digest", digest)
|
||||
c.Status(http.StatusCreated)
|
||||
}
|
||||
}
|
||||
|
||||
// GetBlobUpload 获取上传状态
|
||||
func GetBlobUpload(store storage.Storage) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
uuid := c.Param("uuid")
|
||||
|
||||
size, err := store.GetBlobUpload(uuid)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusNotFound, gin.H{
|
||||
"errors": []gin.H{
|
||||
{
|
||||
"code": "UPLOAD_UNKNOWN",
|
||||
"message": err.Error(),
|
||||
},
|
||||
},
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
location := c.Request.URL.Path
|
||||
c.Header("Location", location)
|
||||
c.Header("Docker-Upload-UUID", uuid)
|
||||
c.Header("Range", "0-"+strconv.FormatInt(size-1, 10))
|
||||
c.Status(http.StatusNoContent)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user