2018-09-24 09:53:16 +02:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
2019-06-03 22:58:15 +02:00
|
|
|
"context"
|
2020-01-31 15:29:06 +01:00
|
|
|
"errors"
|
|
|
|
"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"
|
2018-09-24 09:53:16 +02:00
|
|
|
"github.com/urfave/cli"
|
|
|
|
)
|
|
|
|
|
2020-02-18 23:42:51 +01:00
|
|
|
// ImportCommand is used to register the import cli command
|
2020-01-31 17:29:55 +01:00
|
|
|
var ImportCommand = cli.Command{
|
|
|
|
Name: "import",
|
2020-01-31 15:29:06 +01:00
|
|
|
Aliases: []string{"mv"},
|
2020-05-07 12:49:06 +02:00
|
|
|
Usage: "Moves files to originals folder, converts and indexes them as needed",
|
2020-01-31 17:29:55 +01:00
|
|
|
Action: importAction,
|
2018-09-24 09:53:16 +02:00
|
|
|
}
|
|
|
|
|
2020-02-18 23:42:51 +01:00
|
|
|
// importAction moves photos to originals path. Default import path is used if no path argument provided
|
2020-01-31 17:29:55 +01:00
|
|
|
func importAction(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-02-18 23:42:51 +01:00
|
|
|
// very if copy directory exist and is writable
|
2019-07-02 22:03:23 +02:00
|
|
|
if conf.ReadOnly() {
|
|
|
|
return config.ErrReadOnly
|
|
|
|
}
|
|
|
|
|
2019-05-06 23:18:10 +02:00
|
|
|
if err := conf.CreateDirectories(); err != nil {
|
2019-01-15 14:00:42 +01:00
|
|
|
return err
|
2018-09-24 09:53:16 +02:00
|
|
|
}
|
|
|
|
|
2019-06-03 22:58:15 +02:00
|
|
|
cctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
if err := conf.Init(cctx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-04-30 20:07:03 +02:00
|
|
|
conf.InitDb()
|
2018-09-24 09:53:16 +02:00
|
|
|
|
2020-02-18 23:42:51 +01:00
|
|
|
// get cli first argument
|
2020-01-31 15:29:06 +01:00
|
|
|
sourcePath := strings.TrimSpace(ctx.Args().First())
|
|
|
|
|
|
|
|
if sourcePath == "" {
|
|
|
|
sourcePath = conf.ImportPath()
|
|
|
|
} else {
|
|
|
|
abs, err := filepath.Abs(sourcePath)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
sourcePath = abs
|
|
|
|
}
|
|
|
|
|
|
|
|
if sourcePath == conf.OriginalsPath() {
|
2020-05-07 12:49:06 +02:00
|
|
|
return errors.New("import folder is identical with originals")
|
2020-01-31 15:29:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
log.Infof("moving media files from %s to %s", sourcePath, conf.OriginalsPath())
|
2018-09-24 09:53:16 +02:00
|
|
|
|
2020-04-05 22:26:53 +02:00
|
|
|
imp := service.Import()
|
2020-01-31 15:29:06 +01:00
|
|
|
opt := photoprism.ImportOptionsMove(sourcePath)
|
2018-09-24 09:53:16 +02:00
|
|
|
|
2020-01-31 15:29:06 +01:00
|
|
|
imp.Start(opt)
|
2018-09-24 09:53:16 +02:00
|
|
|
|
2019-05-14 16:04:17 +02:00
|
|
|
elapsed := time.Since(start)
|
|
|
|
|
2020-01-31 15:29:06 +01:00
|
|
|
log.Infof("import completed in %s", elapsed)
|
2019-06-03 22:58:15 +02:00
|
|
|
conf.Shutdown()
|
2018-09-24 09:53:16 +02:00
|
|
|
return nil
|
|
|
|
}
|