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"
|
|
|
|
|
2021-09-22 19:33:41 +02:00
|
|
|
"github.com/urfave/cli"
|
|
|
|
|
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"
|
2022-09-30 00:42:19 +02:00
|
|
|
"github.com/photoprism/photoprism/pkg/clean"
|
2018-09-24 09:53:16 +02:00
|
|
|
)
|
|
|
|
|
2021-01-24 17:46:18 +01:00
|
|
|
// ImportCommand registers the import cli command.
|
2020-01-31 17:29:55 +01:00
|
|
|
var ImportCommand = cli.Command{
|
2021-10-05 23:13:06 +02:00
|
|
|
Name: "mv",
|
|
|
|
Aliases: []string{"import"},
|
|
|
|
Usage: "Moves media files to originals",
|
2022-10-03 22:59:29 +02:00
|
|
|
ArgsUsage: "[source]",
|
2022-09-30 00:42:19 +02:00
|
|
|
Flags: []cli.Flag{
|
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "dest, d",
|
|
|
|
Usage: "relative originals `PATH` to which the files should be imported",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-04-30 20:07:03 +02:00
|
|
|
conf.InitDb()
|
2022-09-28 09:01:17 +02:00
|
|
|
defer conf.Shutdown()
|
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() {
|
2021-10-05 22:33:29 +02:00
|
|
|
return errors.New("import path is identical with originals")
|
2020-01-31 15:29:06 +01:00
|
|
|
}
|
|
|
|
|
2022-09-30 00:42:19 +02:00
|
|
|
var destFolder string
|
|
|
|
if ctx.IsSet("dest") {
|
|
|
|
destFolder = clean.UserPath(ctx.String("dest"))
|
|
|
|
} else {
|
|
|
|
destFolder = conf.ImportDest()
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Infof("moving media files from %s to %s", sourcePath, filepath.Join(conf.OriginalsPath(), destFolder))
|
2018-09-24 09:53:16 +02:00
|
|
|
|
2021-08-13 20:04:59 +02:00
|
|
|
w := service.Import()
|
2022-09-30 00:42:19 +02:00
|
|
|
opt := photoprism.ImportOptionsMove(sourcePath, destFolder)
|
2018-09-24 09:53:16 +02:00
|
|
|
|
2021-08-13 20:04:59 +02:00
|
|
|
w.Start(opt)
|
2018-09-24 09:53:16 +02:00
|
|
|
|
2019-05-14 16:04:17 +02:00
|
|
|
elapsed := time.Since(start)
|
|
|
|
|
2021-10-02 15:34:41 +02:00
|
|
|
log.Infof("import completed in %s", elapsed)
|
|
|
|
|
2018-09-24 09:53:16 +02:00
|
|
|
return nil
|
|
|
|
}
|