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
|
|
|
// CopyCommand is used to register the copy cli command
|
2020-01-31 15:29:06 +01:00
|
|
|
var CopyCommand = cli.Command{
|
2020-01-31 17:29:55 +01:00
|
|
|
Name: "copy",
|
2020-01-31 15:29:06 +01:00
|
|
|
Aliases: []string{"cp"},
|
2020-01-31 17:29:55 +01:00
|
|
|
Usage: "Copies files to originals path, converts and indexes them as needed",
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2019-05-06 23:18:10 +02:00
|
|
|
conf.MigrateDb()
|
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() {
|
|
|
|
return errors.New("import path is identical with originals path")
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Infof("copying 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.ImportOptionsCopy(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
|
|
|
|
}
|