mirror of
https://github.com/truewhile/MeBox.git
synced 2026-09-28 11:16:37 +08:00
b0fe40142a
* Rebrand MMTL to MeBox across codebase and assets Rename the project display name, Go module path, environment variable prefix (MEBOX_*), Docker image references, and UI branding from MMTL/mmtl to MeBox/mebox. Replace logo assets with the new MeBox icon and keep legacy SQLite migration support for existing mmtl.db deployments. Co-authored-by: truewhile <truewhile@users.noreply.github.com> * Fix logo icons: use cube-only crop without truncated text Previous icon generation cropped too much of the source image, including partial MeBox wordmark text that was cut off in square icon containers. Regenerate logo-64/192/512, favicon, and SVG from cube-only region. Co-authored-by: truewhile <truewhile@users.noreply.github.com> --------- Co-authored-by: Cursor Agent <cursoragent@cursor.com> Co-authored-by: truewhile <truewhile@users.noreply.github.com>
92 lines
2.3 KiB
Go
92 lines
2.3 KiB
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
"github.com/truewhile/MeBox/internal/model"
|
|
)
|
|
|
|
const maxRecycleBinRecords = 200
|
|
|
|
// SoftDelete 物理删除媒体记录(统一硬删除以降低 SQLite 存储与索引压力)。
|
|
func (s *MediaService) SoftDelete(ctx context.Context, id string) error {
|
|
err := s.repo.DB.WithContext(ctx).Unscoped().Where("id = ?", id).Delete(&model.Media{}).Error
|
|
if err == nil {
|
|
s.invalidateMediaCache(ctx)
|
|
}
|
|
return err
|
|
}
|
|
|
|
// RestoreDeleted unsets DeletedAt for a single media row.
|
|
func (s *MediaService) RestoreDeleted(ctx context.Context, id string) error {
|
|
err := s.repo.DB.WithContext(ctx).Unscoped().Model(&model.Media{}).
|
|
Where("id = ?", id).Update("deleted_at", nil).Error
|
|
if err == nil {
|
|
s.invalidateMediaCache(ctx)
|
|
}
|
|
return err
|
|
}
|
|
|
|
// ListRecycleBin returns every soft-deleted row, newest first.
|
|
func (s *MediaService) ListRecycleBin(ctx context.Context, limit int) ([]model.Media, error) {
|
|
if err := pruneRecycleBinRows(ctx, s.repo.DB, maxRecycleBinRecords); err != nil {
|
|
return nil, err
|
|
}
|
|
if limit <= 0 || limit > maxRecycleBinRecords {
|
|
limit = maxRecycleBinRecords
|
|
}
|
|
var rows []model.Media
|
|
err := s.repo.DB.Unscoped().
|
|
Where("deleted_at IS NOT NULL").
|
|
Order("deleted_at desc").
|
|
Limit(limit).
|
|
Find(&rows).Error
|
|
return rows, err
|
|
}
|
|
|
|
func pruneRecycleBinRows(ctx context.Context, db *gorm.DB, keep int) error {
|
|
if db == nil {
|
|
return nil
|
|
}
|
|
if keep <= 0 {
|
|
keep = maxRecycleBinRecords
|
|
}
|
|
var rows []struct {
|
|
ID string
|
|
}
|
|
if err := db.WithContext(ctx).Unscoped().
|
|
Model(&model.Media{}).
|
|
Select("id").
|
|
Where("deleted_at IS NOT NULL").
|
|
Order("deleted_at desc").
|
|
Limit(100000).
|
|
Offset(keep).
|
|
Find(&rows).Error; err != nil {
|
|
return err
|
|
}
|
|
if len(rows) == 0 {
|
|
return nil
|
|
}
|
|
ids := make([]string, 0, len(rows))
|
|
for _, row := range rows {
|
|
if row.ID != "" {
|
|
ids = append(ids, row.ID)
|
|
}
|
|
}
|
|
if len(ids) == 0 {
|
|
return nil
|
|
}
|
|
return db.WithContext(ctx).Unscoped().Where("id IN ?", ids).Delete(&model.Media{}).Error
|
|
}
|
|
|
|
// PurgeDeleted permanently removes a soft-deleted row from the database.
|
|
func (s *MediaService) PurgeDeleted(ctx context.Context, id string) error {
|
|
err := s.repo.DB.WithContext(ctx).Unscoped().Where("id = ?", id).Delete(&model.Media{}).Error
|
|
if err == nil {
|
|
s.invalidateMediaCache(ctx)
|
|
}
|
|
return err
|
|
}
|