wip refactoring: better logging and error systems

This commit is contained in:
Julien Neuhart
2019-07-07 17:45:07 +02:00
parent a4aa8dafac
commit c8f7ea934c
36 changed files with 1346 additions and 1096 deletions

View File

@@ -0,0 +1,137 @@
package context
import (
"net/http"
"strconv"
"time"
"github.com/labstack/echo/v4"
"github.com/thecodingmachine/gotenberg/internal/app/api/pkg/resource"
"github.com/thecodingmachine/gotenberg/internal/pkg/config"
"github.com/thecodingmachine/gotenberg/internal/pkg/logger"
"github.com/thecodingmachine/gotenberg/internal/pkg/standarderror"
)
// Context extends the default echo.Context.
type Context struct {
echo.Context
logger *logger.Logger
config *config.Config
resource *resource.Resource
startTime time.Time
}
// New creates a new context.
func New(c echo.Context, logger *logger.Logger, config *config.Config) *Context {
// TODO timeout context?
return &Context{
c,
logger,
config,
nil,
time.Now(),
}
}
// MustCastFromEchoContext cast an echo.Context to our custom
// context. If something goes wrong, panic.
func MustCastFromEchoContext(c echo.Context) *Context {
ctx, ok := c.(*Context)
if !ok {
panic("unable to cast an echo.Context to a custom context")
}
return ctx
}
// StandardLogger returns the custom logger.
// This method should be used instead of the
// default Logger() method coming from
// the echo.Context!
func (ctx *Context) StandardLogger() *logger.Logger {
return ctx.logger
}
// Resource returns the associated resource
// to the context.
func (ctx *Context) Resource() *resource.Resource {
return ctx.resource
}
// WithResource adds a resource to the context.
func (ctx *Context) WithResource(resourceDirPath string) error {
const op = "context.WithResource"
r, err := resource.New(ctx, ctx.logger, ctx.config, resourceDirPath)
ctx.resource = r
if err != nil {
return &standarderror.Error{
Op: op,
Err: err,
}
}
return nil
}
// LogRequestResult logs the result of a request.
// This method should only be used by a middleware!
func (ctx *Context) LogRequestResult(err error, isDebug bool) error {
req := ctx.Request()
resp := ctx.Response()
stopTime := time.Now()
fields := map[string]interface{}{
"time_rfc3339": timeRFC3339(), // FIXME required?
"remote_ip": ctx.RealIP(),
"host": req.Host,
"uri": req.RequestURI,
"method": req.Method,
"path": path(req),
"referer": req.Referer(),
"user_agent": req.UserAgent(),
"status": resp.Status,
"latency": lantency(ctx.startTime, stopTime),
"latency_human": latencyHuman(ctx.startTime, stopTime),
"bytes_in": bytesIn(req),
"bytes_out": bytesOut(resp),
}
if err != nil {
ctx.logger.WithFields(fields).Error("request failed")
return err
}
if isDebug {
ctx.logger.WithFields(fields).Debug("request handled")
return nil
}
ctx.logger.WithFields(fields).Info("request handled")
return nil
}
func timeRFC3339() string {
return time.Now().Format(time.RFC3339)
}
func path(r *http.Request) string {
path := r.URL.Path
if path == "" {
path = "/"
}
return path
}
func lantency(startTime time.Time, stopTime time.Time) string {
return strconv.FormatInt(int64(stopTime.Sub(startTime)), 10)
}
func latencyHuman(startTime time.Time, stopTime time.Time) string {
return stopTime.Sub(startTime).String()
}
func bytesIn(r *http.Request) string {
bytesIn := r.Header.Get(echo.HeaderContentLength)
if bytesIn == "" {
bytesIn = "0"
}
return bytesIn
}
func bytesOut(r *echo.Response) string {
return strconv.FormatInt(r.Size, 10)
}

View File

@@ -0,0 +1,3 @@
// Package context helps extending
// the default echo.Context.
package context

View File

@@ -0,0 +1,3 @@
// Package handler contains all
// the endpoint methods of the API.
package handler

View File

@@ -0,0 +1,151 @@
package handler
import (
"fmt"
"net/http"
"os"
"github.com/thecodingmachine/gotenberg/internal/app/api/pkg/context"
"github.com/thecodingmachine/gotenberg/internal/app/api/pkg/resource"
"github.com/thecodingmachine/gotenberg/internal/pkg/printer"
"github.com/thecodingmachine/gotenberg/internal/pkg/random"
"github.com/thecodingmachine/gotenberg/internal/pkg/standarderror"
)
const (
// PingEndpoint is the route for healthcheck.
PingEndpoint = "/ping"
// MergeEndpoint is the route for merging PDF files.
MergeEndpoint = "/merge"
// ConvertGroupEndpoint is the route of the group
// in charge of converting files to PDF.
ConvertGroupEndpoint = "/convert"
// HTMLEndpoint is the route for converting
// HTML to PDF.
HTMLEndpoint = "/html"
// URLEndpoint is the route for converting
// a URL to PDF.
URLEndpoint = "/url"
// MarkdownEndpoint is the route for converting
// Markdown to PDF.
MarkdownEndpoint = "/markdown"
// OfficeEndpoint is the route for converting
// Office files to PDF.
OfficeEndpoint = "/office"
)
func convert(ctx *context.Context, p printer.Printer) error {
const (
op = "convert"
debugOp = "handler.convert"
)
r := ctx.Resource()
logger := ctx.StandardLogger()
baseFilename := random.Get()
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.Has(resource.WebhookURLFormField) {
logger.DebugfOp(debugOp, "no '%s' found, converting synchronously", resource.WebhookURLFormField)
if err := convertSync(filename, fpath, ctx, p); err != nil {
return &standarderror.Error{Op: op, Err: err}
}
return nil
}
// as a webhook URL has been given, we
// run the following lines in a goroutine so that
// it doesn't block.
logger.DebugfOp(debugOp, "'%s' found, converting asynchronously", resource.WebhookURLFormField)
return convertAsync(filename, fpath, ctx, p)
}
func convertSync(filename, fpath string, ctx *context.Context, p printer.Printer) error {
const (
op = "convertSync"
debugOp = "handler.convertSync"
)
r := ctx.Resource()
logger := ctx.StandardLogger()
if err := p.Print(fpath); err != nil {
return &standarderror.Error{Op: op, Err: err}
}
if !r.Has(resource.ResultFilenameFormField) {
logger.DebugfOp(
debugOp,
"no '%s' found, using generated filename '%s'",
resource.ResultFilenameFormField,
filename,
)
if err := ctx.Attachment(fpath, filename); err != nil {
return &standarderror.Error{Op: op, Err: err}
}
return nil
}
logger.DebugfOp(
debugOp,
"'%s' found, so not using generated filename",
resource.ResultFilenameFormField,
)
filename, err := r.Get(resource.ResultFilenameFormField)
if err != nil {
return &standarderror.Error{Op: op, Err: err}
}
if err := ctx.Attachment(fpath, filename); err != nil {
return &standarderror.Error{Op: op, Err: err}
}
return nil
}
func convertAsync(filename, fpath string, ctx *context.Context, p printer.Printer) error {
const (
op = "convertAsync"
debugOp = "handler.convertAsync"
)
r := ctx.Resource()
logger := ctx.StandardLogger()
go func() {
defer r.Close() // nolint: errcheck
if err := p.Print(fpath); err != nil {
logger.ErrorOp(
op,
&standarderror.Error{Op: op, Err: err},
)
return
}
f, err := os.Open(fpath)
if err != nil {
logger.ErrorOp(
op,
&standarderror.Error{Op: op, Err: err},
)
return
}
defer f.Close() // nolint: errcheck
webhookURL, err := r.Get(resource.WebhookURLFormField)
if err != nil {
logger.ErrorOp(
op,
&standarderror.Error{Op: op, Err: err},
)
return
}
logger.DebugfOp(
debugOp,
"sending result file '%s' to '%s'",
filename,
webhookURL,
)
resp, err := http.Post(webhookURL, "application/pdf", f) /* #nosec */
if err != nil {
logger.ErrorOp(
op,
&standarderror.Error{Op: op, Err: err},
)
return
}
defer resp.Body.Close() // nolint: errcheck
}()
return nil
}

View File

@@ -0,0 +1,24 @@
package handler
import (
"github.com/labstack/echo/v4"
"github.com/thecodingmachine/gotenberg/internal/app/api/pkg/context"
"github.com/thecodingmachine/gotenberg/internal/pkg/printer"
)
// HTML is the endpoint for converting
// HTML to PDF.
func HTML(c echo.Context) error {
ctx := context.MustCastFromEchoContext(c)
r := ctx.Resource()
opts, err := r.ChromePrinterOptions()
if err != nil {
return err
}
fpath, err := r.Fpath("index.html")
if err != nil {
return err
}
p := printer.NewHTML(fpath, opts)
return convert(ctx, p)
}

View File

@@ -0,0 +1,27 @@
package handler
import (
"github.com/labstack/echo/v4"
"github.com/thecodingmachine/gotenberg/internal/app/api/pkg/context"
"github.com/thecodingmachine/gotenberg/internal/pkg/printer"
)
// Markdown is the endpoint for converting
// Markdown to PDF.
func Markdown(c echo.Context) error {
ctx := context.MustCastFromEchoContext(c)
r := ctx.Resource()
opts, err := r.ChromePrinterOptions()
if err != nil {
return err
}
fpath, err := r.Fpath("index.html")
if err != nil {
return err
}
p, err := printer.NewMarkdown(fpath, opts)
if err != nil {
return err
}
return convert(ctx, p)
}

View File

@@ -0,0 +1,24 @@
package handler
import (
"github.com/labstack/echo/v4"
"github.com/thecodingmachine/gotenberg/internal/app/api/pkg/context"
"github.com/thecodingmachine/gotenberg/internal/pkg/printer"
)
// Merge is the endpoint for
// merging PDF files.
func Merge(c echo.Context) error {
ctx := context.MustCastFromEchoContext(c)
r := ctx.Resource()
opts, err := r.MergePrinterOptions()
if err != nil {
return err
}
fpaths, err := r.Fpaths(".pdf")
if err != nil {
return err
}
p := printer.NewMerge(fpaths, opts)
return convert(ctx, p)
}

View File

@@ -0,0 +1,37 @@
package handler
import (
"github.com/labstack/echo/v4"
"github.com/thecodingmachine/gotenberg/internal/app/api/pkg/context"
"github.com/thecodingmachine/gotenberg/internal/pkg/printer"
)
// Office is the endpoint for converting
// Office files to PDF.
func Office(c echo.Context) error {
ctx := context.MustCastFromEchoContext(c)
r := ctx.Resource()
opts, err := r.OfficePrinterOptions()
if err != nil {
return err
}
fpaths, err := r.Fpaths(
".txt",
".rtf",
".fodt",
".doc",
".docx",
".odt",
".xls",
".xlsx",
".ods",
".ppt",
".pptx",
".odp",
)
if err != nil {
return err
}
p := printer.NewOffice(fpaths, opts)
return convert(ctx, p)
}

View File

@@ -0,0 +1,10 @@
package handler
import (
"github.com/labstack/echo/v4"
)
// Ping is the endpoint for healthcheck.
func Ping(c echo.Context) error {
return nil
}

View File

@@ -0,0 +1,25 @@
package handler
import (
"github.com/labstack/echo/v4"
"github.com/thecodingmachine/gotenberg/internal/app/api/pkg/context"
"github.com/thecodingmachine/gotenberg/internal/app/api/pkg/resource"
"github.com/thecodingmachine/gotenberg/internal/pkg/printer"
)
// URL is the endpoint for converting
// a URL to PDF.
func URL(c echo.Context) error {
ctx := context.MustCastFromEchoContext(c)
r := ctx.Resource()
opts, err := r.ChromePrinterOptions()
if err != nil {
return err
}
remoteURL, err := r.Get(resource.RemoteURLFormField)
if err != nil {
return err
}
p := printer.NewURL(remoteURL, opts)
return convert(ctx, p)
}

View File

@@ -0,0 +1,38 @@
package middleware
import (
"github.com/labstack/echo/v4"
"github.com/thecodingmachine/gotenberg/internal/app/api/pkg/context"
"github.com/thecodingmachine/gotenberg/internal/app/api/pkg/resource"
"github.com/thecodingmachine/gotenberg/internal/pkg/standarderror"
)
// Cleanup helps removing a resource at the end of a request.
func Cleanup() echo.MiddlewareFunc {
return func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
const op = "middleware.Cleanup"
err := next(c)
ctx := context.MustCastFromEchoContext(c)
r := ctx.Resource()
if r == nil {
return err
}
// if a webhook URL has been given,
// do not remove the resource here because
// we don't know if the result file has been
// generated or sent.
if r.Has(resource.WebhookURLFormField) {
return err
}
// a resource is associated with our custom context.
if resourceErr := r.Close(); resourceErr != nil {
ctx.StandardLogger().ErrorOp(op, &standarderror.Error{
Op: op,
Err: resourceErr,
})
}
return err
}
}
}

View File

@@ -0,0 +1,41 @@
package middleware
import (
"github.com/labstack/echo/v4"
"github.com/thecodingmachine/gotenberg/internal/app/api/pkg/context"
"github.com/thecodingmachine/gotenberg/internal/app/api/pkg/handler"
"github.com/thecodingmachine/gotenberg/internal/pkg/config"
"github.com/thecodingmachine/gotenberg/internal/pkg/logger"
"github.com/thecodingmachine/gotenberg/internal/pkg/random"
)
// Context helps extending the default echo.Context with
// our custom context.
func Context(config *config.Config) echo.MiddlewareFunc {
return func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
// generate a unique identifier for the request.
trace := random.Get()
// create the logger for this request using
// the previous identifier as trace.
logger := logger.New(config.LogLevel(), trace)
// extend the current echo context with our custom
// context.
ctx := context.New(c, logger, config)
// if its an healthcheck request, there
// is no resource associated to it.
if ctx.Path() == handler.PingEndpoint {
return next(ctx)
}
// if the endpoint is not for healthcheck, associate a
// resource to our custom context.
if err := ctx.WithResource(trace); err != nil {
// required to have a correct status code
// in the logs.
ctx.Error(err)
return ctx.LogRequestResult(err, false)
}
return next(ctx)
}
}
}

View File

@@ -0,0 +1,3 @@
// Package middleware contains the
// middleware of the API.
package middleware

View File

@@ -0,0 +1,43 @@
package middleware
import (
"net/http"
"github.com/labstack/echo/v4"
"github.com/thecodingmachine/gotenberg/internal/app/api/pkg/context"
"github.com/thecodingmachine/gotenberg/internal/pkg/standarderror"
)
// Error helps handling errors (if any).
func Error() echo.MiddlewareFunc {
return func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
ctx := context.MustCastFromEchoContext(c)
err := next(ctx)
if err == nil {
// so far so good!
return nil
}
// we log the initial error before returning
// the HTTP error.
logger := ctx.StandardLogger()
logger.Error(err.Error())
// handle our custom HTTP error.
var httpErr error
errCode := standarderror.Code(err)
errMessage := standarderror.Message(err)
switch errCode {
case standarderror.Invalid:
httpErr = echo.NewHTTPError(http.StatusBadRequest, errMessage)
case standarderror.Timeout:
httpErr = echo.NewHTTPError(http.StatusRequestTimeout, errMessage)
default:
httpErr = echo.NewHTTPError(http.StatusInternalServerError, errMessage)
}
// required to have a correct status code
// in the logs.
ctx.Error(httpErr)
return httpErr
}
}
}

View File

@@ -0,0 +1,21 @@
package middleware
import (
"github.com/labstack/echo/v4"
"github.com/thecodingmachine/gotenberg/internal/app/api/pkg/context"
"github.com/thecodingmachine/gotenberg/internal/app/api/pkg/handler"
)
// Logger helps logging the result of a request.
func Logger() echo.MiddlewareFunc {
return func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
ctx := context.MustCastFromEchoContext(c)
err := next(ctx)
// we do not want to log healthcheck requests if
// log level is not set to DEBUG.
isDebug := ctx.Path() == handler.PingEndpoint
return ctx.LogRequestResult(err, isDebug)
}
}
}

View File

@@ -0,0 +1,5 @@
// Package resource helps creating a folder
// containing all uploaded files and the resulting
// PDF file. It also helps centralizing all
// the form values.
package resource

View File

@@ -0,0 +1,417 @@
package resource
import (
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"strconv"
"github.com/labstack/echo/v4"
"github.com/thecodingmachine/gotenberg/internal/pkg/config"
"github.com/thecodingmachine/gotenberg/internal/pkg/logger"
"github.com/thecodingmachine/gotenberg/internal/pkg/printer"
"github.com/thecodingmachine/gotenberg/internal/pkg/standarderror"
)
const (
// ResultFilenameFormField contains the name
// of a form field.
ResultFilenameFormField string = "resultFilename"
// WaitTimeoutFormField contains the name
// of a form field.
WaitTimeoutFormField string = "waitTimeout"
// WebhookURLFormField contains the name
// of a form field.
WebhookURLFormField string = "webhookURL"
// RemoteURLFormField contains the name
// of a form field.
RemoteURLFormField string = "remoteURL"
// WaitDelayFormField contains the name
// of a form field.
WaitDelayFormField string = "waitDelay"
// PaperWidthFormField contains the name
// of a form field.
PaperWidthFormField string = "paperWidth"
// PaperHeightFormField contains the name
// of a form field.
PaperHeightFormField string = "paperHeight"
// MarginTopFormField contains the name
// of a form field.
MarginTopFormField string = "marginTop"
// MarginBottomFormField contains the name
// of a form field.
MarginBottomFormField string = "marginBottom"
// MarginLeftFormField contains the name
// of a form field.
MarginLeftFormField string = "marginLeft"
// MarginRightFormField contains the name
// of a form field.
MarginRightFormField string = "marginRight"
// LandscapeFormField contains the name
// of a form field.
LandscapeFormField string = "landscape"
)
// Resource helps retrieving form values
// and form files from a request.
type Resource struct {
logger *logger.Logger
config *config.Config
formValues map[string]string
formFilesDirPath string
}
// New creates a new resource.
func New(c echo.Context, logger *logger.Logger, config *config.Config, dirPath string) (*Resource, error) {
const op = "resource.New"
r := &Resource{
logger: logger,
config: config,
formValues: formValues(c, logger),
formFilesDirPath: dirPath,
}
if err := os.MkdirAll(dirPath, 0755); err != nil {
return nil, &standarderror.Error{Op: op, Err: err}
}
r.logger.DebugfOp(op, "directory '%s' created", dirPath)
if err := formFiles(c, logger, dirPath); err != nil {
return r, &standarderror.Error{Op: op, Err: err}
}
return r, nil
}
func formValues(c echo.Context, logger *logger.Logger) map[string]string {
const debugOp = "resource.formValues"
v := make(map[string]string)
v[ResultFilenameFormField] = c.FormValue(ResultFilenameFormField)
v[WaitTimeoutFormField] = c.FormValue(WaitTimeoutFormField)
v[WebhookURLFormField] = c.FormValue(WebhookURLFormField)
v[RemoteURLFormField] = c.FormValue(RemoteURLFormField)
v[WaitDelayFormField] = c.FormValue(WaitDelayFormField)
v[PaperWidthFormField] = c.FormValue(PaperWidthFormField)
v[PaperHeightFormField] = c.FormValue(PaperHeightFormField)
v[MarginTopFormField] = c.FormValue(MarginTopFormField)
v[MarginBottomFormField] = c.FormValue(MarginBottomFormField)
v[MarginLeftFormField] = c.FormValue(MarginLeftFormField)
v[MarginRightFormField] = c.FormValue(MarginRightFormField)
v[LandscapeFormField] = c.FormValue(LandscapeFormField)
logger.DebugfOp(debugOp, "%v", v)
return v
}
func formFiles(c echo.Context, logger *logger.Logger, dirPath string) error {
const (
op = "formFiles"
debugOp = "resource.formFiles"
)
form, err := c.MultipartForm()
if err != nil {
return &standarderror.Error{Op: op, Err: err}
}
for _, files := range form.File {
for _, fh := range files {
in, err := fh.Open()
if err != nil {
return &standarderror.Error{Op: op, Err: err}
}
defer in.Close() // nolint: errcheck
fpath := fmt.Sprintf("%s/%s", dirPath, fh.Filename)
out, err := os.Create(fpath)
if err != nil {
return &standarderror.Error{Op: op, Err: err}
}
defer out.Close() // nolint: errcheck
if err := out.Chmod(0644); err != nil {
return &standarderror.Error{Op: op, Err: err}
}
if _, err := io.Copy(out, in); err != nil {
return &standarderror.Error{Op: op, Err: err}
}
if _, err := out.Seek(0, 0); err != nil {
return &standarderror.Error{Op: op, Err: err}
}
logger.DebugfOp(debugOp, "'%s' created", fh.Filename)
}
}
return nil
}
// DirPath returns the directory
// path where are stored the form
// files and the resulting PDF file.
func (r *Resource) DirPath() string {
return r.formFilesDirPath
}
// Close deletes the working directory of the
// resource if it exists.
func (r *Resource) Close() error {
const op = "resource.Close"
if _, err := os.Stat(r.formFilesDirPath); os.IsNotExist(err) {
r.logger.DebugfOp(op, "directory '%s' does not exist, nothing to remove", r.formFilesDirPath)
return nil
}
if err := os.RemoveAll(r.formFilesDirPath); err != nil {
return &standarderror.Error{Op: op, Err: err}
}
r.logger.DebugfOp(op, "directory '%s' removed", r.formFilesDirPath)
return nil
}
const defaultHeaderFooterHTML string = "<html><head></head><body></body></html>"
// ChromePrinterOptions returns the Chrome printer options
// thanks to the form values and form files from the request
// plus the default values from the configuration.
func (r *Resource) ChromePrinterOptions() (*printer.ChromeOptions, error) {
const op = "resource.ChromePrinterOptions"
waitTimeout, err := r.float64(WaitTimeoutFormField, r.config.DefaultWaitTimeout())
if err != nil {
return nil, &standarderror.Error{Op: op, Err: err}
}
waitDelay, err := r.float64(WaitDelayFormField, 0.0)
if err != nil {
return nil, &standarderror.Error{Op: op, Err: err}
}
headerHTML, err := r.content("header.html", defaultHeaderFooterHTML)
if err != nil {
return nil, &standarderror.Error{Op: op, Err: err}
}
footerHTML, err := r.content("footer.html", defaultHeaderFooterHTML)
if err != nil {
return nil, &standarderror.Error{Op: op, Err: err}
}
paperWidth, err := r.float64(PaperWidthFormField, 8.27)
if err != nil {
return nil, &standarderror.Error{Op: op, Err: err}
}
paperHeight, err := r.float64(PaperHeightFormField, 11.7)
if err != nil {
return nil, &standarderror.Error{Op: op, Err: err}
}
marginTop, err := r.float64(MarginTopFormField, 1)
if err != nil {
return nil, &standarderror.Error{Op: op, Err: err}
}
marginBottom, err := r.float64(MarginBottomFormField, 1)
if err != nil {
return nil, &standarderror.Error{Op: op, Err: err}
}
marginLeft, err := r.float64(MarginLeftFormField, 1)
if err != nil {
return nil, &standarderror.Error{Op: op, Err: err}
}
marginRight, err := r.float64(MarginRightFormField, 1)
if err != nil {
return nil, &standarderror.Error{Op: op, Err: err}
}
landscape, err := r.bool(LandscapeFormField, false)
if err != nil {
return nil, &standarderror.Error{Op: op, Err: err}
}
opts := &printer.ChromeOptions{
WaitTimeout: waitTimeout,
WaitDelay: waitDelay,
HeaderHTML: headerHTML,
FooterHTML: footerHTML,
PaperWidth: paperWidth,
PaperHeight: paperHeight,
MarginTop: marginTop,
MarginBottom: marginBottom,
MarginLeft: marginLeft,
MarginRight: marginRight,
Landscape: landscape,
}
r.logger.DebugfOp(op, "%v", opts)
return opts, nil
}
// OfficePrinterOptions returns the Office printer options
// thanks to the form values from the request
// plus the default values from the configuration.
func (r *Resource) OfficePrinterOptions() (*printer.OfficeOptions, error) {
const op = "resource.OfficePrinterOptions"
waitTimeout, err := r.float64(WaitTimeoutFormField, r.config.DefaultWaitTimeout())
if err != nil {
return nil, &standarderror.Error{Op: op, Err: err}
}
landscape, err := r.bool(LandscapeFormField, false)
if err != nil {
return nil, &standarderror.Error{Op: op, Err: err}
}
opts := &printer.OfficeOptions{
WaitTimeout: waitTimeout,
Landscape: landscape,
}
r.logger.DebugfOp(op, "%v", opts)
return opts, nil
}
// MergePrinterOptions returns the merge printer options
// thanks to the form values from the request
// plus the default values from the configuration.
func (r *Resource) MergePrinterOptions() (*printer.MergeOptions, error) {
const op = "resource.MergePrinterOptions"
waitTimeout, err := r.float64(WaitTimeoutFormField, r.config.DefaultWaitTimeout())
if err != nil {
return nil, &standarderror.Error{Op: op, Err: err}
}
opts := &printer.MergeOptions{
WaitTimeout: waitTimeout,
}
r.logger.DebugfOp(op, "%v", opts)
return opts, nil
}
// Has returns true if the resource
// contains the given form field and
// its value is not empty.
func (r *Resource) Has(formField string) bool {
v, ok := r.formValues[formField]
if ok {
ok = v != ""
}
return ok
}
func (r *Resource) hasFile(filename string) bool {
fpath := fmt.Sprintf("%s/%s", r.formFilesDirPath, filename)
_, err := os.Stat(fpath)
return !os.IsNotExist(err)
}
// Get returns the form field value.
func (r *Resource) Get(formField string) (string, error) {
const op = "resource.Get"
v, err := r.value(formField)
if err != nil {
return "", &standarderror.Error{Op: op, Err: err}
}
return v, nil
}
func (r *Resource) value(formField string) (string, error) {
const op = "value"
v, ok := r.formValues[formField]
if !ok {
return "", &standarderror.Error{
Code: standarderror.Invalid,
Message: fmt.Sprintf("'%s' does not exist", formField),
Op: op,
}
}
return v, nil
}
func (r *Resource) float64(formField string, defaultValue float64) (float64, error) {
const op = "float64"
if !r.Has(formField) {
return defaultValue, nil
}
v, err := r.value(formField)
if err != nil {
return 0.0, &standarderror.Error{Op: op, Err: err}
}
f, err := strconv.ParseFloat(v, 64)
if err != nil {
return 0.0, &standarderror.Error{
Code: standarderror.Invalid,
Message: fmt.Sprintf("'%s' is not a float", formField),
Op: op,
}
}
return f, nil
}
func (r *Resource) bool(formField string, defaultValue bool) (bool, error) {
const op = "bool"
if !r.Has(formField) {
return defaultValue, nil
}
v, err := r.value(formField)
if err != nil {
return false, &standarderror.Error{Op: op, Err: err}
}
b, err := strconv.ParseBool(v)
if err != nil {
return false, &standarderror.Error{
Code: standarderror.Invalid,
Message: fmt.Sprintf("'%s' is not a boolean", formField),
Op: op,
}
}
return b, nil
}
// Fpath returns the path of the given filename.
// This filename should be the name of a form file.
func (r *Resource) Fpath(filename string) (string, error) {
const op = "resource.Fpath"
fpath := fmt.Sprintf("%s/%s", r.formFilesDirPath, filename)
_, err := os.Stat(fpath)
if os.IsNotExist(err) {
return "", &standarderror.Error{
Code: standarderror.Invalid,
Message: fmt.Sprintf("file '%s' does not exist", filename),
Op: op,
}
}
absPath, err := filepath.Abs(fpath)
if err != nil {
return "", &standarderror.Error{Op: op, Err: err}
}
return absPath, nil
}
func (r *Resource) content(filename string, defaultValue string) (string, error) {
const op = "content"
if !r.hasFile(filename) {
return defaultValue, nil
}
fpath, err := r.Fpath(filename)
if err != nil {
return "", &standarderror.Error{Op: op, Err: err}
}
b, err := ioutil.ReadFile(fpath)
if err != nil {
return "", &standarderror.Error{Op: op, Err: err}
}
return string(b), nil
}
// Fpaths returns the list of files of the resource
// according to given file extensions.
func (r *Resource) Fpaths(exts ...string) ([]string, error) {
const op = "resource.Fpaths"
var fpaths []string
err := filepath.Walk(r.formFilesDirPath, func(path string, info os.FileInfo, _ error) error {
const walkOp = "filepath.Walk"
if info.IsDir() {
return nil
}
fpath, err := r.Fpath(info.Name())
if err != nil {
return &standarderror.Error{Op: walkOp, Err: err}
}
for _, ext := range exts {
if filepath.Ext(fpath) == ext {
fpaths = append(fpaths, fpath)
return nil
}
}
return nil
})
if err != nil {
return nil, &standarderror.Error{Op: op, Err: err}
}
if len(fpaths) == 0 {
return nil, &standarderror.Error{
Code: standarderror.Invalid,
Message: fmt.Sprintf("no file found for extentions %v", exts),
Op: op,
}
}
return fpaths, nil
}