webhook: resulting file was deleted because of defer outside of goroutine

This commit is contained in:
Julien Neuhart
2018-12-13 18:15:28 +01:00
parent 404535c512
commit 4c1699df6c
6 changed files with 41 additions and 30 deletions

View File

@@ -87,11 +87,12 @@ func newContext(r *resource) (context.Context, context.CancelFunc) {
func print(c echo.Context, p printer.Printer, r *resource) error {
baseFilename, err := rand.Get()
if err != nil {
return fmt.Errorf("getting result file name: %v", err)
return hijackErr(fmt.Errorf("getting result file name: %v", err), r)
}
filename := fmt.Sprintf("%s.pdf", baseFilename)
fpath := fmt.Sprintf("%s/%s", r.dirPath, filename)
if r.webhookURL() == "" {
defer r.removeAll()
// if no webhook URL given, run conversion
// and directly return the resulting PDF file
// or an error.
@@ -104,6 +105,7 @@ func print(c echo.Context, p printer.Printer, r *resource) error {
// run the following lines in a goroutine so that
// it doesn't block.
go func() {
defer r.removeAll()
if err := p.Print(fpath); err != nil {
c.Logger().Errorf("%v", err)
return
@@ -123,3 +125,10 @@ func print(c echo.Context, p printer.Printer, r *resource) error {
}()
return nil
}
func hijackErr(err error, r *resource) error {
if r != nil {
defer r.removeAll()
}
return err
}