git.iko.soy/typeable/pandoc
on master help sign in
Fix infinite HTTP requests when writing epubs from URL source.

Due to a bug in code added to avoid overwriting the cover image if it had the form `fileX.YYY`, pandoc made an endless sequence of HTTP requests when writing epub with input from a URL. Closes #7013.

commite741c7f5533d60971e98c6ced477d998335ef0b5 authorJohn MacFarlane <jgm@berkeley.edu> dateSun 10 Jan 2021, 12:49 (6 years ago) parentd98ec4f
1 file changed+9−5
src/Text/Pandoc/Writers/EPUB.hs+9−5
src/Text/Pandoc/Writers/EPUB.hs+9−5
@@ -465,7 +465,12 @@ pandocToEPUB version opts doc = do
465465 case epubCoverImage metadata of
466466 Nothing -> return ([],[])
467467 Just img -> do
468− let coverImage = takeFileName img
468+ let fp = takeFileName img
469+ mediaPaths <- gets (map (fst . snd) . stMediaPaths)
470+ coverImageName <- -- see #4206
471+ if ("media/" <> fp) `elem` mediaPaths
472+ then getMediaNextNewName (takeExtension fp)
473+ else return fp
469474 imgContent <- lift $ P.readFileLazy img
470475 (coverImageWidth, coverImageHeight) <-
471476 case imageSize opts' (B.toStrict imgContent) of
@@ -478,7 +483,7 @@ pandocToEPUB version opts doc = do
478483 ("coverpage", toVal' "true"),
479484 ("pagetitle", toVal $
480485 escapeStringForXML $ TS.pack plainTitle),
481− ("cover-image", toVal' coverImage),
486+ ("cover-image", toVal' coverImageName),
482487 ("cover-image-width", toVal' $
483488 show coverImageWidth),
484489 ("cover-image-height", toVal' $
@@ -486,7 +491,7 @@ pandocToEPUB version opts doc = do
486491 cssvars True <> vars }
487492 (Pandoc meta [])
488493 coverEntry <- mkEntry "text/cover.xhtml" cpContent
489− coverImageEntry <- mkEntry ("media/" ++ coverImage)
494+ coverImageEntry <- mkEntry ("media/" ++ coverImageName)
490495 imgContent
491496 return ( [ coverEntry ]
492497 , [ coverImageEntry ] )
@@ -1076,8 +1081,7 @@ getMediaNextNewName :: PandocMonad m => String -> E m String
10761081getMediaNextNewName ext = do
10771082 nextId <- gets stMediaNextId
10781083 modify $ \st -> st { stMediaNextId = nextId + 1 }
1079− let nextName = "file" ++ show nextId ++ ext
1080− (P.fetchItem (TS.pack nextName) >> getMediaNextNewName ext) `catchError` const (return nextName)
1084+ return $ "file" ++ show nextId ++ ext
10811085
10821086isHtmlFormat :: Format -> Bool
10831087isHtmlFormat (Format "html") = True