From c1ccbc553fc756ffc6954574727bc4a1dd7be783 Mon Sep 17 00:00:00 2001
From: John MacFarlane <jgm@berkeley.edu>
Date: Fri, 24 Jun 2022 17:52:39 -0700
Subject: [PATCH] PDF: use sha1 hash of filename when converting svg.

The previous code threw away the directory component of
the filename in constructing a new one. This led to
surprising results if you had e.g. `foo/pic.svg` and `bar/pic.svg`;
in the final PDF they'd be the same image, because the latter
would overwrite the former in the temp directory.
---
 src/Text/Pandoc/PDF.hs | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/src/Text/Pandoc/PDF.hs b/src/Text/Pandoc/PDF.hs
index 5833cb605..40fb8befc 100644
--- a/src/Text/Pandoc/PDF.hs
+++ b/src/Text/Pandoc/PDF.hs
@@ -52,6 +52,7 @@ import qualified Text.Pandoc.UTF8 as UTF8
 import Text.Pandoc.Walk (walkM)
 import Text.Pandoc.Writers.Shared (getField, metaToContext)
 import Control.Monad.Catch (MonadMask)
+import Data.Digest.Pure.SHA (sha1, showDigest)
 #ifdef _WINDOWS
 import Data.List (intercalate)
 #endif
@@ -215,8 +216,9 @@ convertImage opts tmpdir fname = do
                  E.catch (Right pngOut <$ JP.savePngImage pngOut img) $
                      \(e :: E.SomeException) -> return (Left (tshow e))
   where
-    pngOut = normalise $ replaceDirectory (replaceExtension fname ".png") tmpdir
-    pdfOut = normalise $ replaceDirectory (replaceExtension fname ".pdf") tmpdir
+    sha = showDigest (sha1 (UTF8.fromStringLazy fname))
+    pngOut = normalise $ tmpdir </> sha <.> "png"
+    pdfOut = normalise $ tmpdir </> sha <.> "pdf"
     svgIn = normalise fname
     mime = getMimeType fname
     doNothing = return (Right fname)