forked from cloudreve/Cloudreve
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(thumb): generator settings and test button
- Loading branch information
Showing
5 changed files
with
114 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters