refactoring: now detecting file type using filename from form data. Also, only one accepted content type

This commit is contained in:
Julien Neuhart
2018-04-08 21:49:02 +02:00
parent cdeef33ff7
commit bca02f6198
14 changed files with 119 additions and 400 deletions

View File

@@ -3,7 +3,6 @@ package process
import (
"bytes"
"fmt"
"os/exec"
"text/template"
"time"
@@ -37,7 +36,7 @@ func (e *impossibleConversionError) Error() string {
func Unconv(workingDir string, file *gfile.File) (string, error) {
cmdData := &conversionData{
FilePath: file.Path,
ResultFilePath: fmt.Sprintf("%s%s", gfile.MakeFilePath(workingDir), gfile.PDFExt),
ResultFilePath: gfile.MakeFilePath(workingDir, ".pdf"),
}
var (
@@ -81,7 +80,7 @@ type mergeData struct {
func Merge(workingDir string, filesPaths []string) (string, error) {
cmdData := &mergeData{
FilesPaths: filesPaths,
ResultFilePath: fmt.Sprintf("%s%s", gfile.MakeFilePath(workingDir), gfile.PDFExt),
ResultFilePath: gfile.MakeFilePath(workingDir, ".pdf"),
}
cmdTemplate := commandsConfig.Merge.Template
@@ -127,7 +126,6 @@ func run(command string, timeout int) error {
if err := cmd.Process.Kill(); err != nil {
return err
}
return &commandTimeoutError{}
case err := <-done:
if err != nil {

View File

@@ -1,6 +1,7 @@
package process
import (
"fmt"
"os"
"path/filepath"
"testing"
@@ -9,6 +10,18 @@ import (
gfile "github.com/thecodingmachine/gotenberg/app/converter/file"
)
func makeFile(workingDir string, fileName string) *gfile.File {
filePath := fmt.Sprintf("%s%s", "../../../_tests/", fileName)
absPath, _ := filepath.Abs(filePath)
r, _ := os.Open(absPath)
defer r.Close()
f, _ := gfile.NewFile(workingDir, r, fileName)
return f
}
func TestLoad(t *testing.T) {
path, _ := filepath.Abs("../../../_tests/configurations/gotenberg.yml")
c, _ := config.NewAppConfig(path)
@@ -28,29 +41,17 @@ func TestUnconv(t *testing.T) {
os.Mkdir(workingDir, 0666)
// case 1: uses an HTML file type.
path, _ = filepath.Abs("../../../_tests/file.html")
r, _ := os.Open(path)
defer r.Close()
f, _ := gfile.NewFile(workingDir, r)
if _, err := Unconv(workingDir, f); err != nil {
if _, err := Unconv(workingDir, makeFile(workingDir, "file.html")); err != nil {
t.Error("HTML conversion to PDF should have worked!")
}
// case 2: uses an Office file type.
path, _ = filepath.Abs("../../../_tests/file.docx")
r, _ = os.Open(path)
defer r.Close()
f, _ = gfile.NewFile(workingDir, r)
if _, err := Unconv(workingDir, f); err != nil {
if _, err := Unconv(workingDir, makeFile(workingDir, "file.docx")); err != nil {
t.Error("Office conversion to PDF should have worked!")
}
// case 3: uses a PDF file type.
path, _ = filepath.Abs("../../../_tests/file.pdf")
r, _ = os.Open(path)
defer r.Close()
f, _ = gfile.NewFile(workingDir, r)
if _, err := Unconv(workingDir, f); err == nil {
if _, err := Unconv(workingDir, makeFile(workingDir, "file.pdf")); err == nil {
t.Error("PDF conversion to PDF should not have worked!")
}
@@ -58,11 +59,7 @@ func TestUnconv(t *testing.T) {
path, _ = filepath.Abs("../../../_tests/configurations/timeout-gotenberg.yml")
c, _ = config.NewAppConfig(path)
Load(c.CommandsConfig)
path, _ = filepath.Abs("../../../_tests/file.docx")
r, _ = os.Open(path)
defer r.Close()
f, _ = gfile.NewFile(workingDir, r)
if _, err := Unconv(workingDir, f); err == nil {
if _, err := Unconv(workingDir, makeFile(workingDir, "file.docx")); err == nil {
t.Error("Office conversion to PDF should have reached timeout!")
}