-
Notifications
You must be signed in to change notification settings - Fork 72
Refactor graphImageResponse to use Temporary Files #1667
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,18 +15,21 @@ import Data.Time.Calendar.OrdinalDate (fromMondayStartWeek, mondayStartWeek) | |
| import Database.Persist.Sqlite (entityKey, entityVal, selectList, (==.)) | ||
| import Database.Tables | ||
| import Export.GetImages | ||
| import Export.ImageConversion | ||
| import Export.LatexGenerator | ||
| import Export.PdfGenerator | ||
| import Happstack.Server | ||
| import MasterTemplate | ||
| import Models.Meeting (returnMeeting) | ||
| import Scripts | ||
| import System.Directory (removeFile) | ||
| import System.FilePath ((</>)) | ||
| import System.IO (IOMode (WriteMode), hClose, withFile) | ||
| import System.IO.Temp (withSystemTempDirectory, withSystemTempFile) | ||
| import Text.Blaze ((!)) | ||
| import qualified Text.Blaze.Html5 as H | ||
| import qualified Text.Blaze.Html5.Attributes as A | ||
| import Text.Read (readMaybe) | ||
| import Util.Helpers (returnImageData, safeHead) | ||
| import Util.Helpers (readImageData, safeHead) | ||
|
|
||
| gridResponse :: ServerPart Response | ||
| gridResponse = | ||
|
|
@@ -45,37 +48,53 @@ exportTimetableImageResponse :: ServerPart Response | |
| exportTimetableImageResponse = do | ||
| session <- lookText' "session" | ||
| selectedCourses <- lookText' "courses" | ||
| (svgFilename, imageFilename) <- liftIO $ getActiveTimetable selectedCourses session | ||
| liftIO $ returnImageData svgFilename imageFilename | ||
|
|
||
| liftIO $ withSystemTempFile "timetable.svg" $ \svgPath svgHandle -> do | ||
| withSystemTempFile "timetable.png" $ \pngPath pngHandle -> do | ||
| hClose svgHandle | ||
| hClose pngHandle | ||
| getActiveTimetable selectedCourses session svgPath pngPath | ||
| readImageData pngPath | ||
|
|
||
| -- | Returns a PDF containing graph and timetable requested by the user. | ||
| exportTimetablePDFResponse :: ServerPart Response | ||
| exportTimetablePDFResponse = do | ||
| selectedCourses <- lookText' "courses" | ||
| graphInfo <- look "JsonLocalStorageObj" | ||
| (graphSvg, graphImg) <- liftIO $ getActiveGraphImage graphInfo | ||
| (fallsvgFilename, fallimageFilename) <- liftIO $ getActiveTimetable selectedCourses "Fall" | ||
| (springsvgFilename, springimageFilename) <- liftIO $ getActiveTimetable selectedCourses "Spring" | ||
| pdfName <- liftIO $ returnPDF graphSvg graphImg fallsvgFilename fallimageFilename springsvgFilename springimageFilename | ||
| liftIO $ returnPdfBS pdfName | ||
|
|
||
| -- | Returns 64base bytestring of PDF for given name, then deletes PDF from local. | ||
| liftIO $ withSystemTempDirectory "timetable-pdf" $ \tempDir -> do | ||
| let graphSvgPath = tempDir </> "graph.svg" | ||
| graphPngPath = tempDir </> "graph.png" | ||
| fallSvgPath = tempDir </> "fall.svg" | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Overall you can simplify the code here by passing |
||
| fallPngPath = tempDir </> "fall.png" | ||
| springSvgPath = tempDir </> "spring.svg" | ||
| springPngPath = tempDir </> "spring.png" | ||
|
|
||
| withFile graphSvgPath WriteMode $ \graphSvgHandle -> | ||
| writeActiveGraphImage graphInfo graphSvgHandle | ||
| createImageFile graphSvgPath graphPngPath | ||
|
|
||
| getActiveTimetable selectedCourses "Fall" fallSvgPath fallPngPath | ||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's quite a few blank lines in this section. I would remove this one at least, and possibly some others. |
||
| getActiveTimetable selectedCourses "Spring" springSvgPath springPngPath | ||
|
|
||
| pdfName <- returnPDF graphPngPath fallPngPath springPngPath tempDir | ||
| returnPdfBS pdfName | ||
|
|
||
| -- | Returns 64base bytestring of PDF for given name. | ||
| returnPdfBS :: String -> IO Response | ||
| returnPdfBS pdfFilename = do | ||
| pdfData <- BS.readFile pdfFilename | ||
| _ <- removeFile pdfFilename | ||
| return $ toResponseBS "application/pdf" $ BEnc.encode $ L.fromStrict pdfData | ||
|
|
||
| -- | Returns the name of a generated pdf that contains graphImg and timetableImg | ||
| -- and deletes all of the img and svg files passed as arguments | ||
| returnPDF :: String -> String -> String -> String -> String -> String -> IO String | ||
| returnPDF graphSvg graphImg fallTimetableSvg fallTimetableImg springTimetableSvg springTimetableImg = do | ||
| rand <- randomName | ||
| let texName = rand ++ ".tex" | ||
| pdfName = rand ++ ".pdf" | ||
| -- | Returns the name of a generated pdf that contains graphImg, fallTimetableImg, | ||
| -- and springTimetableImg generated in tempDir. | ||
| returnPDF :: String -> String -> String -> FilePath -> IO String | ||
| returnPDF graphImg fallTimetableImg springTimetableImg tempDir = do | ||
| let texName = tempDir </> "timetable.tex" | ||
| pdfName = tempDir </> "timetable.pdf" | ||
| generateTex [graphImg, fallTimetableImg, springTimetableImg] texName -- generate a temporary TEX file | ||
| createPDF texName -- create PDF using TEX and delete the TEX file afterwards | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This comment is no longer accurate (doesn't delete the TEX file afterwards). |
||
| mapM_ removeFile [graphSvg, graphImg, fallTimetableSvg, fallTimetableImg, springTimetableSvg, springTimetableImg] | ||
| return pdfName | ||
|
|
||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
always list explicit names that are imported (e.g.,
createImageFile)