Skip to content

Commit 386253f

Browse files
committed
put path sanitize function in utils
1 parent 7d53e0f commit 386253f

File tree

6 files changed

+57
-48
lines changed

6 files changed

+57
-48
lines changed

service/src/Handler/Register.hs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,3 @@ checkPassword =
108108
then return $ Left ("Password must be at least 3 characters long" :: Text)
109109
else return $ Right password
110110
)
111-
112-
-- Helper to convert Int to Text
113-
showT :: (Show a) => a -> Text
114-
showT = T.pack . show

service/src/Handler/Replay.hs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,6 @@ getParagraph era source book paragraph = do
2323
[p] -> return p
2424
_ -> error "No paragraph found for this era."
2525

26-
formatDuration :: Int -> Text
27-
formatDuration ms =
28-
let totalSeconds = ms `div` 1000
29-
(minutes, seconds) = totalSeconds `divMod` 60
30-
pad2 n = if n < 10 then "0" <> show n else show n
31-
in pack $ pad2 minutes <> ":" <> pad2 seconds
32-
3326
getReplayR :: Text -> Handler Html
3427
getReplayR replayId = do
3528
Entity _ replay <- runDB $ getBy404 $ UniqueUuid replayId

service/src/Handler/SaveReplay.hs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,6 @@ getUuid = do
9393
d = fromIntegral (w2' .&. 0xFFFFFFFF) :: Word32
9494
pure $ toText (fromWords a b c d)
9595

96-
diffInMilliseconds :: UTCTime -> UTCTime -> Int
97-
diffInMilliseconds t1 t2 =
98-
let diffSeconds = diffUTCTime t1 t2
99-
diffMillis = diffSeconds * 1000
100-
in round diffMillis
101-
10296
postSaveReplayR :: Handler Value
10397
postSaveReplayR = do
10498
userId <- requireAuthId

service/src/Import/NoFoundation.hs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
{-# LANGUAGE CPP #-}
2+
23
module Import.NoFoundation
3-
( module Import
4-
) where
4+
( module Import,
5+
)
6+
where
57

6-
import ClassyPrelude.Yesod as Import
7-
import Model as Import
8-
import Settings as Import
9-
import Settings.StaticFiles as Import
10-
import Yesod.Auth as Import
11-
import Yesod.Core.Types as Import (loggerSet)
8+
import ClassyPrelude.Yesod as Import
9+
import Import.Utils as Import
10+
import Model as Import
11+
import Settings as Import
12+
import Settings.StaticFiles as Import
13+
import Yesod.Auth as Import
14+
import Yesod.Core.Types as Import (loggerSet)
1215
import Yesod.Default.Config2 as Import

service/src/Import/Utils.hs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
{-# LANGUAGE FlexibleInstances #-}
2+
3+
module Import.Utils where
4+
5+
import ClassyPrelude.Yesod (isInfixOf)
6+
import Data.Text (Text, pack)
7+
import Data.Time.Clock (UTCTime, diffUTCTime)
8+
import System.FilePath.Posix (joinPath, normalise, splitPath)
9+
10+
diffInMilliseconds :: UTCTime -> UTCTime -> Int
11+
diffInMilliseconds t1 t2 =
12+
let diffSeconds = diffUTCTime t1 t2
13+
diffMillis = diffSeconds * 1000
14+
in round diffMillis
15+
16+
formatDuration :: Int -> Text
17+
formatDuration ms =
18+
let totalSeconds = ms `div` 1000
19+
(minutes, seconds) = totalSeconds `divMod` 60
20+
pad2 n = if n < 10 then "0" <> show n else show n
21+
in pack $ pad2 minutes <> ":" <> pad2 seconds
22+
23+
class PathSanitizer a where
24+
takeBaseName :: a -> FilePath
25+
26+
instance PathSanitizer FilePath where
27+
takeBaseName = sanitizePath
28+
29+
sanitizePath :: FilePath -> FilePath
30+
sanitizePath p =
31+
let (s, suffix) = case span (== '/') p of
32+
(sl@('/' : '/' : _), rest) -> (sl, rest)
33+
_ -> ("", dropWhile (== '/') p)
34+
parts =
35+
filter
36+
( \x ->
37+
x `notElem` ["./", "../"]
38+
&& not (".." `isInfixOf` x)
39+
)
40+
. splitPath
41+
. normalise
42+
$ suffix
43+
in s ++ joinPath parts
44+
45+
showT :: (Show a) => a -> Text
46+
showT = pack . show

service/src/Model.hs

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ module Model where
2020

2121
import ClassyPrelude.Yesod hiding (joinPath)
2222
import Database.Persist.Quasi
23-
import System.FilePath.Posix (joinPath, normalise, splitPath)
2423
import Yesod.Auth.HashDB (HashDBUser (..))
2524

2625
data LobbyStatus = LobbyStatusWaiting | LobbyStatusInProgress | LobbyStatusFinished
@@ -32,28 +31,6 @@ instance ToJSON LobbyStatus
3231

3332
instance FromJSON LobbyStatus
3433

35-
class PathSanitizer a where
36-
takeBaseName :: a -> FilePath
37-
38-
instance PathSanitizer FilePath where
39-
takeBaseName = sanitizePath
40-
41-
sanitizePath :: FilePath -> FilePath
42-
sanitizePath p =
43-
let (s, suffix) = case span (== '/') p of
44-
(sl@('/' : '/' : _), rest) -> (sl, rest)
45-
_ -> ("", dropWhile (== '/') p)
46-
parts =
47-
filter
48-
( \x ->
49-
x `notElem` ["./", "../"]
50-
&& not (".." `isInfixOf` x)
51-
)
52-
. splitPath
53-
. normalise
54-
$ suffix
55-
in s ++ joinPath parts
56-
5734
share
5835
[mkPersist sqlSettings, mkMigrate "migrateAll"]
5936
$(persistFileWith lowerCaseSettings "config/models.persistentmodels")

0 commit comments

Comments
 (0)