Skip to content
This repository has been archived by the owner on Jan 9, 2025. It is now read-only.

Commit

Permalink
test: workaround
Browse files Browse the repository at this point in the history
  • Loading branch information
chuang8511 committed Sep 27, 2024
1 parent f2b9fb3 commit 0b52097
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions operator/document/v0/markdown_transformer.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/base64"
"encoding/csv"
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -245,7 +246,7 @@ func ConvertToPDF(base64Encoded, fileExtension string) (string, error) {
return "", fmt.Errorf("failed to create temporary document: %w", err)
}
inputFileName := tempPpt.Name()
defer os.Remove(inputFileName)
// defer os.Remove(inputFileName)

err = writeDecodeToFile(base64Encoded, tempPpt)
if err != nil {
Expand All @@ -256,7 +257,7 @@ func ConvertToPDF(base64Encoded, fileExtension string) (string, error) {
if err != nil {
return "", fmt.Errorf("failed to create temporary directory: %s", err.Error())
}
defer os.RemoveAll(tempDir)
// defer os.RemoveAll(tempDir)

cmd := exec.Command("libreoffice", "--headless", "--convert-to", "pdf", inputFileName)
cmd.Env = append(os.Environ(), "HOME="+tempDir)
Expand All @@ -270,18 +271,25 @@ func ConvertToPDF(base64Encoded, fileExtension string) (string, error) {
// So, we need to remove the path and keep only the file name.
noPathFileName := filepath.Base(inputFileName)
tempPDFName := strings.TrimSuffix(noPathFileName, filepath.Ext(inputFileName)) + ".pdf"
defer os.Remove(tempPDFName)
// defer os.Remove(tempPDFName)

base64PDF, err := encodeFileToBase64(tempPDFName)

if err != nil {
// In the different containers, we have the different versions of LibreOffice, which means the behavior of LibreOffice may be different.
// So, we need to handle the case when the generated PDF is not in the temp directory.

log.Printf("fileExtension: %s", fileExtension)
log.Printf("tempPDFName: %s", tempPDFName)
log.Printf("tempDir: %s", tempDir)
log.Printf("inputFileName: %s", inputFileName)

if fileExtension == "pdf" {
base64PDF, err := encodeFileToBase64(inputFileName)
if err != nil {
return "", fmt.Errorf("failed to encode file to base64: %w", err)
}
log.Printf("base64PDF: %s", base64PDF)
return base64PDF, nil
}
}
Expand Down

0 comments on commit 0b52097

Please sign in to comment.