wip: oci image management
This commit is contained in:
64
controller/upload.go
Normal file
64
controller/upload.go
Normal file
@@ -0,0 +1,64 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"io"
|
||||
"strings"
|
||||
|
||||
"gitea.loveuer.com/loveuer/cluster/internal/registry/storage"
|
||||
)
|
||||
|
||||
// StartBlobUpload 开始 blob 上传
|
||||
func StartBlobUpload(store storage.Storage, repo string) (string, error) {
|
||||
return store.StartBlobUpload(repo)
|
||||
}
|
||||
|
||||
// PatchBlobUpload 上传 blob 数据块
|
||||
func PatchBlobUpload(store storage.Storage, uuid string, data io.Reader) (int64, error) {
|
||||
if err := store.PutBlobUploadChunk(uuid, data); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
// 获取当前上传大小
|
||||
size, err := store.GetBlobUpload(uuid)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
return size, nil
|
||||
}
|
||||
|
||||
// PutBlobUpload 完成 blob 上传
|
||||
func PutBlobUpload(store storage.Storage, repo, uuid, digest string, data interface{}, requestPath string) (string, error) {
|
||||
// 如果有请求体,先追加数据
|
||||
if data != nil {
|
||||
if reader, ok := data.(io.Reader); ok {
|
||||
if err := store.PutBlobUploadChunk(uuid, reader); err != nil {
|
||||
return "", err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 完成上传
|
||||
if err := store.CompleteBlobUpload(repo, uuid, digest); err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
// 清理上传文件
|
||||
store.DeleteBlobUpload(uuid)
|
||||
|
||||
// 返回 blob 位置
|
||||
// 从 /v2/{name}/blobs/uploads/{uuid} 转换为 /v2/{name}/blobs/{digest}
|
||||
pathParts := strings.Split(requestPath, "/")
|
||||
if len(pathParts) >= 4 {
|
||||
// 构建新的路径: /v2/{name}/blobs/{digest}
|
||||
location := "/v2/" + pathParts[2] + "/blobs/" + digest
|
||||
return location, nil
|
||||
}
|
||||
|
||||
return requestPath, nil
|
||||
}
|
||||
|
||||
// GetBlobUpload 获取上传状态
|
||||
func GetBlobUpload(store storage.Storage, uuid string) (int64, error) {
|
||||
return store.GetBlobUpload(uuid)
|
||||
}
|
||||
Reference in New Issue
Block a user