2023-07-05 14:48:09 +00:00
|
|
|
package registry
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
|
|
|
type ObjExporter struct {
|
|
|
|
selector *ObjSelector
|
|
|
|
}
|
|
|
|
|
|
|
|
type PreGenerateInfo struct {
|
2024-06-10 16:26:55 +00:00
|
|
|
Buckets []string `json:"buckets"`
|
|
|
|
Containers []string `json:"containers"`
|
|
|
|
Objects []ObjInfo `json:"objects"`
|
|
|
|
ObjSize string `json:"obj_size"`
|
2023-07-05 14:48:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type ObjInfo struct {
|
|
|
|
Bucket string `json:"bucket"`
|
|
|
|
Object string `json:"object"`
|
2024-06-10 16:26:55 +00:00
|
|
|
CID string `json:"cid"`
|
|
|
|
OID string `json:"oid"`
|
2023-07-05 14:48:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewObjExporter(selector *ObjSelector) *ObjExporter {
|
|
|
|
return &ObjExporter{selector: selector}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *ObjExporter) ExportJSONPreGen(fileName string) error {
|
|
|
|
f, err := os.Create(fileName)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
// there can be a lot of object, so manually form json
|
|
|
|
if _, err = f.WriteString(`{"objects":[`); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
bucketMap := make(map[string]struct{})
|
2024-05-31 18:20:23 +00:00
|
|
|
containerMap := make(map[string]struct{})
|
2023-07-05 14:48:09 +00:00
|
|
|
|
|
|
|
count, err := o.selector.Count()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var comma string
|
|
|
|
for i := 0; i < count; i++ {
|
2023-07-20 12:01:57 +00:00
|
|
|
info := o.selector.NextObject()
|
2023-07-05 14:48:09 +00:00
|
|
|
if info == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2024-05-31 18:20:23 +00:00
|
|
|
if err = writeObjectInfo(comma, info, f); err != nil {
|
2023-07-05 14:48:09 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if i == 0 {
|
|
|
|
comma = ","
|
|
|
|
}
|
|
|
|
|
2024-05-31 18:20:23 +00:00
|
|
|
if info.S3Bucket != "" {
|
|
|
|
bucketMap[info.S3Bucket] = struct{}{}
|
|
|
|
}
|
|
|
|
if info.CID != "" {
|
|
|
|
containerMap[info.CID] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err = f.WriteString(`]`); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(bucketMap) > 0 {
|
|
|
|
if err = writeContainerInfo("buckets", bucketMap, f); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(containerMap) > 0 {
|
|
|
|
if err = writeContainerInfo("containers", containerMap, f); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-07-05 14:48:09 +00:00
|
|
|
}
|
|
|
|
|
2024-05-31 18:20:23 +00:00
|
|
|
if _, err = f.WriteString(`}`); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func writeObjectInfo(comma string, info *ObjectInfo, f *os.File) (err error) {
|
|
|
|
var res string
|
|
|
|
if info.S3Bucket != "" || info.S3Key != "" {
|
|
|
|
res = fmt.Sprintf(`%s{"bucket":"%s","object":"%s"}`, comma, info.S3Bucket, info.S3Key)
|
|
|
|
} else {
|
|
|
|
res = fmt.Sprintf(`%s{"cid":"%s","oid":"%s"}`, comma, info.CID, info.OID)
|
|
|
|
}
|
|
|
|
_, err = f.WriteString(res)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func writeContainerInfo(attrName string, bucketMap map[string]struct{}, f *os.File) (err error) {
|
|
|
|
if _, err = f.WriteString(fmt.Sprintf(`,"%s":[`, attrName)); err != nil {
|
2023-07-05 14:48:09 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
i := 0
|
2024-05-31 18:20:23 +00:00
|
|
|
comma := ""
|
2023-07-05 14:48:09 +00:00
|
|
|
for bucket := range bucketMap {
|
|
|
|
if _, err = f.WriteString(fmt.Sprintf(`%s"%s"`, comma, bucket)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if i == 0 {
|
|
|
|
comma = ","
|
|
|
|
}
|
|
|
|
i++
|
|
|
|
}
|
2024-05-31 18:20:23 +00:00
|
|
|
_, err = f.WriteString(`]`)
|
2023-07-05 14:48:09 +00:00
|
|
|
return err
|
|
|
|
}
|