2020-08-19 11:35:38 +02:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"regexp"
|
|
|
|
"time"
|
|
|
|
|
2022-10-15 21:54:11 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/migrate"
|
|
|
|
|
2020-08-19 11:35:38 +02:00
|
|
|
"github.com/manifoldco/promptui"
|
2021-11-28 13:52:27 +01:00
|
|
|
"github.com/sirupsen/logrus"
|
2021-09-22 19:33:41 +02:00
|
|
|
"github.com/urfave/cli"
|
|
|
|
|
2020-08-19 11:35:38 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/config"
|
|
|
|
"github.com/photoprism/photoprism/internal/entity"
|
|
|
|
)
|
|
|
|
|
2022-01-06 13:21:42 +01:00
|
|
|
// ResetCommand resets the index, clears the cache, and removes sidecar files after confirmation.
|
2020-08-19 11:35:38 +02:00
|
|
|
var ResetCommand = cli.Command{
|
2021-11-28 13:52:27 +01:00
|
|
|
Name: "reset",
|
2022-01-06 13:21:42 +01:00
|
|
|
Usage: "Resets the index, clears the cache, and removes sidecar files",
|
2021-11-28 13:52:27 +01:00
|
|
|
Flags: []cli.Flag{
|
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "index, i",
|
|
|
|
Usage: "reset index database only",
|
|
|
|
},
|
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "trace, t",
|
|
|
|
Usage: "show trace logs for debugging",
|
|
|
|
},
|
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "yes, y",
|
2022-01-06 14:33:49 +01:00
|
|
|
Usage: "assume \"yes\" and run non-interactively",
|
2021-11-28 13:52:27 +01:00
|
|
|
},
|
|
|
|
},
|
2020-08-19 11:35:38 +02:00
|
|
|
Action: resetAction,
|
|
|
|
}
|
|
|
|
|
2020-12-30 13:33:47 +01:00
|
|
|
// resetAction resets the index and removes sidecar files after confirmation.
|
2020-08-19 11:35:38 +02:00
|
|
|
func resetAction(ctx *cli.Context) error {
|
2022-10-04 12:27:40 +02:00
|
|
|
conf, err := InitConfig(ctx)
|
2022-09-28 09:01:17 +02:00
|
|
|
|
2020-10-08 08:52:03 +02:00
|
|
|
_, cancel := context.WithCancel(context.Background())
|
2020-08-19 11:35:38 +02:00
|
|
|
defer cancel()
|
2020-10-08 08:52:03 +02:00
|
|
|
|
2022-10-04 12:27:40 +02:00
|
|
|
if err != nil {
|
2020-08-19 11:35:38 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
conf.RegisterDb()
|
2021-11-28 13:52:27 +01:00
|
|
|
defer conf.Shutdown()
|
2020-08-19 11:35:38 +02:00
|
|
|
|
2021-11-28 13:52:27 +01:00
|
|
|
if !ctx.Bool("yes") {
|
|
|
|
log.Warnf("This will delete and recreate your index database after confirmation")
|
2020-08-19 15:36:31 +02:00
|
|
|
|
2021-11-28 13:52:27 +01:00
|
|
|
if !ctx.Bool("index") {
|
2021-12-02 14:30:50 +01:00
|
|
|
log.Warnf("You will be asked next if you also want to remove cache and sidecar files")
|
2020-12-30 13:33:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-28 13:52:27 +01:00
|
|
|
if ctx.Bool("trace") {
|
|
|
|
log.SetLevel(logrus.TraceLevel)
|
|
|
|
log.Infoln("reset: enabled trace mode")
|
2020-08-19 15:36:31 +02:00
|
|
|
}
|
2020-08-19 11:35:38 +02:00
|
|
|
|
2022-10-03 22:59:29 +02:00
|
|
|
confirmed := ctx.Bool("yes")
|
2020-12-30 13:33:47 +01:00
|
|
|
|
2021-11-28 13:52:27 +01:00
|
|
|
// Show prompt?
|
2022-10-03 22:59:29 +02:00
|
|
|
if !confirmed {
|
2021-11-28 13:52:27 +01:00
|
|
|
removeIndexPrompt := promptui.Prompt{
|
|
|
|
Label: "Delete and recreate index database?",
|
|
|
|
IsConfirm: true,
|
|
|
|
}
|
2020-12-30 13:33:47 +01:00
|
|
|
|
2021-11-28 13:52:27 +01:00
|
|
|
if _, err := removeIndexPrompt.Run(); err == nil {
|
2022-10-03 22:59:29 +02:00
|
|
|
confirmed = true
|
2021-11-28 13:52:27 +01:00
|
|
|
} else {
|
|
|
|
log.Infof("keeping index database")
|
2020-12-30 13:33:47 +01:00
|
|
|
}
|
2021-11-28 13:52:27 +01:00
|
|
|
}
|
2020-12-30 13:33:47 +01:00
|
|
|
|
2021-11-28 13:52:27 +01:00
|
|
|
// Reset index?
|
2022-10-03 22:59:29 +02:00
|
|
|
if confirmed {
|
2021-11-28 13:52:27 +01:00
|
|
|
resetIndexDb(conf)
|
|
|
|
}
|
2020-12-30 13:33:47 +01:00
|
|
|
|
2021-11-28 13:52:27 +01:00
|
|
|
// Reset index only?
|
2022-01-06 14:33:49 +01:00
|
|
|
if ctx.Bool("index") || ctx.Bool("yes") {
|
2021-11-28 13:52:27 +01:00
|
|
|
return nil
|
|
|
|
}
|
2020-12-30 13:33:47 +01:00
|
|
|
|
2021-12-02 14:30:50 +01:00
|
|
|
// Clear cache.
|
|
|
|
removeCachePrompt := promptui.Prompt{
|
|
|
|
Label: "Clear cache incl thumbnails?",
|
|
|
|
IsConfirm: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := removeCachePrompt.Run(); err == nil {
|
|
|
|
resetCache(conf)
|
|
|
|
} else {
|
|
|
|
log.Infof("keeping cache files")
|
|
|
|
}
|
|
|
|
|
|
|
|
// *.json sidecar files.
|
2021-11-28 13:52:27 +01:00
|
|
|
removeSidecarJsonPrompt := promptui.Prompt{
|
2021-12-02 14:30:50 +01:00
|
|
|
Label: "Delete all *.json sidecar files?",
|
2021-11-28 13:52:27 +01:00
|
|
|
IsConfirm: true,
|
|
|
|
}
|
2020-12-30 13:33:47 +01:00
|
|
|
|
2021-11-28 13:52:27 +01:00
|
|
|
if _, err := removeSidecarJsonPrompt.Run(); err == nil {
|
|
|
|
resetSidecarJson(conf)
|
2020-12-30 13:33:47 +01:00
|
|
|
} else {
|
2021-12-02 14:30:50 +01:00
|
|
|
log.Infof("keeping *.json sidecar files")
|
2020-12-30 13:33:47 +01:00
|
|
|
}
|
2020-08-19 11:35:38 +02:00
|
|
|
|
2022-01-06 13:21:42 +01:00
|
|
|
// *.yml metadata files.
|
2021-08-22 16:14:34 +02:00
|
|
|
removeSidecarYamlPrompt := promptui.Prompt{
|
2022-01-06 13:21:42 +01:00
|
|
|
Label: "Delete all *.yml metadata files?",
|
2020-08-19 11:35:38 +02:00
|
|
|
IsConfirm: true,
|
|
|
|
}
|
|
|
|
|
2021-08-22 16:14:34 +02:00
|
|
|
if _, err := removeSidecarYamlPrompt.Run(); err == nil {
|
2021-11-28 13:52:27 +01:00
|
|
|
resetSidecarYaml(conf)
|
2020-12-21 14:05:02 +01:00
|
|
|
} else {
|
2022-01-06 13:21:42 +01:00
|
|
|
log.Infof("keeping *.yml metadata files")
|
2020-12-21 14:05:02 +01:00
|
|
|
}
|
|
|
|
|
2022-01-06 13:21:42 +01:00
|
|
|
// *.yml album files.
|
2021-08-22 16:14:34 +02:00
|
|
|
removeAlbumYamlPrompt := promptui.Prompt{
|
2022-01-06 13:21:42 +01:00
|
|
|
Label: "Delete all *.yml album files?",
|
2020-12-21 14:05:02 +01:00
|
|
|
IsConfirm: true,
|
|
|
|
}
|
|
|
|
|
2021-08-22 16:14:34 +02:00
|
|
|
if _, err := removeAlbumYamlPrompt.Run(); err == nil {
|
2020-12-21 14:05:02 +01:00
|
|
|
start := time.Now()
|
|
|
|
|
|
|
|
matches, err := filepath.Glob(regexp.QuoteMeta(conf.AlbumsPath()) + "/**/*.yml")
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(matches) > 0 {
|
2022-01-06 13:21:42 +01:00
|
|
|
log.Infof("%d *.yml album files will be removed", len(matches))
|
2020-12-21 14:05:02 +01:00
|
|
|
|
|
|
|
for _, name := range matches {
|
|
|
|
if err := os.Remove(name); err != nil {
|
|
|
|
fmt.Print("E")
|
|
|
|
} else {
|
|
|
|
fmt.Print(".")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println("")
|
|
|
|
|
2022-01-06 13:21:42 +01:00
|
|
|
log.Infof("removed all *.yml album files [%s]", time.Since(start))
|
2020-08-19 11:35:38 +02:00
|
|
|
} else {
|
2022-01-06 13:21:42 +01:00
|
|
|
log.Infof("found no *.yml album files")
|
2020-08-19 11:35:38 +02:00
|
|
|
}
|
|
|
|
} else {
|
2022-01-06 13:21:42 +01:00
|
|
|
log.Infof("keeping *.yml album files")
|
2020-08-19 11:35:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2021-11-28 13:52:27 +01:00
|
|
|
|
|
|
|
// resetIndexDb resets the index database schema.
|
2022-09-02 21:30:50 +02:00
|
|
|
func resetIndexDb(c *config.Config) {
|
2021-11-28 13:52:27 +01:00
|
|
|
start := time.Now()
|
|
|
|
|
|
|
|
tables := entity.Entities
|
|
|
|
|
|
|
|
log.Infoln("dropping existing tables")
|
2022-09-02 21:30:50 +02:00
|
|
|
tables.Drop(c.Db())
|
2021-11-28 13:52:27 +01:00
|
|
|
|
|
|
|
log.Infoln("restoring default schema")
|
2022-10-15 21:54:11 +02:00
|
|
|
entity.InitDb(migrate.Opt(false, nil))
|
2021-11-28 13:52:27 +01:00
|
|
|
|
2022-09-02 21:30:50 +02:00
|
|
|
// Reset admin account?
|
|
|
|
if c.AdminPassword() == "" {
|
|
|
|
log.Warnf("password required to reset admin account")
|
2022-09-02 22:55:57 +02:00
|
|
|
} else {
|
|
|
|
entity.Admin.InitAccount(c.AdminUser(), c.AdminPassword())
|
2021-11-28 13:52:27 +01:00
|
|
|
}
|
|
|
|
|
2022-10-13 22:11:02 +02:00
|
|
|
log.Infof("completed in %s", time.Since(start))
|
2021-11-28 13:52:27 +01:00
|
|
|
}
|
|
|
|
|
2021-12-02 14:30:50 +01:00
|
|
|
// resetCache removes all cache files and folders.
|
2022-09-02 21:30:50 +02:00
|
|
|
func resetCache(c *config.Config) {
|
2021-12-02 14:30:50 +01:00
|
|
|
start := time.Now()
|
|
|
|
|
2022-09-02 21:30:50 +02:00
|
|
|
matches, err := filepath.Glob(regexp.QuoteMeta(c.CachePath()) + "/**")
|
2021-12-02 14:30:50 +01:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("reset: %s (find cache files)", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(matches) > 0 {
|
|
|
|
log.Infof("clearing cache")
|
|
|
|
|
|
|
|
for _, name := range matches {
|
|
|
|
if err := os.RemoveAll(name); err != nil {
|
|
|
|
fmt.Print("E")
|
|
|
|
} else {
|
|
|
|
fmt.Print(".")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println("")
|
|
|
|
|
|
|
|
log.Infof("removed cache files [%s]", time.Since(start))
|
|
|
|
} else {
|
|
|
|
log.Infof("found no cache files")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// resetSidecarJson removes generated *.json sidecar files.
|
2022-09-02 21:30:50 +02:00
|
|
|
func resetSidecarJson(c *config.Config) {
|
2021-11-28 13:52:27 +01:00
|
|
|
start := time.Now()
|
|
|
|
|
2022-09-02 21:30:50 +02:00
|
|
|
matches, err := filepath.Glob(regexp.QuoteMeta(c.SidecarPath()) + "/**/*.json")
|
2021-11-28 13:52:27 +01:00
|
|
|
|
|
|
|
if err != nil {
|
2021-12-02 14:30:50 +01:00
|
|
|
log.Errorf("reset: %s (find *.json sidecar files)", err)
|
2021-11-28 13:52:27 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(matches) > 0 {
|
2021-12-02 14:30:50 +01:00
|
|
|
log.Infof("removing %d *.json sidecar files", len(matches))
|
2021-11-28 13:52:27 +01:00
|
|
|
|
|
|
|
for _, name := range matches {
|
2022-09-02 21:30:50 +02:00
|
|
|
if err = os.Remove(name); err != nil {
|
2021-11-28 13:52:27 +01:00
|
|
|
fmt.Print("E")
|
|
|
|
} else {
|
|
|
|
fmt.Print(".")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println("")
|
|
|
|
|
2021-12-02 14:30:50 +01:00
|
|
|
log.Infof("removed *.json sidecar files [%s]", time.Since(start))
|
2021-11-28 13:52:27 +01:00
|
|
|
} else {
|
2021-12-02 14:30:50 +01:00
|
|
|
log.Infof("found no *.json sidecar files")
|
2021-11-28 13:52:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-02 14:30:50 +01:00
|
|
|
// resetSidecarYaml removes generated *.yml files.
|
2022-09-02 21:30:50 +02:00
|
|
|
func resetSidecarYaml(c *config.Config) {
|
2021-11-28 13:52:27 +01:00
|
|
|
start := time.Now()
|
|
|
|
|
2022-09-02 21:30:50 +02:00
|
|
|
matches, err := filepath.Glob(regexp.QuoteMeta(c.SidecarPath()) + "/**/*.yml")
|
2021-11-28 13:52:27 +01:00
|
|
|
|
|
|
|
if err != nil {
|
2022-01-06 13:21:42 +01:00
|
|
|
log.Errorf("reset: %s (find *.yml metadata files)", err)
|
2021-11-28 13:52:27 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(matches) > 0 {
|
2022-01-06 13:21:42 +01:00
|
|
|
log.Infof("%d *.yml metadata files will be removed", len(matches))
|
2021-11-28 13:52:27 +01:00
|
|
|
|
|
|
|
for _, name := range matches {
|
|
|
|
if err := os.Remove(name); err != nil {
|
|
|
|
fmt.Print("E")
|
|
|
|
} else {
|
|
|
|
fmt.Print(".")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println("")
|
|
|
|
|
2022-01-06 13:21:42 +01:00
|
|
|
log.Infof("removed all *.yml metadata files [%s]", time.Since(start))
|
2021-11-28 13:52:27 +01:00
|
|
|
} else {
|
2022-01-06 13:21:42 +01:00
|
|
|
log.Infof("found no *.yml metadata files")
|
2021-11-28 13:52:27 +01:00
|
|
|
}
|
|
|
|
}
|