From fce06b5294e8984eebddb0167ce06f501f114ba0 Mon Sep 17 00:00:00 2001 From: Walid <19792122+wmahfoudh@users.noreply.github.com> Date: Sun, 22 Dec 2024 13:58:45 +0400 Subject: [PATCH] Fix cross-filesystem file move in to_pdf plugin (issue 1221) --- plugins/tools/to_pdf/to_pdf.go | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/plugins/tools/to_pdf/to_pdf.go b/plugins/tools/to_pdf/to_pdf.go index 23b7d7cb..f89b892e 100644 --- a/plugins/tools/to_pdf/to_pdf.go +++ b/plugins/tools/to_pdf/to_pdf.go @@ -76,12 +76,19 @@ func main() { } // Move the output PDF to the current directory - err = os.Rename(pdfPath, outputFile) + err = copyFile(pdfPath, outputFile) if err != nil { fmt.Fprintf(os.Stderr, "Error moving output file: %v\n", err) os.Exit(1) } + // Remove the original file after copying + err = os.Remove(pdfPath) + if err != nil { + fmt.Fprintf(os.Stderr, "Error cleaning up temporary file: %v\n", err) + os.Exit(1) + } + // Clean up temporary files cleanupTempFiles(tmpDir) @@ -103,3 +110,25 @@ func cleanupTempFiles(dir string) { } } } + +// Copy a file from source src to destination dst +func copyFile(src, dst string) error { + sourceFile, err := os.Open(src) + if err != nil { + return err + } + defer sourceFile.Close() + + destFile, err := os.Create(dst) + if err != nil { + return err + } + defer destFile.Close() + + _, err = io.Copy(destFile, sourceFile) + if err != nil { + return err + } + + return destFile.Sync() +} \ No newline at end of file