using a (more) random directory name for user profile tmp dir + improving performance with a go routine

This commit is contained in:
Julien Neuhart
2020-06-05 15:05:27 +02:00
parent 542fe18dcd
commit 8a218a6626

View File

@@ -94,13 +94,14 @@ func (p officePrinter) Print(destination string) error {
func (p officePrinter) unoconv(ctx context.Context, fpath, destination string) error { func (p officePrinter) unoconv(ctx context.Context, fpath, destination string) error {
const op string = "printer.unoconv" const op string = "printer.unoconv"
resolver := func() error { resolver := func() error {
dirName := xrand.Get()
port, err := freeport.GetFreePort() port, err := freeport.GetFreePort()
if err != nil { if err != nil {
return err return err
} }
args := []string{ args := []string{
"--user-profile", "--user-profile",
fmt.Sprintf("///tmp/%d", port), fmt.Sprintf("///tmp/%s", dirName),
"--port", "--port",
fmt.Sprintf("%d", port), fmt.Sprintf("%d", port),
"--format", "--format",
@@ -116,11 +117,7 @@ func (p officePrinter) unoconv(ctx context.Context, fpath, destination string) e
err = xexec.Run(ctx, p.logger, "unoconv", args...) err = xexec.Run(ctx, p.logger, "unoconv", args...)
// always remove user profile folders created by LibreOffice. // always remove user profile folders created by LibreOffice.
// see https://github.com/thecodingmachine/gotenberg/issues/192. // see https://github.com/thecodingmachine/gotenberg/issues/192.
userProfileDirPath := fmt.Sprintf("/tmp/%d", port) go cleanupUserProfile(p.logger, dirName)
if err := os.RemoveAll(userProfileDirPath); err != nil {
// find a way to bubble up this error?
p.logger.ErrorOpf(op, "failed to remove user profile directory '%s': %s", userProfileDirPath, err.Error())
}
if err != nil { if err != nil {
// find a way to check it in the handlers? // find a way to check it in the handlers?
if p.opts.PageRanges != "" && strings.Contains(err.Error(), "exit status 5") { if p.opts.PageRanges != "" && strings.Contains(err.Error(), "exit status 5") {
@@ -140,6 +137,15 @@ func (p officePrinter) unoconv(ctx context.Context, fpath, destination string) e
return nil return nil
} }
func cleanupUserProfile(logger xlog.Logger, dirName string) {
const op = "printer.cleanupUserProfile"
path := fmt.Sprintf("/tmp/%s", dirName)
if err := os.RemoveAll(path); err != nil {
// find a way to bubble up this error?
logger.ErrorOpf(op, "failed to remove user profile directory '%s': %s", path, err.Error())
}
}
// Compile-time checks to ensure type implements desired interfaces. // Compile-time checks to ensure type implements desired interfaces.
var ( var (
_ = Printer(new(officePrinter)) _ = Printer(new(officePrinter))