2018-09-24 09:53:16 +02:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
2020-10-08 08:52:03 +02:00
|
|
|
"context"
|
2021-04-20 08:54:44 +02:00
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
2019-05-14 16:04:17 +02:00
|
|
|
"time"
|
|
|
|
|
2021-09-22 19:33:41 +02:00
|
|
|
"github.com/urfave/cli"
|
2021-04-26 12:37:52 +02:00
|
|
|
|
2019-05-06 23:18:10 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/config"
|
2020-04-05 22:26:53 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/service"
|
2021-09-22 19:33:41 +02:00
|
|
|
"github.com/photoprism/photoprism/pkg/txt"
|
2018-09-24 09:53:16 +02:00
|
|
|
)
|
|
|
|
|
2021-01-24 17:46:18 +01:00
|
|
|
// ConvertCommand registers the convert cli command.
|
2018-09-24 09:53:16 +02:00
|
|
|
var ConvertCommand = cli.Command{
|
2021-04-20 08:54:44 +02:00
|
|
|
Name: "convert",
|
2021-10-05 23:13:06 +02:00
|
|
|
Usage: "Converts media files in other formats to JPEG / AVC",
|
|
|
|
ArgsUsage: "[path]",
|
2021-04-20 08:54:44 +02:00
|
|
|
Action: convertAction,
|
2018-09-24 09:53:16 +02:00
|
|
|
}
|
|
|
|
|
2021-02-11 21:37:44 +01:00
|
|
|
// convertAction converts originals in other formats to JPEG and AVC sidecar files.
|
2018-11-17 06:21:39 +01:00
|
|
|
func convertAction(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-06-07 14:33:07 +02:00
|
|
|
if !conf.SidecarWritable() {
|
2019-07-02 22:03:23 +02:00
|
|
|
return config.ErrReadOnly
|
|
|
|
}
|
|
|
|
|
2020-10-08 08:52:03 +02:00
|
|
|
_, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
if err := conf.Init(); err != nil {
|
2019-01-15 14:00:42 +01:00
|
|
|
return err
|
2018-09-24 09:53:16 +02:00
|
|
|
}
|
|
|
|
|
2021-04-20 08:54:44 +02:00
|
|
|
convertPath := conf.OriginalsPath()
|
|
|
|
|
|
|
|
// Use first argument to limit scope if set.
|
|
|
|
subPath := strings.TrimSpace(ctx.Args().First())
|
|
|
|
|
|
|
|
if subPath != "" {
|
|
|
|
convertPath = filepath.Join(convertPath, subPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Infof("converting originals in %s", txt.Quote(convertPath))
|
2018-09-24 09:53:16 +02:00
|
|
|
|
2021-08-13 20:04:59 +02:00
|
|
|
w := service.Convert()
|
2018-09-24 09:53:16 +02:00
|
|
|
|
2021-08-13 20:04:59 +02:00
|
|
|
if err := w.Start(convertPath); err != nil {
|
2020-04-05 22:26:53 +02:00
|
|
|
log.Error(err)
|
|
|
|
}
|
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("converting completed in %s", elapsed)
|
2018-09-24 09:53:16 +02:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|