mirror of
https://github.com/truewhile/MeBox.git
synced 2026-10-02 04:16:38 +08:00
fix: add storage deletion with library cleanup and scan validation
- Delete storage config now properly removes related libraries and media - Scanner validates storage config exists before starting cloud scan - Prevents orphaned scans when storage is removed - Added repository methods: DeleteByType, FindByID for storage config - Remove unused storageHandler route to fix compilation
This commit is contained in:
@@ -697,6 +697,15 @@ func (s *ScannerService) scanCloudLibrary(ctx context.Context, lib *model.Librar
|
||||
if s.storage == nil {
|
||||
return res, fmt.Errorf("cloud storage service unavailable")
|
||||
}
|
||||
|
||||
// 验证存储配置是否存在且已启用
|
||||
cfg, err := s.repo.StorageConfig.FindByID(ctx, mount.Provider)
|
||||
if err != nil || cfg == nil {
|
||||
return res, fmt.Errorf("storage config not found: %s", mount.Provider)
|
||||
}
|
||||
if !cfg.Enabled {
|
||||
return res, fmt.Errorf("storage %s is disabled", mount.Provider)
|
||||
}
|
||||
typ := mount.Provider
|
||||
rootDir := mount.ScanDir
|
||||
rootDisplayDir := mount.DisplayDir
|
||||
@@ -1428,3 +1437,5 @@ func (s *ScannerService) maybeGenerateSTRMAfterScan(libraryID string) {
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -340,3 +340,6 @@ var _ = time.Now
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -495,3 +495,52 @@ func strr(v any) string {
|
||||
}
|
||||
return strings.TrimSpace(fmt.Sprint(v))
|
||||
}
|
||||
|
||||
|
||||
// DeleteStorage 删除存储配置并清理关联数据
|
||||
func (s *StorageConfigService) DeleteStorage(ctx context.Context, storageType string) error {
|
||||
// 查找配置
|
||||
cfg, err := s.repo.StorageConfig.Get(ctx, storageType)
|
||||
if err != nil || cfg == nil {
|
||||
return fmt.Errorf("storage config not found: %s", storageType)
|
||||
}
|
||||
|
||||
// 查找使用该存储的云盘库
|
||||
libs, err := s.repo.Library.List(ctx)
|
||||
if err != nil {
|
||||
return fmt.Errorf("list libraries: %w", err)
|
||||
}
|
||||
|
||||
var affectedLibs []string
|
||||
for _, lib := range libs {
|
||||
// 检查是否是云盘库且Provider匹配
|
||||
if mount, ok := ParseCloudLibraryMount(lib.Path); ok && mount.Provider == storageType {
|
||||
affectedLibs = append(affectedLibs, lib.ID)
|
||||
}
|
||||
}
|
||||
|
||||
// 删除关联媒体
|
||||
for _, libID := range affectedLibs {
|
||||
if err := s.repo.Media.PurgeByLibrary(ctx, libID); err != nil {
|
||||
s.log.Warn("purge media by library failed", zap.String("library_id", libID), zap.Error(err))
|
||||
}
|
||||
}
|
||||
|
||||
// 删除关联库
|
||||
for _, libID := range affectedLibs {
|
||||
if err := s.repo.Library.Delete(ctx, libID); err != nil {
|
||||
s.log.Warn("delete library failed", zap.String("library_id", libID), zap.Error(err))
|
||||
}
|
||||
}
|
||||
|
||||
// 删除存储配置
|
||||
if err := s.repo.StorageConfig.Delete(ctx, cfg.ID); err != nil {
|
||||
return fmt.Errorf("delete storage config: %w", err)
|
||||
}
|
||||
|
||||
s.log.Info("storage deleted",
|
||||
zap.String("storage_type", storageType),
|
||||
zap.Int("libraries_deleted", len(affectedLibs)))
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user