wip: oci image management
This commit is contained in:
27
controller/blob.go
Normal file
27
controller/blob.go
Normal file
@@ -0,0 +1,27 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"io"
|
||||
|
||||
"gitea.loveuer.com/loveuer/cluster/internal/registry/storage"
|
||||
)
|
||||
|
||||
// GetBlob 获取 blob
|
||||
func GetBlob(store storage.Storage, repo, digest string) (io.ReadCloser, int64, error) {
|
||||
return store.GetBlob(repo, digest)
|
||||
}
|
||||
|
||||
// HeadBlob 检查 blob 是否存在并返回大小
|
||||
func HeadBlob(store storage.Storage, repo, digest string) (bool, int64, error) {
|
||||
exists, err := store.BlobExists(repo, digest)
|
||||
if err != nil || !exists {
|
||||
return false, 0, err
|
||||
}
|
||||
|
||||
size, err := store.GetBlobSize(repo, digest)
|
||||
if err != nil {
|
||||
return false, 0, err
|
||||
}
|
||||
|
||||
return true, size, nil
|
||||
}
|
||||
57
controller/manifest.go
Normal file
57
controller/manifest.go
Normal file
@@ -0,0 +1,57 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
|
||||
"gitea.loveuer.com/loveuer/cluster/internal/registry/storage"
|
||||
)
|
||||
|
||||
// GetManifest 获取 manifest
|
||||
func GetManifest(store storage.Storage, repo, reference string) ([]byte, string, string, error) {
|
||||
data, mediaType, err := store.GetManifest(repo, reference)
|
||||
if err != nil {
|
||||
return nil, "", "", err
|
||||
}
|
||||
|
||||
digest := calculateDigest(data)
|
||||
return data, mediaType, digest, nil
|
||||
}
|
||||
|
||||
// PutManifest 推送 manifest
|
||||
func PutManifest(store storage.Storage, repo, reference string, data []byte, mediaType string) (string, error) {
|
||||
if err := store.PutManifest(repo, reference, data, mediaType); err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
digest := calculateDigest(data)
|
||||
return digest, nil
|
||||
}
|
||||
|
||||
// DeleteManifest 删除 manifest
|
||||
func DeleteManifest(store storage.Storage, repo, reference string) error {
|
||||
return store.DeleteManifest(repo, reference)
|
||||
}
|
||||
|
||||
// HeadManifest 检查 manifest 是否存在并返回元数据
|
||||
func HeadManifest(store storage.Storage, repo, reference string) (bool, []byte, string, string, error) {
|
||||
exists, err := store.ManifestExists(repo, reference)
|
||||
if err != nil || !exists {
|
||||
return false, nil, "", "", err
|
||||
}
|
||||
|
||||
// 获取 manifest 以设置正确的 headers
|
||||
data, mediaType, err := store.GetManifest(repo, reference)
|
||||
if err != nil {
|
||||
return false, nil, "", "", err
|
||||
}
|
||||
|
||||
digest := calculateDigest(data)
|
||||
return true, data, mediaType, digest, nil
|
||||
}
|
||||
|
||||
// calculateDigest 计算 SHA256 digest
|
||||
func calculateDigest(data []byte) string {
|
||||
hash := sha256.Sum256(data)
|
||||
return "sha256:" + hex.EncodeToString(hash[:])
|
||||
}
|
||||
14
controller/registry.go
Normal file
14
controller/registry.go
Normal file
@@ -0,0 +1,14 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"gitea.loveuer.com/loveuer/cluster/internal/database"
|
||||
)
|
||||
|
||||
// ListImages 返回所有仓库列表
|
||||
func ListImages() ([]database.Repository, error) {
|
||||
var repos []database.Repository
|
||||
if err := database.DB.Find(&repos).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return repos, nil
|
||||
}
|
||||
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