feat(thumb): generator settings and test button

This commit is contained in:
Aaron Liu
2023-04-07 19:33:02 +08:00
parent ac536408c6
commit cf03206283
5 changed files with 114 additions and 2 deletions

74
pkg/thumb/tester.go Normal file
View File

@@ -0,0 +1,74 @@
package thumb
import (
"bytes"
"context"
"errors"
"fmt"
"os/exec"
"strings"
)
var (
ErrUnknownGenerator = errors.New("unknown generator type")
ErrUnknownOutput = errors.New("unknown output from generator")
)
// TestGenerator tests thumb generator by getting lib version
func TestGenerator(ctx context.Context, name, executable string) (string, error) {
switch name {
case "vips":
return testVipsGenerator(ctx, executable)
case "ffmpeg":
return testFfmpegGenerator(ctx, executable)
case "libreOffice":
return testLibreOfficeGenerator(ctx, executable)
default:
return "", ErrUnknownGenerator
}
}
func testVipsGenerator(ctx context.Context, executable string) (string, error) {
cmd := exec.CommandContext(ctx, executable, "--version")
var output bytes.Buffer
cmd.Stdout = &output
if err := cmd.Run(); err != nil {
return "", fmt.Errorf("failed to invoke vips executable: %w", err)
}
if !strings.Contains(output.String(), "vips") {
return "", ErrUnknownOutput
}
return output.String(), nil
}
func testFfmpegGenerator(ctx context.Context, executable string) (string, error) {
cmd := exec.CommandContext(ctx, executable, "-version")
var output bytes.Buffer
cmd.Stdout = &output
if err := cmd.Run(); err != nil {
return "", fmt.Errorf("failed to invoke ffmpeg executable: %w", err)
}
if !strings.Contains(output.String(), "ffmpeg") {
return "", ErrUnknownOutput
}
return output.String(), nil
}
func testLibreOfficeGenerator(ctx context.Context, executable string) (string, error) {
cmd := exec.CommandContext(ctx, executable, "--version")
var output bytes.Buffer
cmd.Stdout = &output
if err := cmd.Run(); err != nil {
return "", fmt.Errorf("failed to invoke libreoffice executable: %w", err)
}
if !strings.Contains(output.String(), "LibreOffice") {
return "", ErrUnknownOutput
}
return output.String(), nil
}