github.com/cloudreve/Cloudreve/v3@v3.0.0-20240224133659-3edb00a6484c/pkg/thumb/tester.go (about) 1 package thumb 2 3 import ( 4 "bytes" 5 "context" 6 "errors" 7 "fmt" 8 "os/exec" 9 "strings" 10 ) 11 12 var ( 13 ErrUnknownGenerator = errors.New("unknown generator type") 14 ErrUnknownOutput = errors.New("unknown output from generator") 15 ) 16 17 // TestGenerator tests thumb generator by getting lib version 18 func TestGenerator(ctx context.Context, name, executable string) (string, error) { 19 switch name { 20 case "vips": 21 return testVipsGenerator(ctx, executable) 22 case "ffmpeg": 23 return testFfmpegGenerator(ctx, executable) 24 case "libreOffice": 25 return testLibreOfficeGenerator(ctx, executable) 26 default: 27 return "", ErrUnknownGenerator 28 } 29 } 30 31 func testVipsGenerator(ctx context.Context, executable string) (string, error) { 32 cmd := exec.CommandContext(ctx, executable, "--version") 33 var output bytes.Buffer 34 cmd.Stdout = &output 35 if err := cmd.Run(); err != nil { 36 return "", fmt.Errorf("failed to invoke vips executable: %w", err) 37 } 38 39 if !strings.Contains(output.String(), "vips") { 40 return "", ErrUnknownOutput 41 } 42 43 return output.String(), nil 44 } 45 46 func testFfmpegGenerator(ctx context.Context, executable string) (string, error) { 47 cmd := exec.CommandContext(ctx, executable, "-version") 48 var output bytes.Buffer 49 cmd.Stdout = &output 50 if err := cmd.Run(); err != nil { 51 return "", fmt.Errorf("failed to invoke ffmpeg executable: %w", err) 52 } 53 54 if !strings.Contains(output.String(), "ffmpeg") { 55 return "", ErrUnknownOutput 56 } 57 58 return output.String(), nil 59 } 60 61 func testLibreOfficeGenerator(ctx context.Context, executable string) (string, error) { 62 cmd := exec.CommandContext(ctx, executable, "--version") 63 var output bytes.Buffer 64 cmd.Stdout = &output 65 if err := cmd.Run(); err != nil { 66 return "", fmt.Errorf("failed to invoke libreoffice executable: %w", err) 67 } 68 69 if !strings.Contains(output.String(), "LibreOffice") { 70 return "", ErrUnknownOutput 71 } 72 73 return output.String(), nil 74 }