summaryrefslogtreecommitdiffstats
path: root/bridge/helper
diff options
context:
space:
mode:
Diffstat (limited to 'bridge/helper')
-rw-r--r--bridge/helper/helper.go66
-rw-r--r--bridge/helper/libtgsconverter.go34
-rw-r--r--bridge/helper/lottie_convert.go89
3 files changed, 123 insertions, 66 deletions
diff --git a/bridge/helper/helper.go b/bridge/helper/helper.go
index 1bdd8a40..581a2c43 100644
--- a/bridge/helper/helper.go
+++ b/bridge/helper/helper.go
@@ -5,10 +5,7 @@ import (
"fmt"
"image/png"
"io"
- "io/ioutil"
"net/http"
- "os"
- "os/exec"
"regexp"
"strings"
"time"
@@ -239,66 +236,3 @@ func ConvertWebPToPNG(data *[]byte) error {
*data = w.Bytes()
return nil
}
-
-// CanConvertTgsToX Checks whether the external command necessary for ConvertTgsToX works.
-func CanConvertTgsToX() error {
- // We depend on the fact that `lottie_convert.py --help` has exit status 0.
- // Hyrum's Law predicted this, and Murphy's Law predicts that this will break eventually.
- // However, there is no alternative like `lottie_convert.py --is-properly-installed`
- cmd := exec.Command("lottie_convert.py", "--help")
- return cmd.Run()
-}
-
-// ConvertTgsToWebP convert input data (which should be tgs format) to WebP format
-// This relies on an external command, which is ugly, but works.
-func ConvertTgsToX(data *[]byte, outputFormat string, logger *logrus.Entry) error {
- // lottie can't handle input from a pipe, so write to a temporary file:
- tmpInFile, err := ioutil.TempFile(os.TempDir(), "matterbridge-lottie-input-*.tgs")
- if err != nil {
- return err
- }
- tmpInFileName := tmpInFile.Name()
- defer func() {
- if removeErr := os.Remove(tmpInFileName); removeErr != nil {
- logger.Errorf("Could not delete temporary (input) file %s: %v", tmpInFileName, removeErr)
- }
- }()
- // lottie can handle writing to a pipe, but there is no way to do that platform-independently.
- // "/dev/stdout" won't work on Windows, and "-" upsets Cairo for some reason. So we need another file:
- tmpOutFile, err := ioutil.TempFile(os.TempDir(), "matterbridge-lottie-output-*.data")
- if err != nil {
- return err
- }
- tmpOutFileName := tmpOutFile.Name()
- defer func() {
- if removeErr := os.Remove(tmpOutFileName); removeErr != nil {
- logger.Errorf("Could not delete temporary (output) file %s: %v", tmpOutFileName, removeErr)
- }
- }()
-
- if _, writeErr := tmpInFile.Write(*data); writeErr != nil {
- return writeErr
- }
- // Must close before calling lottie to avoid data races:
- if closeErr := tmpInFile.Close(); closeErr != nil {
- return closeErr
- }
-
- // Call lottie to transform:
- cmd := exec.Command("lottie_convert.py", "--input-format", "lottie", "--output-format", outputFormat, tmpInFileName, tmpOutFileName)
- cmd.Stdout = nil
- cmd.Stderr = nil
- // NB: lottie writes progress into to stderr in all cases.
- _, stderr := cmd.Output()
- if stderr != nil {
- // 'stderr' already contains some parts of Stderr, because it was set to 'nil'.
- return stderr
- }
- dataContents, err := ioutil.ReadFile(tmpOutFileName)
- if err != nil {
- return err
- }
-
- *data = dataContents
- return nil
-}
diff --git a/bridge/helper/libtgsconverter.go b/bridge/helper/libtgsconverter.go
new file mode 100644
index 00000000..7181da90
--- /dev/null
+++ b/bridge/helper/libtgsconverter.go
@@ -0,0 +1,34 @@
+// +build cgo
+
+package helper
+
+import (
+ "fmt"
+ "github.com/Benau/tgsconverter/libtgsconverter"
+ "github.com/sirupsen/logrus"
+)
+
+func CanConvertTgsToX() error {
+ return nil
+}
+
+// ConvertTgsToX convert input data (which should be tgs format) to any format supported by libtgsconverter
+func ConvertTgsToX(data *[]byte, outputFormat string, logger *logrus.Entry) error {
+ options := libtgsconverter.NewConverterOptions()
+ options.SetExtension(outputFormat)
+ blob, err := libtgsconverter.ImportFromData(*data, options)
+ if err != nil {
+ return fmt.Errorf("failed to run libtgsconverter.ImportFromData: %s", err.Error())
+ }
+
+ *data = blob
+ return nil
+}
+
+func SupportsFormat(format string) bool {
+ return libtgsconverter.SupportsExtension(format)
+}
+
+func LottieBackend() string {
+ return "libtgsconverter"
+}
diff --git a/bridge/helper/lottie_convert.go b/bridge/helper/lottie_convert.go
new file mode 100644
index 00000000..c6ae0a7a
--- /dev/null
+++ b/bridge/helper/lottie_convert.go
@@ -0,0 +1,89 @@
+// +build !cgo
+
+package helper
+
+import (
+ "io/ioutil"
+ "os"
+ "os/exec"
+ "github.com/sirupsen/logrus"
+)
+
+// CanConvertTgsToX Checks whether the external command necessary for ConvertTgsToX works.
+func CanConvertTgsToX() error {
+ // We depend on the fact that `lottie_convert.py --help` has exit status 0.
+ // Hyrum's Law predicted this, and Murphy's Law predicts that this will break eventually.
+ // However, there is no alternative like `lottie_convert.py --is-properly-installed`
+ cmd := exec.Command("lottie_convert.py", "--help")
+ return cmd.Run()
+}
+
+// ConvertTgsToWebP convert input data (which should be tgs format) to WebP format
+// This relies on an external command, which is ugly, but works.
+func ConvertTgsToX(data *[]byte, outputFormat string, logger *logrus.Entry) error {
+ // lottie can't handle input from a pipe, so write to a temporary file:
+ tmpInFile, err := ioutil.TempFile(os.TempDir(), "matterbridge-lottie-input-*.tgs")
+ if err != nil {
+ return err
+ }
+ tmpInFileName := tmpInFile.Name()
+ defer func() {
+ if removeErr := os.Remove(tmpInFileName); removeErr != nil {
+ logger.Errorf("Could not delete temporary (input) file %s: %v", tmpInFileName, removeErr)
+ }
+ }()
+ // lottie can handle writing to a pipe, but there is no way to do that platform-independently.
+ // "/dev/stdout" won't work on Windows, and "-" upsets Cairo for some reason. So we need another file:
+ tmpOutFile, err := ioutil.TempFile(os.TempDir(), "matterbridge-lottie-output-*.data")
+ if err != nil {
+ return err
+ }
+ tmpOutFileName := tmpOutFile.Name()
+ defer func() {
+ if removeErr := os.Remove(tmpOutFileName); removeErr != nil {
+ logger.Errorf("Could not delete temporary (output) file %s: %v", tmpOutFileName, removeErr)
+ }
+ }()
+
+ if _, writeErr := tmpInFile.Write(*data); writeErr != nil {
+ return writeErr
+ }
+ // Must close before calling lottie to avoid data races:
+ if closeErr := tmpInFile.Close(); closeErr != nil {
+ return closeErr
+ }
+
+ // Call lottie to transform:
+ cmd := exec.Command("lottie_convert.py", "--input-format", "lottie", "--output-format", outputFormat, tmpInFileName, tmpOutFileName)
+ cmd.Stdout = nil
+ cmd.Stderr = nil
+ // NB: lottie writes progress into to stderr in all cases.
+ _, stderr := cmd.Output()
+ if stderr != nil {
+ // 'stderr' already contains some parts of Stderr, because it was set to 'nil'.
+ return stderr
+ }
+ dataContents, err := ioutil.ReadFile(tmpOutFileName)
+ if err != nil {
+ return err
+ }
+
+ *data = dataContents
+ return nil
+}
+
+func SupportsFormat(format string) bool {
+ switch format {
+ case "png":
+ fallthrough
+ case "webp":
+ return true
+ default:
+ return false
+ }
+ return false
+}
+
+func LottieBackend() string {
+ return "lottie_convert.py"
+}