CLI: Add file extension flag to "photoprism convert" command #3038

Signed-off-by: Michael Mayer <michael@photoprism.app>
This commit is contained in:
Michael Mayer 2022-12-28 21:14:46 +01:00
parent e6701323b2
commit 84cab27261
3 changed files with 15 additions and 4 deletions

View file

@ -19,6 +19,10 @@ var ConvertCommand = cli.Command{
Usage: "Converts files in other formats to JPEG and AVC as needed",
ArgsUsage: "[sub-folder]",
Flags: []cli.Flag{
cli.StringSliceFlag{
Name: "ext, e",
Usage: "only process files with the specified extensions, e.g. mp4",
},
cli.BoolFlag{
Name: "force, f",
Usage: "replace existing JPEG files in the sidecar folder",
@ -61,7 +65,7 @@ func convertAction(ctx *cli.Context) error {
w := get.Convert()
// Start file conversion.
if err := w.Start(convertPath, ctx.Bool("force")); err != nil {
if err := w.Start(convertPath, ctx.StringSlice("ext"), ctx.Bool("force")); err != nil {
log.Error(err)
}

View file

@ -13,6 +13,7 @@ import (
"github.com/photoprism/photoprism/internal/mutex"
"github.com/photoprism/photoprism/pkg/clean"
"github.com/photoprism/photoprism/pkg/fs"
"github.com/photoprism/photoprism/pkg/list"
)
// Convert represents a converter that can convert RAW/HEIF images to JPEG.
@ -37,7 +38,7 @@ func NewConvert(conf *config.Config) *Convert {
}
// Start converts all files in a directory to JPEG if possible.
func (c *Convert) Start(path string, force bool) (err error) {
func (c *Convert) Start(path string, ext []string, force bool) (err error) {
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("convert: %s (panic)\nstack: %s", r, debug.Stack())
@ -93,10 +94,16 @@ func (c *Convert) Start(path string, force bool) (err error) {
isDir, _ := info.IsDirOrSymlinkToDir()
isSymlink := info.IsSymlink()
// Skip file?
if skip, result := fs.SkipWalk(fileName, isDir, isSymlink, done, ignore); skip {
return result
}
// Process only files with specified extensions?
if list.Excludes(ext, fs.NormalizedExt(fileName)) {
return nil
}
f, err := NewMediaFile(fileName)
if err != nil || f.Empty() || !(f.IsRaw() || f.IsHEIC() || f.IsAVIF() || f.IsImageOther() || f.IsVideo()) {

View file

@ -29,7 +29,7 @@ func TestConvert_Start(t *testing.T) {
convert := NewConvert(conf)
err := convert.Start(conf.ImportPath(), false)
err := convert.Start(conf.ImportPath(), nil, false)
if err != nil {
t.Fatal(err)
@ -57,7 +57,7 @@ func TestConvert_Start(t *testing.T) {
_ = os.Remove(existingJpegFilename)
if err := convert.Start(conf.ImportPath(), false); err != nil {
if err := convert.Start(conf.ImportPath(), nil, false); err != nil {
t.Fatal(err)
}