2020-05-07 19:42:04 +02:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"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"
|
|
|
|
|
2022-10-15 21:54:11 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/get"
|
2020-05-07 19:42:04 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/photoprism"
|
2022-04-15 09:42:07 +02:00
|
|
|
"github.com/photoprism/photoprism/pkg/clean"
|
2020-05-07 19:42:04 +02:00
|
|
|
"github.com/photoprism/photoprism/pkg/fs"
|
|
|
|
)
|
|
|
|
|
2023-02-14 11:37:22 +01:00
|
|
|
// PurgeCommand configures the command name, flags, and action.
|
2020-05-07 19:42:04 +02:00
|
|
|
var PurgeCommand = cli.Command{
|
|
|
|
Name: "purge",
|
2021-10-07 11:43:43 +02:00
|
|
|
Usage: "Updates missing files, photo counts, and album covers",
|
2020-05-07 19:42:04 +02:00
|
|
|
Flags: purgeFlags,
|
|
|
|
Action: purgeAction,
|
|
|
|
}
|
|
|
|
|
|
|
|
var purgeFlags = []cli.Flag{
|
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "hard",
|
2021-10-05 22:44:27 +02:00
|
|
|
Usage: "permanently remove from index",
|
2020-05-07 20:33:11 +02:00
|
|
|
},
|
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "dry",
|
|
|
|
Usage: "dry run, don't actually remove anything",
|
2020-05-07 19:42:04 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// purgeAction removes missing files from search results
|
|
|
|
func purgeAction(ctx *cli.Context) error {
|
|
|
|
start := time.Now()
|
|
|
|
|
2022-10-04 12:27:40 +02:00
|
|
|
conf, err := InitConfig(ctx)
|
2020-05-07 19:42:04 +02:00
|
|
|
|
2020-10-08 08:52:03 +02:00
|
|
|
_, cancel := context.WithCancel(context.Background())
|
2020-05-07 19:42:04 +02:00
|
|
|
defer cancel()
|
2020-10-08 08:52:03 +02:00
|
|
|
|
2022-10-04 12:27:40 +02:00
|
|
|
if err != nil {
|
2020-05-07 19:42:04 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
conf.InitDb()
|
2022-09-28 09:01:17 +02:00
|
|
|
defer conf.Shutdown()
|
2020-05-07 19:42:04 +02:00
|
|
|
|
|
|
|
// get cli first argument
|
|
|
|
subPath := strings.TrimSpace(ctx.Args().First())
|
|
|
|
|
|
|
|
if subPath == "" {
|
2022-04-15 09:42:07 +02:00
|
|
|
log.Infof("purge: removing missing files in %s", clean.Log(filepath.Base(conf.OriginalsPath())))
|
2020-05-07 19:42:04 +02:00
|
|
|
} else {
|
2022-04-15 09:42:07 +02:00
|
|
|
log.Infof("purge: removing missing files in %s", clean.Log(fs.RelName(filepath.Join(conf.OriginalsPath(), subPath), filepath.Dir(conf.OriginalsPath()))))
|
2020-05-07 19:42:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if conf.ReadOnly() {
|
2023-02-09 13:14:56 +01:00
|
|
|
log.Infof("config: enabled read-only mode")
|
2020-05-07 19:42:04 +02:00
|
|
|
}
|
|
|
|
|
2022-10-15 21:54:11 +02:00
|
|
|
w := get.Purge()
|
2020-05-07 19:42:04 +02:00
|
|
|
|
|
|
|
opt := photoprism.PurgeOptions{
|
|
|
|
Path: subPath,
|
2020-05-07 21:55:34 +02:00
|
|
|
Dry: ctx.Bool("dry"),
|
2020-05-07 19:42:04 +02:00
|
|
|
Hard: ctx.Bool("hard"),
|
|
|
|
}
|
|
|
|
|
2021-08-13 20:04:59 +02:00
|
|
|
if files, photos, err := w.Start(opt); err != nil {
|
2020-05-07 19:42:04 +02:00
|
|
|
return err
|
|
|
|
} else {
|
2021-10-02 15:34:41 +02: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 19:42:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|