2020-05-07 17:42:04 +00:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2021-10-02 12:24:44 +00:00
|
|
|
"github.com/dustin/go-humanize/english"
|
|
|
|
|
2021-09-22 17:33:41 +00:00
|
|
|
"github.com/urfave/cli"
|
|
|
|
|
2020-05-07 17:42:04 +00:00
|
|
|
"github.com/photoprism/photoprism/internal/config"
|
|
|
|
"github.com/photoprism/photoprism/internal/photoprism"
|
|
|
|
"github.com/photoprism/photoprism/internal/service"
|
|
|
|
"github.com/photoprism/photoprism/pkg/fs"
|
2021-12-14 19:01:39 +00:00
|
|
|
"github.com/photoprism/photoprism/pkg/sanitize"
|
2020-05-07 17:42:04 +00:00
|
|
|
)
|
|
|
|
|
2021-01-24 16:46:18 +00:00
|
|
|
// PurgeCommand registers the index cli command.
|
2020-05-07 17:42:04 +00:00
|
|
|
var PurgeCommand = cli.Command{
|
|
|
|
Name: "purge",
|
2021-10-07 09:43:43 +00:00
|
|
|
Usage: "Updates missing files, photo counts, and album covers",
|
2020-05-07 17:42:04 +00:00
|
|
|
Flags: purgeFlags,
|
|
|
|
Action: purgeAction,
|
|
|
|
}
|
|
|
|
|
|
|
|
var purgeFlags = []cli.Flag{
|
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "hard",
|
2021-10-05 20:44:27 +00:00
|
|
|
Usage: "permanently remove from index",
|
2020-05-07 18:33:11 +00:00
|
|
|
},
|
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "dry",
|
|
|
|
Usage: "dry run, don't actually remove anything",
|
2020-05-07 17:42:04 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// purgeAction removes missing files from search results
|
|
|
|
func purgeAction(ctx *cli.Context) error {
|
|
|
|
start := time.Now()
|
|
|
|
|
|
|
|
conf := config.NewConfig(ctx)
|
|
|
|
service.SetConfig(conf)
|
|
|
|
|
2020-10-08 06:52:03 +00:00
|
|
|
_, cancel := context.WithCancel(context.Background())
|
2020-05-07 17:42:04 +00:00
|
|
|
defer cancel()
|
2020-10-08 06:52:03 +00:00
|
|
|
|
|
|
|
if err := conf.Init(); err != nil {
|
2020-05-07 17:42:04 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
conf.InitDb()
|
|
|
|
|
|
|
|
// get cli first argument
|
|
|
|
subPath := strings.TrimSpace(ctx.Args().First())
|
|
|
|
|
|
|
|
if subPath == "" {
|
2021-12-14 19:01:39 +00:00
|
|
|
log.Infof("purge: removing missing files in %s", sanitize.Log(filepath.Base(conf.OriginalsPath())))
|
2020-05-07 17:42:04 +00:00
|
|
|
} else {
|
2021-12-14 19:01:39 +00:00
|
|
|
log.Infof("purge: removing missing files in %s", sanitize.Log(fs.RelName(filepath.Join(conf.OriginalsPath(), subPath), filepath.Dir(conf.OriginalsPath()))))
|
2020-05-07 17:42:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if conf.ReadOnly() {
|
2021-11-18 02:02:10 +00:00
|
|
|
log.Infof("config: read-only mode enabled")
|
2020-05-07 17:42:04 +00:00
|
|
|
}
|
|
|
|
|
2021-08-13 18:04:59 +00:00
|
|
|
w := service.Purge()
|
2020-05-07 17:42:04 +00:00
|
|
|
|
|
|
|
opt := photoprism.PurgeOptions{
|
|
|
|
Path: subPath,
|
2020-05-07 19:55:34 +00:00
|
|
|
Dry: ctx.Bool("dry"),
|
2020-05-07 17:42:04 +00:00
|
|
|
Hard: ctx.Bool("hard"),
|
|
|
|
}
|
|
|
|
|
2021-08-13 18:04:59 +00:00
|
|
|
if files, photos, err := w.Start(opt); err != nil {
|
2020-05-07 17:42:04 +00:00
|
|
|
return err
|
|
|
|
} else {
|
2021-10-02 13:34:41 +00:00
|
|
|
log.Infof("purged %s and %s in %s", english.Plural(len(files), "file", "files"), english.Plural(len(photos), "photo", "photos"), time.Since(start))
|
2020-05-07 17:42:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
conf.Shutdown()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|