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
|
|
|
|
2022-10-15 21:54:11 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/get"
|
2018-09-24 09:53:16 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/photoprism"
|
2022-04-15 09:42:07 +02:00
|
|
|
"github.com/photoprism/photoprism/pkg/clean"
|
2021-09-22 19:33:41 +02:00
|
|
|
"github.com/photoprism/photoprism/pkg/fs"
|
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",
|
2023-02-14 11:37:22 +01:00
|
|
|
ArgsUsage: "[subfolder]",
|
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",
|
2022-04-16 12:39:47 +02:00
|
|
|
Usage: "rescan all originals, including unchanged files",
|
|
|
|
},
|
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "archived, a",
|
2022-04-16 13:02:16 +02:00
|
|
|
Usage: "do not skip files belonging to archived photos",
|
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()
|
|
|
|
|
2022-10-04 12:27:40 +02:00
|
|
|
conf, err := InitConfig(ctx)
|
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
|
|
|
|
2022-10-04 12:27:40 +02:00
|
|
|
if 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()
|
2022-09-28 09:01:17 +02:00
|
|
|
defer conf.Shutdown()
|
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 == "" {
|
2022-04-15 09:42:07 +02:00
|
|
|
log.Infof("indexing originals in %s", clean.Log(conf.OriginalsPath()))
|
2020-05-06 15:53:47 +02:00
|
|
|
} else {
|
2022-04-15 09:42:07 +02:00
|
|
|
log.Infof("indexing originals in %s", clean.Log(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() {
|
2023-02-09 13:14:56 +01:00
|
|
|
log.Infof("config: enabled read-only mode")
|
2019-07-02 23:01:56 +02:00
|
|
|
}
|
|
|
|
|
2023-02-23 01:06:52 +01:00
|
|
|
var found fs.Done
|
2023-02-23 18:33:50 +01:00
|
|
|
var indexed int
|
2020-05-08 07:56:14 +02:00
|
|
|
|
2023-09-03 16:02:42 +02:00
|
|
|
// Update file index.
|
2022-10-15 21:54:11 +02:00
|
|
|
if w := get.Index(); w != nil {
|
2023-02-23 03:45:58 +01:00
|
|
|
indexStart := time.Now()
|
2022-04-02 18:04:02 +02:00
|
|
|
convert := conf.Settings().Index.Convert && conf.SidecarWritable()
|
2022-04-16 12:39:47 +02:00
|
|
|
opt := photoprism.NewIndexOptions(subPath, ctx.Bool("force"), convert, true, false, !ctx.Bool("archived"))
|
2020-05-08 07:56:14 +02:00
|
|
|
|
2023-02-23 18:33:50 +01:00
|
|
|
found, indexed = w.Start(opt)
|
2023-02-23 01:06:52 +01:00
|
|
|
|
2023-02-23 18:33:50 +01:00
|
|
|
log.Infof("index: updated %s [%s]", english.Plural(indexed, "file", "files"), time.Since(indexStart))
|
2021-09-30 15:50:10 +02:00
|
|
|
}
|
|
|
|
|
2023-09-03 16:02:42 +02:00
|
|
|
// Remove missing files from search results.
|
2022-10-15 21:54:11 +02:00
|
|
|
if w := get.Purge(); w != nil {
|
2023-09-03 16:02:42 +02:00
|
|
|
// Purge worker options.
|
2021-08-13 20:04:59 +02:00
|
|
|
opt := photoprism.PurgeOptions{
|
|
|
|
Path: subPath,
|
2023-02-23 01:06:52 +01:00
|
|
|
Ignore: found,
|
2023-02-23 18:33:50 +01:00
|
|
|
Force: ctx.Bool("force") || ctx.Bool("cleanup") || indexed > 0,
|
2021-08-13 20:04:59 +02:00
|
|
|
}
|
|
|
|
|
2023-09-03 16:02:42 +02:00
|
|
|
// Start purge.
|
|
|
|
purgeStart := time.Now()
|
2023-02-23 18:33:50 +01:00
|
|
|
if files, photos, updated, err := w.Start(opt); err != nil {
|
2021-08-13 20:04:59 +02:00
|
|
|
log.Error(err)
|
2023-02-23 18:33:50 +01:00
|
|
|
} else if updated > 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
|
|
|
}
|
|
|
|
|
2023-09-03 16:02:42 +02:00
|
|
|
// Delete orphaned index entries, sidecar files and thumbnails?
|
2021-09-30 15:50:10 +02:00
|
|
|
if ctx.Bool("cleanup") {
|
2023-09-03 16:02:42 +02:00
|
|
|
// Get cleanup worker instance.
|
2022-10-15 21:54:11 +02:00
|
|
|
w := get.CleanUp()
|
2021-01-24 17:46:18 +01:00
|
|
|
|
2023-09-03 16:02:42 +02:00
|
|
|
// Cleanup worker options.
|
2021-01-24 17:46:18 +01:00
|
|
|
opt := photoprism.CleanUpOptions{
|
|
|
|
Dry: false,
|
|
|
|
}
|
|
|
|
|
2023-09-03 16:02:42 +02:00
|
|
|
// Start index and cache cleanup.
|
|
|
|
cleanupStart := time.Now()
|
2022-07-22 19:18:42 +02:00
|
|
|
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 {
|
2023-09-03 16:02:42 +02:00
|
|
|
log.Infof("cleanup: deleted %s in total [%s]", english.Plural(total, "file", "files"), 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)
|
|
|
|
|
2023-02-23 01:06:52 +01:00
|
|
|
log.Infof("indexed %s in %s", english.Plural(len(found), "file", "files"), elapsed)
|
2019-06-04 00:22:25 +02:00
|
|
|
|
2018-09-24 09:53:16 +02:00
|
|
|
return nil
|
|
|
|
}
|