2021-01-24 17:46:18 +01:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"time"
|
|
|
|
|
2021-10-02 14:24:44 +02:00
|
|
|
"github.com/dustin/go-humanize/english"
|
2021-09-22 19:33:41 +02:00
|
|
|
"github.com/urfave/cli"
|
|
|
|
|
2021-01-24 17:46:18 +01:00
|
|
|
"github.com/photoprism/photoprism/internal/config"
|
|
|
|
"github.com/photoprism/photoprism/internal/photoprism"
|
|
|
|
"github.com/photoprism/photoprism/internal/service"
|
|
|
|
)
|
|
|
|
|
|
|
|
// CleanUpCommand registers the cleanup command.
|
|
|
|
var CleanUpCommand = cli.Command{
|
|
|
|
Name: "cleanup",
|
2022-07-21 20:23:00 +02:00
|
|
|
Usage: "Removes orphaned index entries, sidecar and thumbnail files",
|
2021-01-24 17:46:18 +01:00
|
|
|
Flags: cleanUpFlags,
|
|
|
|
Action: cleanUpAction,
|
|
|
|
}
|
|
|
|
|
|
|
|
var cleanUpFlags = []cli.Flag{
|
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "dry",
|
|
|
|
Usage: "dry run, don't actually remove anything",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2022-07-21 20:23:00 +02:00
|
|
|
// cleanUpAction removes orphaned index entries, sidecar and thumbnail files.
|
2021-01-24 17:46:18 +01:00
|
|
|
func cleanUpAction(ctx *cli.Context) error {
|
2022-07-22 19:18:42 +02:00
|
|
|
cleanupStart := time.Now()
|
2021-01-24 17:46:18 +01:00
|
|
|
|
|
|
|
conf := config.NewConfig(ctx)
|
|
|
|
service.SetConfig(conf)
|
|
|
|
|
|
|
|
_, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
if err := conf.Init(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
conf.InitDb()
|
2022-09-28 09:01:17 +02:00
|
|
|
defer conf.Shutdown()
|
2021-01-24 17:46:18 +01:00
|
|
|
|
|
|
|
if conf.ReadOnly() {
|
2021-11-18 03:02:10 +01:00
|
|
|
log.Infof("config: read-only mode enabled")
|
2021-01-24 17:46:18 +01:00
|
|
|
}
|
|
|
|
|
2021-08-13 20:04:59 +02:00
|
|
|
w := service.CleanUp()
|
2021-01-24 17:46:18 +01:00
|
|
|
|
|
|
|
opt := photoprism.CleanUpOptions{
|
|
|
|
Dry: ctx.Bool("dry"),
|
|
|
|
}
|
|
|
|
|
2022-07-22 19:18:42 +02:00
|
|
|
// Start cleanup worker.
|
|
|
|
if thumbnails, _, sidecars, err := w.Start(opt); err != nil {
|
2021-01-24 17:46:18 +01:00
|
|
|
return err
|
2022-07-22 19:18:42 +02:00
|
|
|
} else if total := thumbnails + sidecars; total > 0 {
|
|
|
|
log.Infof("removed %s in %s", english.Plural(total, "file", "files"), time.Since(cleanupStart))
|
2021-01-24 17:46:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|