2018-09-24 09:53:16 +02:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
2019-06-03 22:58:15 +02:00
|
|
|
"context"
|
2020-05-06 15:53:47 +02:00
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
2019-05-14 16:04:17 +02:00
|
|
|
"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-08-13 20:04:59 +02:00
|
|
|
|
2019-05-06 23:18:10 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/config"
|
2018-09-24 09:53:16 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/photoprism"
|
2020-04-05 22:26:53 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/service"
|
2021-09-22 19:33:41 +02:00
|
|
|
"github.com/photoprism/photoprism/pkg/fs"
|
2020-05-06 15:53:47 +02:00
|
|
|
"github.com/photoprism/photoprism/pkg/txt"
|
2018-09-24 09:53:16 +02:00
|
|
|
)
|
|
|
|
|
2021-01-24 17:46:18 +01:00
|
|
|
// IndexCommand registers the index cli command.
|
2018-09-24 09:53:16 +02:00
|
|
|
var IndexCommand = cli.Command{
|
2021-04-20 08:54:44 +02:00
|
|
|
Name: "index",
|
2021-10-05 22:33:29 +02:00
|
|
|
Usage: "Indexes original media files",
|
2021-09-03 16:14:09 +02:00
|
|
|
ArgsUsage: "[path]",
|
2021-04-20 08:54:44 +02:00
|
|
|
Flags: indexFlags,
|
|
|
|
Action: indexAction,
|
2018-09-24 09:53:16 +02:00
|
|
|
}
|
|
|
|
|
2020-01-22 09:57:49 +01:00
|
|
|
var indexFlags = []cli.Flag{
|
|
|
|
cli.BoolFlag{
|
2021-09-03 16:14:09 +02:00
|
|
|
Name: "force, f",
|
2020-01-22 10:35:00 +01:00
|
|
|
Usage: "re-index all originals, including unchanged files",
|
2020-01-22 09:57:49 +01:00
|
|
|
},
|
2021-01-24 17:46:18 +01:00
|
|
|
cli.BoolFlag{
|
2021-09-03 16:14:09 +02:00
|
|
|
Name: "cleanup, c",
|
|
|
|
Usage: "remove orphan index entries and thumbnails",
|
2021-01-24 17:46:18 +01:00
|
|
|
},
|
2020-01-22 09:57:49 +01:00
|
|
|
}
|
|
|
|
|
2020-02-18 23:42:51 +01:00
|
|
|
// indexAction indexes all photos in originals directory (photo library)
|
2018-11-17 06:21:39 +01:00
|
|
|
func indexAction(ctx *cli.Context) error {
|
2019-05-14 16:04:17 +02:00
|
|
|
start := time.Now()
|
|
|
|
|
2019-05-06 23:18:10 +02:00
|
|
|
conf := config.NewConfig(ctx)
|
2020-04-05 22:26:53 +02:00
|
|
|
service.SetConfig(conf)
|
2019-06-30 05:38:39 +02:00
|
|
|
|
2020-10-08 08:52:03 +02:00
|
|
|
_, cancel := context.WithCancel(context.Background())
|
2019-06-03 22:58:15 +02:00
|
|
|
defer cancel()
|
2020-10-08 08:52:03 +02:00
|
|
|
|
|
|
|
if err := conf.Init(); err != nil {
|
2019-06-03 22:58:15 +02:00
|
|
|
return err
|
|
|
|
}
|
2018-09-24 09:53:16 +02:00
|
|
|
|
2020-04-30 20:07:03 +02:00
|
|
|
conf.InitDb()
|
2020-05-06 15:53:47 +02:00
|
|
|
|
2021-04-20 08:54:44 +02:00
|
|
|
// Use first argument to limit scope if set.
|
2020-05-06 15:53:47 +02:00
|
|
|
subPath := strings.TrimSpace(ctx.Args().First())
|
|
|
|
|
|
|
|
if subPath == "" {
|
2021-04-20 08:54:44 +02:00
|
|
|
log.Infof("indexing originals in %s", txt.Quote(conf.OriginalsPath()))
|
2020-05-06 15:53:47 +02:00
|
|
|
} else {
|
2021-04-20 08:54:44 +02:00
|
|
|
log.Infof("indexing originals in %s", txt.Quote(filepath.Join(conf.OriginalsPath(), subPath)))
|
2020-05-06 15:53:47 +02:00
|
|
|
}
|
2018-09-24 09:53:16 +02:00
|
|
|
|
2019-07-02 23:01:56 +02:00
|
|
|
if conf.ReadOnly() {
|
2021-01-24 17:46:18 +01:00
|
|
|
log.Infof("index: read-only mode enabled")
|
2019-07-02 23:01:56 +02:00
|
|
|
}
|
|
|
|
|
2021-08-13 20:04:59 +02:00
|
|
|
var indexed fs.Done
|
2020-05-08 07:56:14 +02:00
|
|
|
|
2021-08-13 20:04:59 +02:00
|
|
|
if w := service.Index(); w != nil {
|
|
|
|
opt := photoprism.IndexOptions{
|
|
|
|
Path: subPath,
|
2021-09-05 19:10:44 +02:00
|
|
|
Rescan: ctx.Bool("force"),
|
2021-08-13 20:04:59 +02:00
|
|
|
Convert: conf.Settings().Index.Convert && conf.SidecarWritable(),
|
|
|
|
Stack: true,
|
|
|
|
}
|
2020-05-08 07:56:14 +02:00
|
|
|
|
2021-08-13 20:04:59 +02:00
|
|
|
indexed = w.Start(opt)
|
2021-09-30 15:50:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if w := service.Purge(); w != nil {
|
2021-10-02 14:24:44 +02:00
|
|
|
purgeStart := time.Now()
|
2021-08-13 20:04:59 +02:00
|
|
|
opt := photoprism.PurgeOptions{
|
|
|
|
Path: subPath,
|
|
|
|
Ignore: indexed,
|
|
|
|
}
|
|
|
|
|
|
|
|
if files, photos, err := w.Start(opt); err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
} else if len(files) > 0 || len(photos) > 0 {
|
2021-10-02 14:24:44 +02:00
|
|
|
log.Infof("purge: removed %s and %s [%s]", english.Plural(len(files), "file", "files"), english.Plural(len(photos), "photo", "photos"), time.Since(purgeStart))
|
2021-08-13 20:04:59 +02:00
|
|
|
}
|
2021-09-30 15:50:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ctx.Bool("cleanup") {
|
2021-10-02 14:24:44 +02:00
|
|
|
cleanupStart := time.Now()
|
2021-08-13 20:04:59 +02:00
|
|
|
w := service.CleanUp()
|
2021-01-24 17:46:18 +01:00
|
|
|
|
|
|
|
opt := photoprism.CleanUpOptions{
|
|
|
|
Dry: false,
|
|
|
|
}
|
|
|
|
|
2021-08-13 20:04:59 +02:00
|
|
|
if thumbs, orphans, err := w.Start(opt); err != nil {
|
2021-01-24 17:46:18 +01:00
|
|
|
return err
|
|
|
|
} else {
|
2021-10-02 14:24:44 +02:00
|
|
|
log.Infof("cleanup: removed %s and %s [%s]", english.Plural(orphans, "index entry", "index entries"), english.Plural(thumbs, "thumbnail", "thumbnails"), time.Since(cleanupStart))
|
2021-01-24 17:46:18 +01:00
|
|
|
}
|
2020-05-08 07:56:14 +02:00
|
|
|
}
|
|
|
|
|
2019-05-14 16:04:17 +02:00
|
|
|
elapsed := time.Since(start)
|
|
|
|
|
2021-10-02 15:34:41 +02:00
|
|
|
log.Infof("indexed %s in %s", english.Plural(len(indexed), "file", "files"), elapsed)
|
2019-06-04 00:22:25 +02:00
|
|
|
|
2019-05-06 23:18:10 +02:00
|
|
|
conf.Shutdown()
|
2019-06-04 00:22:25 +02:00
|
|
|
|
2018-09-24 09:53:16 +02:00
|
|
|
return nil
|
|
|
|
}
|