feat(libreoffice): preserve filenames for zipped files instead of using randomly generated UUID filenames (#793)

* feat(libreoffice): preserve filenames for zipped files instead of using generated UUID filenames

* chore: move github.com/google/uuid back to its original line

* fix: add libreoffice/routes.go to PR
This commit is contained in:
Chris Hughes
2024-02-16 07:29:22 -08:00
committed by Julien Neuhart
parent e6d7131701
commit 089f161d1e
3 changed files with 101 additions and 5 deletions

View File

@@ -4,6 +4,8 @@ import (
"errors"
"fmt"
"net/http"
"path/filepath"
"strings"
"github.com/labstack/echo/v4"
@@ -51,10 +53,24 @@ func convertRoute(libreOffice libreofficeapi.Uno, engine gotenberg.PdfEngine) ap
PdfUa: pdfua,
}
// We need to check and see if there are any duplicate filenames in inputPaths.
filenameCounts := make(map[string]int)
for _, path := range inputPaths {
filename := strings.TrimSuffix(filepath.Base(path), filepath.Ext(path))
filenameCounts[filename]++
}
// Alright, let's convert each document to PDF.
outputPaths := make([]string, len(inputPaths))
for i, inputPath := range inputPaths {
outputPaths[i] = ctx.GeneratePath(".pdf")
filename := strings.TrimSuffix(filepath.Base(inputPath), filepath.Ext(inputPath))
extension := filepath.Ext(inputPath)
// Ex: `document.docx`, `document.doc` -> `document.docx.pdf`, `document.doc.pdf`
if filenameCounts[filename] > 1 {
outputPaths[i] = ctx.GeneratePath(".pdf", filename+extension)
} else {
outputPaths[i] = ctx.GeneratePath(".pdf", filename)
}
options := libreofficeapi.Options{
Landscape: landscape,