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"
|
2022-10-15 21:54:11 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/get"
|
2022-04-15 09:42:07 +02:00
|
|
|
"github.com/photoprism/photoprism/pkg/clean"
|
2018-09-24 09:53:16 +02:00
|
|
|
)
|
|
|
|
|
2023-02-14 11:37:22 +01:00
|
|
|
// ConvertCommand configures the command name, flags, and action.
|
2018-09-24 09:53:16 +02:00
|
|
|
var ConvertCommand = cli.Command{
|
2021-04-20 08:54:44 +02:00
|
|
|
Name: "convert",
|
2022-04-03 12:26:07 +02:00
|
|
|
Usage: "Converts files in other formats to JPEG and AVC as needed",
|
2023-02-14 11:37:22 +01:00
|
|
|
ArgsUsage: "[subfolder]",
|
2022-04-03 12:26:07 +02:00
|
|
|
Flags: []cli.Flag{
|
2022-12-28 21:14:46 +01:00
|
|
|
cli.StringSliceFlag{
|
|
|
|
Name: "ext, e",
|
|
|
|
Usage: "only process files with the specified extensions, e.g. mp4",
|
|
|
|
},
|
2022-04-03 12:26:07 +02:00
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "force, f",
|
|
|
|
Usage: "replace existing JPEG files in the sidecar folder",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
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()
|
|
|
|
|
2022-10-04 12:27:40 +02:00
|
|
|
conf, err := InitConfig(ctx)
|
2019-07-02 22:03:23 +02:00
|
|
|
|
2020-10-08 08:52:03 +02:00
|
|
|
_, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
2022-10-04 12:27:40 +02:00
|
|
|
if err != nil {
|
2019-01-15 14:00:42 +01:00
|
|
|
return err
|
2018-09-24 09:53:16 +02:00
|
|
|
}
|
|
|
|
|
2022-10-04 12:27:40 +02:00
|
|
|
if !conf.SidecarWritable() {
|
|
|
|
return config.ErrReadOnly
|
|
|
|
}
|
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
conf.RegisterDb()
|
|
|
|
defer conf.Shutdown()
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2022-04-15 09:42:07 +02:00
|
|
|
log.Infof("converting originals in %s", clean.Log(convertPath))
|
2018-09-24 09:53:16 +02:00
|
|
|
|
2022-10-15 21:54:11 +02:00
|
|
|
w := get.Convert()
|
2018-09-24 09:53:16 +02:00
|
|
|
|
2022-04-03 12:26:07 +02:00
|
|
|
// Start file conversion.
|
2022-12-28 21:14:46 +01:00
|
|
|
if err := w.Start(convertPath, ctx.StringSlice("ext"), ctx.Bool("force")); 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)
|
|
|
|
|
2022-10-13 22:11:02 +02:00
|
|
|
log.Infof("completed in %s", elapsed)
|
2018-09-24 09:53:16 +02:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|