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"
|
2018-09-24 09:53:16 +02:00
|
|
|
)
|
|
|
|
|
2021-01-24 17:46:18 +01:00
|
|
|
// CopyCommand registers the copy cli command.
|
2020-01-31 15:29:06 +01:00
|
|
|
var CopyCommand = cli.Command{
|
2021-10-05 23:13:06 +02:00
|
|
|
Name: "cp",
|
|
|
|
Aliases: []string{"copy"},
|
|
|
|
Usage: "Copies media files to originals",
|
2022-01-05 11:40:44 +01:00
|
|
|
ArgsUsage: "[PATH]",
|
2021-10-05 23:13:06 +02:00
|
|
|
Action: copyAction,
|
2018-09-24 09:53:16 +02:00
|
|
|
}
|
|
|
|
|
2020-02-18 23:42:51 +01:00
|
|
|
// copyAction copies photos to originals path. Default import path is used if no path argument provided
|
2020-01-31 15:29:06 +01:00
|
|
|
func copyAction(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()
|
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
|
|
|
}
|
|
|
|
|
|
|
|
log.Infof("copying media files from %s to %s", sourcePath, conf.OriginalsPath())
|
2018-09-24 09:53:16 +02:00
|
|
|
|
2021-08-13 20:04:59 +02:00
|
|
|
w := service.Import()
|
2020-01-31 15:29:06 +01:00
|
|
|
opt := photoprism.ImportOptionsCopy(sourcePath)
|
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)
|
|
|
|
|
2019-06-03 22:58:15 +02:00
|
|
|
conf.Shutdown()
|
2021-10-02 15:34:41 +02:00
|
|
|
|
2018-09-24 09:53:16 +02:00
|
|
|
return nil
|
|
|
|
}
|