forked from astro/bitlove-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPathPieces.hs
103 lines (84 loc) · 2.83 KB
/
PathPieces.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
{-# LANGUAGE OverloadedStrings #-}
module PathPieces where
import Prelude
import Yesod (PathPiece (..))
import qualified Data.Text as T
import Utils
import Model.User
import Model.Token
data Period = PeriodDays Int
| PeriodAll
deriving (Show, Eq, Read, Ord)
instance PathPiece Period where
fromPathPiece text =
case T.unpack text of
"1" -> Just $ PeriodDays 1
"7" -> Just $ PeriodDays 7
"30" -> Just $ PeriodDays 30
"all" -> Just $ PeriodAll
_ -> Nothing
toPathPiece (PeriodDays days) = T.pack $ show days
toPathPiece PeriodAll = "all"
data TorrentName = TorrentName T.Text
deriving (Show, Eq, Read, Ord)
instance PathPiece TorrentName where
fromPathPiece text = do
let extension = ".torrent"
l <- case T.length text - T.length extension of
l' | l' > 0 -> return l'
_ -> Nothing
case T.splitAt l text of
(name, extension')
| extension == extension' ->
return $ TorrentName name
_ -> Nothing
toPathPiece (TorrentName name) = name `T.append` ".torrent"
data StatsPeriod = StatsDay
| StatsWeek
| StatsMonth
| StatsYear
deriving (Show, Eq, Read)
instance PathPiece StatsPeriod where
fromPathPiece "day" = Just StatsDay
fromPathPiece "week" = Just StatsWeek
fromPathPiece "month" = Just StatsMonth
fromPathPiece "year" = Just StatsYear
fromPathPiece _ = Nothing
toPathPiece StatsDay = "day"
toPathPiece StatsWeek = "week"
toPathPiece StatsMonth = "month"
toPathPiece StatsYear = "year"
data StatsJSON = StatsSwarm
| StatsTraffic
| StatsDownloads
deriving (Show, Eq, Read)
instance PathPiece StatsJSON where
fromPathPiece "swarm.json" = Just StatsSwarm
fromPathPiece "traffic.json" = Just StatsTraffic
fromPathPiece "downloads.json" = Just StatsDownloads
fromPathPiece _ = Nothing
toPathPiece StatsSwarm = "swarm.json"
toPathPiece StatsTraffic = "traffic.json"
toPathPiece StatsDownloads = "downloads.json"
instance PathPiece UserName where
fromPathPiece = Just . UserName
toPathPiece (UserName t) = t
instance PathPiece Token where
fromPathPiece = Just . Token . fromHex
toPathPiece = toHex . unToken
data Thumbnail = Thumbnail Int
deriving (Show, Eq, Ord, Read)
instance PathPiece Thumbnail where
fromPathPiece "48x48.png" =
Just $ Thumbnail 48
fromPathPiece "64x64.png" =
Just $ Thumbnail 64
fromPathPiece _ =
Nothing
toPathPiece (Thumbnail size) =
let s = T.pack $ show size
in T.concat [ s
, "x"
, s
, ".png"
]