Skip to content

Commit 89ec0ff

Browse files
Refactored code in app/Response/Image.hs (#1606)
1 parent 626da75 commit 89ec0ff

File tree

8 files changed

+20
-33
lines changed

8 files changed

+20
-33
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
- Added test cases for the `ExportModal` component in `js/components/common`
3535
- Updated backend tests to use `tasty-discover`
3636
- Added documentation for running a subset of the backend tests
37+
- Deleted `app/Response/Image` file and refactored `app/Util/Helpers` to include `returnImageData`
3738

3839
## [0.7.1] - 2025-06-16
3940

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ Parker Hutcheson,
121121
Yoonie Jang,
122122
Jai Joshi,
123123
Aayush Karki,
124+
Japleen Kaur,
124125
Philip Kukulak,
125126
Jaeyong Lee,
126127
Ryan Lee,

app/Controllers/Graph.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ import Database.Persist.Sqlite (Entity, SelectOpt (Asc), SqlPersistM, selectList
1515
import Database.Tables as Tables (EntityField (GraphDynamic, GraphTitle), Graph, SvgJSON, Text)
1616
import Export.GetImages (getActiveGraphImage)
1717
import Models.Graph (getGraph, insertGraph)
18-
import Response.Image (returnImageData)
1918
import Util.Happstack (createJSONResponse)
19+
import Util.Helpers (returnImageData)
2020

2121
graphResponse :: ServerPart Response
2222
graphResponse =

app/Controllers/Timetable.hs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,13 @@ import Export.PdfGenerator
2020
import Happstack.Server
2121
import MasterTemplate
2222
import Models.Meeting (returnMeeting)
23-
import Response.Image (returnImageData)
2423
import Scripts
2524
import System.Directory (removeFile)
2625
import Text.Blaze ((!))
2726
import qualified Text.Blaze.Html5 as H
2827
import qualified Text.Blaze.Html5.Attributes as A
2928
import Text.Read (readMaybe)
30-
import Util.Helpers
29+
import Util.Helpers (returnImageData, safeHead)
3130

3231
gridResponse :: ServerPart Response
3332
gridResponse =

app/Response.hs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,5 @@ module Response (module X) where
22

33
import Response.About as X
44
import Response.Draw as X
5-
import Response.Image as X
65
import Response.Loading as X
76
import Response.NotFound as X

app/Response/Image.hs

Lines changed: 0 additions & 26 deletions
This file was deleted.

app/Util/Helpers.hs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,25 @@
33
Description : Contains general-use helper functions.
44
-}
55
module Util.Helpers
6-
(safeHead) where
6+
(safeHead, returnImageData) where
7+
8+
import qualified Data.ByteString as BS (readFile)
9+
import qualified Data.ByteString.Base64 as BEnc (encode)
10+
import Happstack.Server (Response, toResponse)
11+
import System.Directory (removeFile)
712

813
-- | Given a list and a default value, returns the head of the list, or the default value
914
-- if the list is empty.
1015
safeHead :: a -> [a] -> a
1116
safeHead listHead [] = listHead
1217
safeHead _ (listHead:_) = listHead
18+
19+
-- | Creates and converts an SVG file to an image file, deletes them both and
20+
-- returns the image data as a response.
21+
returnImageData :: String -> String -> IO Response
22+
returnImageData svgFilename imageFilename = do
23+
imageData <- BS.readFile imageFilename
24+
_ <- removeFile imageFilename
25+
_ <- removeFile svgFilename
26+
let encodedData = BEnc.encode imageData
27+
return $ toResponse encodedData

courseography.cabal

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ library
4646
Models.Graph,
4747
Models.Meeting,
4848
Models.Program,
49-
Response.Image,
5049
Scripts,
5150
Svg.Builder,
5251
Svg.Database,
@@ -188,7 +187,6 @@ executable courseography
188187
Response,
189188
Response.About,
190189
Response.Draw,
191-
Response.Image,
192190
Response.Loading,
193191
Response.NotFound,
194192
Routes,

0 commit comments

Comments
 (0)