chore: stabilize lint and verify builds
This commit is contained in:
@@ -2,7 +2,7 @@ package storage_migrate
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/md5"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"io"
|
||||
@@ -36,7 +36,7 @@ func defaultProviders() container.Providers {
|
||||
func Command() atom.Option {
|
||||
return atom.Command(
|
||||
atom.Name("storage-migrate"),
|
||||
atom.Short("migrate media assets to md5 object keys"),
|
||||
atom.Short("migrate media assets to sha256 object keys"),
|
||||
atom.Arguments(func(cmd *cobra.Command) {
|
||||
cmd.Flags().Bool("dry-run", false, "preview changes without writing")
|
||||
cmd.Flags().Int("batch", 200, "batch size per scan")
|
||||
@@ -54,7 +54,7 @@ type Service struct {
|
||||
Storage *storage.Storage
|
||||
}
|
||||
|
||||
func Serve(cmd *cobra.Command, args []string) error {
|
||||
func Serve(cmd *cobra.Command, _ []string) error {
|
||||
return container.Container.Invoke(func(ctx context.Context, svc Service) error {
|
||||
models.SetDefault(svc.DB)
|
||||
|
||||
@@ -84,7 +84,6 @@ func Serve(cmd *cobra.Command, args []string) error {
|
||||
}
|
||||
|
||||
for _, asset := range list {
|
||||
// 仅处理本地存储且有实际文件路径的资源。
|
||||
if strings.ToLower(asset.Provider) != "local" {
|
||||
continue
|
||||
}
|
||||
@@ -96,13 +95,15 @@ func Serve(cmd *cobra.Command, args []string) error {
|
||||
}
|
||||
|
||||
srcPath := asset.ObjectKey
|
||||
|
||||
if !filepath.IsAbs(srcPath) {
|
||||
srcPath = filepath.Join(localPath, filepath.FromSlash(srcPath))
|
||||
}
|
||||
|
||||
hash, size, err := fileMD5(srcPath)
|
||||
hash, size, err := fileSHA256(srcPath)
|
||||
if err != nil {
|
||||
fmt.Printf("skip asset=%d err=%v\n", asset.ID, err)
|
||||
|
||||
continue
|
||||
}
|
||||
|
||||
@@ -164,26 +165,27 @@ func Serve(cmd *cobra.Command, args []string) error {
|
||||
}
|
||||
|
||||
func buildObjectKey(tenant *models.Tenant, hash, filename string) string {
|
||||
// 按租户维度组织对象路径:quyun/<tenant_uuid>/<md5>.<ext>
|
||||
tenantUUID := "public"
|
||||
if tenant != nil && tenant.UUID.String() != "" {
|
||||
tenantUUID = tenant.UUID.String()
|
||||
}
|
||||
ext := strings.ToLower(filepath.Ext(filename))
|
||||
|
||||
return path.Join("quyun", tenantUUID, hash+ext)
|
||||
}
|
||||
|
||||
func fileMD5(filename string) (string, int64, error) {
|
||||
func fileSHA256(filename string) (string, int64, error) {
|
||||
f, err := os.Open(filename)
|
||||
if err != nil {
|
||||
return "", 0, err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
h := md5.New()
|
||||
h := sha256.New()
|
||||
size, err := io.Copy(h, f)
|
||||
if err != nil {
|
||||
return "", size, err
|
||||
}
|
||||
|
||||
return hex.EncodeToString(h.Sum(nil)), size, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user