Add cleanup command that removes orphaned thumbnails and index entries

This commit is contained in:
Michael Mayer
2021-01-24 17:46:18 +01:00
parent 01f14d39c2
commit e04f0f937f
25 changed files with 340 additions and 28 deletions

View File

@@ -0,0 +1,65 @@
package commands
import (
"context"
"time"
"github.com/photoprism/photoprism/internal/config"
"github.com/photoprism/photoprism/internal/photoprism"
"github.com/photoprism/photoprism/internal/service"
"github.com/urfave/cli"
)
// CleanUpCommand registers the cleanup command.
var CleanUpCommand = cli.Command{
Name: "cleanup",
Usage: "Removes orphaned thumbnails and index entries",
Flags: cleanUpFlags,
Action: cleanUpAction,
}
var cleanUpFlags = []cli.Flag{
cli.BoolFlag{
Name: "dry",
Usage: "dry run, don't actually remove anything",
},
}
// cleanUpAction removes orphaned thumbnails and index entries.
func cleanUpAction(ctx *cli.Context) error {
start := time.Now()
conf := config.NewConfig(ctx)
service.SetConfig(conf)
_, cancel := context.WithCancel(context.Background())
defer cancel()
if err := conf.Init(); err != nil {
return err
}
conf.InitDb()
if conf.ReadOnly() {
log.Infof("cleanup: read-only mode enabled")
}
cleanUp := service.CleanUp()
opt := photoprism.CleanUpOptions{
Dry: ctx.Bool("dry"),
}
if thumbs, orphans, err := cleanUp.Start(opt); err != nil {
return err
} else {
elapsed := time.Since(start)
log.Infof("cleanup: removed %d orphaned thumbnails and %d photos in %s", thumbs, orphans, elapsed)
}
conf.Shutdown()
return nil
}