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"
|
|
|
|
|
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"
|
2020-05-06 15:53:47 +02:00
|
|
|
"github.com/photoprism/photoprism/pkg/txt"
|
2018-09-24 09:53:16 +02:00
|
|
|
"github.com/urfave/cli"
|
|
|
|
)
|
|
|
|
|
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{
|
|
|
|
Name: "index",
|
2020-05-07 12:49:06 +02:00
|
|
|
Usage: "Indexes media files in originals folder",
|
2020-01-22 09:57:49 +01:00
|
|
|
Flags: indexFlags,
|
2018-09-24 09:53:16 +02:00
|
|
|
Action: indexAction,
|
|
|
|
}
|
|
|
|
|
2020-01-22 09:57:49 +01:00
|
|
|
var indexFlags = []cli.Flag{
|
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "all, a",
|
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{
|
|
|
|
Name: "cleanup",
|
|
|
|
Usage: "removes orphaned thumbnails and index entries",
|
|
|
|
},
|
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
|
|
|
|
|
|
|
// get cli first argument
|
|
|
|
subPath := strings.TrimSpace(ctx.Args().First())
|
|
|
|
|
|
|
|
if subPath == "" {
|
|
|
|
log.Infof("indexing photos in %s", txt.Quote(conf.OriginalsPath()))
|
|
|
|
} else {
|
2020-05-07 12:33:09 +02:00
|
|
|
log.Infof("indexing originals folder %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
|
|
|
}
|
|
|
|
|
2020-04-05 22:26:53 +02:00
|
|
|
ind := service.Index()
|
2018-09-24 09:53:16 +02:00
|
|
|
|
2020-05-08 07:56:14 +02:00
|
|
|
indOpt := photoprism.IndexOptions{
|
2020-05-06 15:53:47 +02:00
|
|
|
Path: subPath,
|
|
|
|
Rescan: ctx.Bool("all"),
|
2020-06-07 14:33:07 +02:00
|
|
|
Convert: conf.Settings().Index.Convert && conf.SidecarWritable(),
|
2020-12-19 19:15:32 +01:00
|
|
|
Stack: true,
|
2020-01-22 09:57:49 +01:00
|
|
|
}
|
2018-09-24 09:53:16 +02:00
|
|
|
|
2020-05-08 07:56:14 +02:00
|
|
|
indexed := ind.Start(indOpt)
|
|
|
|
|
|
|
|
prg := service.Purge()
|
|
|
|
|
|
|
|
prgOpt := photoprism.PurgeOptions{
|
|
|
|
Path: subPath,
|
|
|
|
Ignore: indexed,
|
|
|
|
}
|
|
|
|
|
|
|
|
if files, photos, err := prg.Start(prgOpt); err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
} else if len(files) > 0 || len(photos) > 0 {
|
2021-01-24 17:46:18 +01:00
|
|
|
log.Infof("purge: removed %d files and %d photos", len(files), len(photos))
|
|
|
|
}
|
|
|
|
|
|
|
|
if ctx.Bool("cleanup") {
|
|
|
|
cleanUp := service.CleanUp()
|
|
|
|
|
|
|
|
opt := photoprism.CleanUpOptions{
|
|
|
|
Dry: false,
|
|
|
|
}
|
|
|
|
|
|
|
|
if thumbs, orphans, err := cleanUp.Start(opt); err != nil {
|
|
|
|
return err
|
|
|
|
} else {
|
|
|
|
log.Infof("cleanup: removed %d orphaned thumbnails and %d photos", thumbs, orphans)
|
|
|
|
}
|
2020-05-08 07:56:14 +02:00
|
|
|
}
|
|
|
|
|
2019-05-14 16:04:17 +02:00
|
|
|
elapsed := time.Since(start)
|
|
|
|
|
2020-05-08 07:56:14 +02:00
|
|
|
log.Infof("indexed %d files in %s", len(indexed), 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
|
|
|
|
}
|