adding filename form field + updating echo package to v4 (#57)

This commit is contained in:
Julien Neuhart
2019-03-26 17:35:38 +01:00
committed by GitHub
parent 8a4a043e9f
commit 871b52a288
21 changed files with 140 additions and 47 deletions

View File

@@ -8,8 +8,8 @@ import (
"os/signal"
"time"
"github.com/labstack/echo"
"github.com/labstack/echo/middleware"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"github.com/thecodingmachine/gotenberg/internal/pkg/notify"
"github.com/thecodingmachine/gotenberg/internal/pkg/pm2"
"github.com/thecodingmachine/gotenberg/internal/pkg/printer"
@@ -93,14 +93,17 @@ func print(c echo.Context, p printer.Printer, r *resource) error {
}
filename := fmt.Sprintf("%s.pdf", baseFilename)
fpath := fmt.Sprintf("%s/%s", r.dirPath, filename)
// if no webhook URL given, run conversion
// and directly return the resulting PDF file
// or an error.
if r.webhookURL() == "" {
defer r.removeAll()
// if no webhook URL given, run conversion
// and directly return the resulting PDF file
// or an error.
if err := p.Print(fpath); err != nil {
return err
}
if r.filename() != "" {
filename = r.filename()
}
return c.Attachment(fpath, filename)
}
// as a webhook URL has been given, we

View File

@@ -5,7 +5,7 @@ import (
"net/http/httptest"
"testing"
"github.com/labstack/echo"
"github.com/labstack/echo/v4"
"github.com/stretchr/testify/assert"
)

View File

@@ -1,7 +1,7 @@
package api
import (
"github.com/labstack/echo"
"github.com/labstack/echo/v4"
"github.com/thecodingmachine/gotenberg/internal/pkg/printer"
)

View File

@@ -5,7 +5,7 @@ import (
"net/http/httptest"
"testing"
"github.com/labstack/echo"
"github.com/labstack/echo/v4"
"github.com/stretchr/testify/assert"
"github.com/thecodingmachine/gotenberg/test"
)

View File

@@ -1,7 +1,7 @@
package api
import (
"github.com/labstack/echo"
"github.com/labstack/echo/v4"
"github.com/thecodingmachine/gotenberg/internal/pkg/printer"
)

View File

@@ -5,7 +5,7 @@ import (
"net/http/httptest"
"testing"
"github.com/labstack/echo"
"github.com/labstack/echo/v4"
"github.com/stretchr/testify/assert"
"github.com/thecodingmachine/gotenberg/test"
)

View File

@@ -6,7 +6,7 @@ import (
"net/http"
"os"
"github.com/labstack/echo"
"github.com/labstack/echo/v4"
"github.com/thecodingmachine/gotenberg/internal/pkg/printer"
"github.com/thecodingmachine/gotenberg/internal/pkg/rand"
)
@@ -29,14 +29,17 @@ func merge(c echo.Context) error {
}
filename := fmt.Sprintf("%s.pdf", baseFilename)
fpath := fmt.Sprintf("%s/%s", r.dirPath, filename)
// if no webhook URL given, run merge
// and directly return the resulting PDF file
// or an error.
if r.webhookURL() == "" {
defer r.removeAll()
// if no webhook URL given, run merge
// and directly return the resulting PDF file
// or an error.
if err := printer.Merge(fpaths, fpath); err != nil {
return err
}
if r.filename() != "" {
filename = r.filename()
}
return c.Attachment(fpath, filename)
}
// as a webhook URL has been given, we

View File

@@ -5,7 +5,7 @@ import (
"net/http/httptest"
"testing"
"github.com/labstack/echo"
"github.com/labstack/echo/v4"
"github.com/stretchr/testify/assert"
"github.com/thecodingmachine/gotenberg/test"
)

View File

@@ -3,7 +3,7 @@ package api
import (
"errors"
"github.com/labstack/echo"
"github.com/labstack/echo/v4"
"github.com/thecodingmachine/gotenberg/internal/pkg/printer"
)

View File

@@ -5,7 +5,7 @@ import (
"net/http/httptest"
"testing"
"github.com/labstack/echo"
"github.com/labstack/echo/v4"
"github.com/stretchr/testify/assert"
"github.com/thecodingmachine/gotenberg/test"
)

View File

@@ -8,7 +8,7 @@ import (
"path/filepath"
"strconv"
"github.com/labstack/echo"
"github.com/labstack/echo/v4"
"github.com/thecodingmachine/gotenberg/internal/pkg/rand"
)
@@ -23,6 +23,7 @@ const (
marginRight string = "marginRight"
landscape string = "landscape"
webFontsTimeout string = "webFontsTimeout"
filename string = "filename"
)
// resource facilitates storing and accessing
@@ -33,17 +34,6 @@ type resource struct {
}
func newResource(c echo.Context) (*resource, error) {
v := make(map[string]string)
v[remoteURL] = c.FormValue(remoteURL)
v[webhookURL] = c.FormValue(webhookURL)
v[paperWidth] = c.FormValue(paperWidth)
v[paperHeight] = c.FormValue(paperHeight)
v[marginTop] = c.FormValue(marginTop)
v[marginBottom] = c.FormValue(marginBottom)
v[marginLeft] = c.FormValue(marginLeft)
v[marginRight] = c.FormValue(marginRight)
v[landscape] = c.FormValue(landscape)
v[webFontsTimeout] = c.FormValue(webFontsTimeout)
dirPath, err := rand.Get()
if err != nil {
return nil, err
@@ -51,7 +41,7 @@ func newResource(c echo.Context) (*resource, error) {
if err := os.MkdirAll(dirPath, 0755); err != nil {
return nil, fmt.Errorf("%s: making directory: %v", dirPath, err)
}
r := &resource{values: v, dirPath: dirPath}
r := &resource{values: values(c), dirPath: dirPath}
form, err := c.MultipartForm()
if err != nil {
return r, fmt.Errorf("getting multipart form: %v", err)
@@ -71,6 +61,22 @@ func newResource(c echo.Context) (*resource, error) {
return r, nil
}
func values(c echo.Context) map[string]string {
v := make(map[string]string)
v[remoteURL] = c.FormValue(remoteURL)
v[webhookURL] = c.FormValue(webhookURL)
v[paperWidth] = c.FormValue(paperWidth)
v[paperHeight] = c.FormValue(paperHeight)
v[marginTop] = c.FormValue(marginTop)
v[marginBottom] = c.FormValue(marginBottom)
v[marginLeft] = c.FormValue(marginLeft)
v[marginRight] = c.FormValue(marginRight)
v[landscape] = c.FormValue(landscape)
v[webFontsTimeout] = c.FormValue(webFontsTimeout)
v[filename] = c.FormValue(filename)
return v
}
func (r *resource) writeFile(filename string, in io.Reader) error {
fpath := fmt.Sprintf("%s/%s", r.dirPath, filename)
out, err := os.Create(fpath)
@@ -205,4 +211,5 @@ func (r *resource) webFontsTimeout() (int64, error) {
}
func (r *resource) webhookURL() string { return r.values[webhookURL] }
func (r *resource) filename() string { return r.values[filename] }
func (r *resource) removeAll() error { return os.RemoveAll(r.dirPath) }

View File

@@ -1,7 +1,7 @@
package api
import (
"github.com/labstack/echo"
"github.com/labstack/echo/v4"
"github.com/thecodingmachine/gotenberg/internal/pkg/printer"
)

View File

@@ -5,7 +5,7 @@ import (
"net/http/httptest"
"testing"
"github.com/labstack/echo"
"github.com/labstack/echo/v4"
"github.com/stretchr/testify/assert"
"github.com/thecodingmachine/gotenberg/test"
)