8d838d9cb4
build / build (push) Successful in 1m45s
执行时真实时钟与持久授权回读(撤销/过期即停并保留预备份)、写集合写前落盘、 租约与 attempt 身份、恢复队列与原发送身份分离、成功收尾必须释放维护后再登记终态。
152 lines
5.0 KiB
Go
152 lines
5.0 KiB
Go
package v071
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"io"
|
|
"strings"
|
|
)
|
|
|
|
type ObjectStore interface {
|
|
Put(ctx context.Context, provider, path string, reader io.Reader, size int64) error
|
|
Stat(ctx context.Context, provider, path string) (size int64, sha256 string, err error)
|
|
Copy(ctx context.Context, provider, sourcePath, targetPath string) error
|
|
}
|
|
|
|
type TargetObjectConfig struct {
|
|
StorageBackendID string
|
|
Provider string
|
|
}
|
|
|
|
type StagedObject struct {
|
|
Handle string
|
|
StagingPath string
|
|
FinalPath string
|
|
Size int64
|
|
SHA256 string
|
|
}
|
|
|
|
type ObjectStage struct {
|
|
ReleaseRevision string
|
|
Provider string
|
|
Objects []StagedObject
|
|
}
|
|
|
|
type ObjectImporter struct {
|
|
store ObjectStore
|
|
config TargetObjectConfig
|
|
}
|
|
|
|
func NewObjectImporter(store ObjectStore, config TargetObjectConfig) (*ObjectImporter, error) {
|
|
config.StorageBackendID = strings.TrimSpace(config.StorageBackendID)
|
|
config.Provider = strings.TrimSpace(config.Provider)
|
|
if store == nil || !validSearchIdentifier(config.StorageBackendID) || !validObjectProvider(config.Provider) {
|
|
return nil, errors.New("invalid target object importer configuration")
|
|
}
|
|
return &ObjectImporter{store: store, config: config}, nil
|
|
}
|
|
|
|
// CheckLegacyObjectWrites has no configuration bypass. Document-level staging
|
|
// and promotion do not carry an immutable WriteSet/attempt grant, so they cannot
|
|
// participate in the exclusive object write domain. Read-only export is intact.
|
|
func CheckLegacyObjectWrites() error {
|
|
return errors.New("legacy object writes disabled: use the frozen-plan library worker and object gateway")
|
|
}
|
|
|
|
func (importer *ObjectImporter) Stage(context.Context, string, ObjectSnapshot) (ObjectStage, error) {
|
|
return ObjectStage{}, CheckLegacyObjectWrites()
|
|
}
|
|
|
|
func (importer *ObjectImporter) Promote(context.Context, ObjectStage) error {
|
|
return CheckLegacyObjectWrites()
|
|
}
|
|
|
|
// rewriteLocatorBucket 把资源定位符里的桶换成目标桶,结构保持不变:
|
|
// "storage://<backend>/<provider>://<source-bucket>/<key>" →
|
|
// "storage://<backend>/<provider>://<target-bucket>/<key>"。
|
|
//
|
|
// 为什么必须改:同步把字节写进目标桶,但行是从快照原样搬的,定位符里写着测试桶;
|
|
// 目标实例之后按这一行去取图/附件时会指向测试环境的桶(跨环境引用),
|
|
// 或者干脆取不到。键不变,只换桶。
|
|
func rewriteLocatorBucket(locator, bucket string) (string, bool) {
|
|
return rewriteLocatorTarget(locator, "", bucket)
|
|
}
|
|
|
|
// rewriteLocatorTarget 同时改写定位符里的存储后端 id 与桶。
|
|
//
|
|
// 为什么后端 id 也要改:storage://<id>/minio://<bucket>/<key> 里的 id 是来源实例
|
|
// storage_backends 的主键。目标实例有自己的行;只改桶不改 id,原版应用会以
|
|
// "storage backend not found" 落到默认服务,然后拒绝整条路径(真跑 500 code1007)。
|
|
func rewriteLocatorTarget(locator, backendID, bucket string) (string, bool) {
|
|
if bucket == "" || locator == "" || locator != strings.TrimSpace(locator) {
|
|
return "", false
|
|
}
|
|
prefix := ""
|
|
rest := locator
|
|
if strings.HasPrefix(locator, "storage://") {
|
|
trimmed := strings.TrimPrefix(locator, "storage://")
|
|
parts := strings.SplitN(trimmed, "/", 2)
|
|
if len(parts) != 2 || !validSearchIdentifier(parts[0]) {
|
|
return "", false
|
|
}
|
|
if parts[1] == "" {
|
|
return "", false
|
|
}
|
|
if backendID != "" {
|
|
if !validSearchIdentifier(backendID) {
|
|
return "", false
|
|
}
|
|
parts[0] = backendID
|
|
}
|
|
prefix, rest = "storage://"+parts[0]+"/", parts[1]
|
|
}
|
|
separator := strings.Index(rest, "://")
|
|
if separator < 0 {
|
|
// 裸对象键里没有桶:落盘时用的是配置桶,不需要改,也不算失败。
|
|
return locator, true
|
|
}
|
|
scheme := rest[:separator]
|
|
if !validSearchIdentifier(scheme) {
|
|
return "", false
|
|
}
|
|
schemeless := rest[separator+3:]
|
|
boundary := strings.Index(schemeless, "/")
|
|
if boundary <= 0 || boundary == len(schemeless)-1 {
|
|
return "", false
|
|
}
|
|
key := schemeless[boundary+1:]
|
|
if key == "" || !validSearchIdentifier(schemeless[:boundary]) {
|
|
return "", false
|
|
}
|
|
return prefix + scheme + "://" + bucket + "/" + key, true
|
|
}
|
|
|
|
func providerPathFromLocator(locator string) (string, bool) {
|
|
rest := locator
|
|
if strings.HasPrefix(locator, "storage://") {
|
|
rest = strings.TrimPrefix(locator, "storage://")
|
|
parts := strings.SplitN(rest, "/", 2)
|
|
if len(parts) != 2 || !validSearchIdentifier(parts[0]) || !validObjectLocator(parts[1]) {
|
|
return "", false
|
|
}
|
|
rest = parts[1]
|
|
} else if !validObjectLocator(locator) {
|
|
return "", false
|
|
}
|
|
// WeKnora nests the provider locator inside the storage locator:
|
|
// "storage://<backend-id>/oss://<bucket>/<key>". S3 keys address the
|
|
// bucket configured for the store, so strip the inner scheme and bucket.
|
|
if separator := strings.Index(rest, "://"); separator >= 0 {
|
|
if !validSearchIdentifier(rest[:separator]) {
|
|
return "", false
|
|
}
|
|
schemeless := rest[separator+3:]
|
|
boundary := strings.Index(schemeless, "/")
|
|
if boundary < 0 || boundary == len(schemeless)-1 {
|
|
return "", false
|
|
}
|
|
rest = schemeless[boundary+1:]
|
|
}
|
|
return rest, validObjectLocator(rest)
|
|
}
|