{-# LANGUAGE CPP #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ViewPatterns #-}
module WaiAppStatic.Listing (
defaultListing,
) where
import qualified Data.Text as T
import Data.Time
import Data.Time.Clock.POSIX
import Text.Blaze ((!))
import qualified Text.Blaze.Html5 as H
import qualified Text.Blaze.Html5.Attributes as A
import WaiAppStatic.Types
#if !MIN_VERSION_time(1,5,0)
import System.Locale (defaultTimeLocale)
#endif
import Data.List (sortBy)
import Util
import qualified Text.Blaze.Html.Renderer.Utf8 as HU
defaultListing :: Listing
defaultListing :: Listing
defaultListing Pieces
pieces (Folder [Either Piece File]
contents) = do
let isTop :: Bool
isTop = Pieces -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null Pieces
pieces Bool -> Bool -> Bool
|| (Piece -> Maybe Piece) -> Pieces -> [Maybe Piece]
forall a b. (a -> b) -> [a] -> [b]
map Piece -> Maybe Piece
forall a. a -> Maybe a
Just Pieces
pieces [Maybe Piece] -> [Maybe Piece] -> Bool
forall a. Eq a => a -> a -> Bool
== [Text -> Maybe Piece
toPiece Text
""]
let fps'' :: [Either FolderName File]
fps'' :: [Either Piece File]
fps'' = (if Bool
isTop then [Either Piece File] -> [Either Piece File]
forall a. a -> a
id else (Piece -> Either Piece File
forall a b. a -> Either a b
Left (Text -> Piece
unsafeToPiece Text
"") Either Piece File -> [Either Piece File] -> [Either Piece File]
forall a. a -> [a] -> [a]
:)) [Either Piece File]
contents
Builder -> IO Builder
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (Builder -> IO Builder) -> Builder -> IO Builder
forall a b. (a -> b) -> a -> b
$
Html -> Builder
HU.renderHtmlBuilder (Html -> Builder) -> Html -> Builder
forall a b. (a -> b) -> a -> b
$
Html -> Html
H.html (Html -> Html) -> Html -> Html
forall a b. (a -> b) -> a -> b
$ do
Html -> Html
H.head (Html -> Html) -> Html -> Html
forall a b. (a -> b) -> a -> b
$ do
let title :: Text
title = Text -> [Text] -> Text
T.intercalate Text
"/" ([Text] -> Text) -> [Text] -> Text
forall a b. (a -> b) -> a -> b
$ (Piece -> Text) -> Pieces -> [Text]
forall a b. (a -> b) -> [a] -> [b]
map Piece -> Text
fromPiece Pieces
pieces
let title' :: Text
title' = if Text -> Bool
T.null Text
title then Text
"root folder" else Text
title
Html -> Html
H.title (Html -> Html) -> Html -> Html
forall a b. (a -> b) -> a -> b
$ Text -> Html
forall a. ToMarkup a => a -> Html
H.toHtml Text
title'
Html -> Html
H.style (Html -> Html) -> Html -> Html
forall a b. (a -> b) -> a -> b
$
[Char] -> Html
forall a. ToMarkup a => a -> Html
H.toHtml ([Char] -> Html) -> [Char] -> Html
forall a b. (a -> b) -> a -> b
$
[[Char]] -> [Char]
unlines
[ [Char]
"table { margin: 0 auto; width: 760px; border-collapse: collapse; font-family: 'sans-serif'; }"
, [Char]
"table, th, td { border: 1px solid #353948; }"
, [Char]
"td.size { text-align: right; font-size: 0.7em; width: 50px }"
, [Char]
"td.date { text-align: right; font-size: 0.7em; width: 130px }"
, [Char]
"td { padding-right: 1em; padding-left: 1em; }"
, [Char]
"th.first { background-color: white; width: 24px }"
, [Char]
"td.first { padding-right: 0; padding-left: 0; text-align: center }"
, [Char]
"tr { background-color: white; }"
, [Char]
"tr.alt { background-color: #A3B5BA}"
, [Char]
"th { background-color: #3C4569; color: white; font-size: 1.125em; }"
, [Char]
"h1 { width: 760px; margin: 1em auto; font-size: 1em; font-family: sans-serif }"
, [Char]
"img { width: 20px }"
, [Char]
"a { text-decoration: none }"
]
Html -> Html
H.body (Html -> Html) -> Html -> Html
forall a b. (a -> b) -> a -> b
$ do
let hasTrailingSlash :: Bool
hasTrailingSlash =
case (Piece -> Text) -> Pieces -> [Text]
forall a b. (a -> b) -> [a] -> [b]
map Piece -> Text
fromPiece (Pieces -> [Text]) -> Pieces -> [Text]
forall a b. (a -> b) -> a -> b
$ Pieces -> Pieces
forall a. [a] -> [a]
reverse Pieces
pieces of
Text
"" : [Text]
_ -> Bool
True
[Text]
_ -> Bool
False
Html -> Html
H.h1 (Html -> Html) -> Html -> Html
forall a b. (a -> b) -> a -> b
$ Bool -> Pieces -> Html
showFolder' Bool
hasTrailingSlash (Pieces -> Html) -> Pieces -> Html
forall a b. (a -> b) -> a -> b
$ (Piece -> Bool) -> Pieces -> Pieces
forall a. (a -> Bool) -> [a] -> [a]
filter (Bool -> Bool
not (Bool -> Bool) -> (Piece -> Bool) -> Piece -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Bool
T.null (Text -> Bool) -> (Piece -> Text) -> Piece -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Piece -> Text
fromPiece) Pieces
pieces
[Text] -> [Char] -> [Char] -> [Either Piece File] -> Html
renderDirectoryContentsTable ((Piece -> Text) -> Pieces -> [Text]
forall a b. (a -> b) -> [a] -> [b]
map Piece -> Text
fromPiece Pieces
pieces) [Char]
haskellSrc [Char]
folderSrc [Either Piece File]
fps''
where
image :: Text -> [Char]
image Text
x = Text -> [Char]
T.unpack (Text -> [Char]) -> Text -> [Char]
forall a b. (a -> b) -> a -> b
$ [Text] -> Text
T.concat [Pieces -> Text
relativeDirFromPieces Pieces
pieces, Text
".hidden/", Text
x, Text
".png"]
folderSrc :: [Char]
folderSrc = Text -> [Char]
image Text
"folder"
haskellSrc :: [Char]
haskellSrc = Text -> [Char]
image Text
"haskell"
showName :: a -> a
showName a
"" = a
"root"
showName a
x = a
x
showFolder' :: Bool -> Pieces -> H.Html
showFolder' :: Bool -> Pieces -> Html
showFolder' Bool
hasTrailingSlash Pieces
pieces' = Bool -> Pieces -> Html
showFolder Bool
hasTrailingSlash (Text -> Piece
unsafeToPiece Text
"root" Piece -> Pieces -> Pieces
forall a. a -> [a] -> [a]
: Pieces
pieces')
showFolder :: Bool -> Pieces -> H.Html
showFolder :: Bool -> Pieces -> Html
showFolder Bool
_ [] = Html
"/"
showFolder Bool
_ [Piece
x] = Text -> Html
forall a. ToMarkup a => a -> Html
H.toHtml (Text -> Html) -> Text -> Html
forall a b. (a -> b) -> a -> b
$ Text -> Text
forall {a}. (Eq a, IsString a) => a -> a
showName (Text -> Text) -> Text -> Text
forall a b. (a -> b) -> a -> b
$ Piece -> Text
fromPiece Piece
x
showFolder Bool
hasTrailingSlash (Piece
x : Pieces
xs) = do
let len :: Int
len = Pieces -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length Pieces
xs Int -> Int -> Int
forall a. Num a => a -> a -> a
- (if Bool
hasTrailingSlash then Int
0 else Int
1)
href :: [Char]
href
| Int
len Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0 = [Char]
"."
| Bool
otherwise = [[Char]] -> [Char]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat ([[Char]] -> [Char]) -> [[Char]] -> [Char]
forall a b. (a -> b) -> a -> b
$ Int -> [Char] -> [[Char]]
forall a. Int -> a -> [a]
replicate Int
len [Char]
"../" :: String
Html -> Html
H.a (Html -> Html) -> Attribute -> Html -> Html
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.href ([Char] -> AttributeValue
forall a. ToValue a => a -> AttributeValue
H.toValue [Char]
href) (Html -> Html) -> Html -> Html
forall a b. (a -> b) -> a -> b
$ Text -> Html
forall a. ToMarkup a => a -> Html
H.toHtml (Text -> Html) -> Text -> Html
forall a b. (a -> b) -> a -> b
$ Text -> Text
forall {a}. (Eq a, IsString a) => a -> a
showName (Text -> Text) -> Text -> Text
forall a b. (a -> b) -> a -> b
$ Piece -> Text
fromPiece Piece
x
Html
" / " :: H.Html
Bool -> Pieces -> Html
showFolder Bool
hasTrailingSlash Pieces
xs
renderDirectoryContentsTable
:: [T.Text]
-> String
-> String
-> [Either FolderName File]
-> H.Html
renderDirectoryContentsTable :: [Text] -> [Char] -> [Char] -> [Either Piece File] -> Html
renderDirectoryContentsTable [Text]
pathInfo' [Char]
haskellSrc [Char]
folderSrc [Either Piece File]
fps =
Html -> Html
H.table (Html -> Html) -> Html -> Html
forall a b. (a -> b) -> a -> b
$ do
Html -> Html
H.thead (Html -> Html) -> Html -> Html
forall a b. (a -> b) -> a -> b
$ do
Html -> Html
H.th (Html -> Html) -> Attribute -> Html -> Html
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.class_ AttributeValue
"first" (Html -> Html) -> Html -> Html
forall a b. (a -> b) -> a -> b
$ Html
H.img Html -> Attribute -> Html
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.src ([Char] -> AttributeValue
forall a. ToValue a => a -> AttributeValue
H.toValue [Char]
haskellSrc)
Html -> Html
H.th Html
"Name"
Html -> Html
H.th Html
"Modified"
Html -> Html
H.th Html
"Size"
Html -> Html
H.tbody (Html -> Html) -> Html -> Html
forall a b. (a -> b) -> a -> b
$ ((Either Piece File, Bool) -> Html)
-> [(Either Piece File, Bool)] -> Html
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ (Either Piece File, Bool) -> Html
mkRow ([Either Piece File] -> [Bool] -> [(Either Piece File, Bool)]
forall a b. [a] -> [b] -> [(a, b)]
zip ((Either Piece File -> Either Piece File -> Ordering)
-> [Either Piece File] -> [Either Piece File]
forall a. (a -> a -> Ordering) -> [a] -> [a]
sortBy Either Piece File -> Either Piece File -> Ordering
sortMD [Either Piece File]
fps) ([Bool] -> [(Either Piece File, Bool)])
-> [Bool] -> [(Either Piece File, Bool)]
forall a b. (a -> b) -> a -> b
$ [Bool] -> [Bool]
forall a. HasCallStack => [a] -> [a]
cycle [Bool
False, Bool
True])
where
sortMD :: Either FolderName File -> Either FolderName File -> Ordering
sortMD :: Either Piece File -> Either Piece File -> Ordering
sortMD Left{} Right{} = Ordering
LT
sortMD Right{} Left{} = Ordering
GT
sortMD (Left Piece
a) (Left Piece
b) = Piece -> Piece -> Ordering
forall a. Ord a => a -> a -> Ordering
compare Piece
a Piece
b
sortMD (Right File
a) (Right File
b) = Piece -> Piece -> Ordering
forall a. Ord a => a -> a -> Ordering
compare (File -> Piece
fileName File
a) (File -> Piece
fileName File
b)
mkRow :: (Either FolderName File, Bool) -> H.Html
mkRow :: (Either Piece File, Bool) -> Html
mkRow (Either Piece File
md, Bool
alt) =
(if Bool
alt then (Html -> Attribute -> Html
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.class_ AttributeValue
"alt") else Html -> Html
forall a. a -> a
id) (Html -> Html) -> Html -> Html
forall a b. (a -> b) -> a -> b
$
Html -> Html
H.tr (Html -> Html) -> Html -> Html
forall a b. (a -> b) -> a -> b
$ do
Html -> Html
H.td (Html -> Html) -> Attribute -> Html -> Html
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.class_ AttributeValue
"first" (Html -> Html) -> Html -> Html
forall a b. (a -> b) -> a -> b
$
case Either Piece File
md of
Left{} ->
Html
H.img
Html -> Attribute -> Html
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.src ([Char] -> AttributeValue
forall a. ToValue a => a -> AttributeValue
H.toValue [Char]
folderSrc)
Html -> Attribute -> Html
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.alt AttributeValue
"Folder"
Right{} -> () -> Html
forall a. a -> MarkupM a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
let name :: Piece
name =
case (Piece -> Piece) -> (File -> Piece) -> Either Piece File -> Piece
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either Piece -> Piece
forall a. a -> a
id File -> Piece
fileName Either Piece File
md of
(Piece -> Text
fromPiece -> Text
"") -> Text -> Piece
unsafeToPiece Text
".."
Piece
x -> Piece
x
let href :: Text
href = Text -> Text
addCurrentDir (Text -> Text) -> Text -> Text
forall a b. (a -> b) -> a -> b
$ Piece -> Text
fromPiece Piece
name
addCurrentDir :: Text -> Text
addCurrentDir Text
x =
case [Text] -> [Text]
forall a. [a] -> [a]
reverse [Text]
pathInfo' of
Text
"" : [Text]
_ -> Text
x
[] -> Text
x
Text
currentDir : [Text]
_ -> [Text] -> Text
T.concat [Text
currentDir, Text
"/", Text
x]
Html -> Html
H.td (Html -> Html
H.a (Html -> Html) -> Attribute -> Html -> Html
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.href (Text -> AttributeValue
forall a. ToValue a => a -> AttributeValue
H.toValue Text
href) (Html -> Html) -> Html -> Html
forall a b. (a -> b) -> a -> b
$ Text -> Html
forall a. ToMarkup a => a -> Html
H.toHtml (Text -> Html) -> Text -> Html
forall a b. (a -> b) -> a -> b
$ Piece -> Text
fromPiece Piece
name)
Html -> Html
H.td (Html -> Html) -> Attribute -> Html -> Html
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.class_ AttributeValue
"date" (Html -> Html) -> Html -> Html
forall a b. (a -> b) -> a -> b
$
[Char] -> Html
forall a. ToMarkup a => a -> Html
H.toHtml ([Char] -> Html) -> [Char] -> Html
forall a b. (a -> b) -> a -> b
$
case Either Piece File
md of
Right File{fileGetModified :: File -> Maybe CTime
fileGetModified = Just CTime
t} ->
TimeLocale -> [Char] -> CTime -> [Char]
forall {a}. Real a => TimeLocale -> [Char] -> a -> [Char]
formatCalendarTime TimeLocale
defaultTimeLocale [Char]
"%d-%b-%Y %X" CTime
t
Either Piece File
_ -> [Char]
""
Html -> Html
H.td (Html -> Html) -> Attribute -> Html -> Html
forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.class_ AttributeValue
"size" (Html -> Html) -> Html -> Html
forall a b. (a -> b) -> a -> b
$
[Char] -> Html
forall a. ToMarkup a => a -> Html
H.toHtml ([Char] -> Html) -> [Char] -> Html
forall a b. (a -> b) -> a -> b
$
case Either Piece File
md of
Right File{fileGetSize :: File -> Integer
fileGetSize = Integer
s} -> Integer -> [Char]
forall {a}. (Show a, Integral a) => a -> [Char]
prettyShow Integer
s
Left{} -> [Char]
""
formatCalendarTime :: TimeLocale -> [Char] -> a -> [Char]
formatCalendarTime TimeLocale
a [Char]
b a
c = TimeLocale -> [Char] -> UTCTime -> [Char]
forall t. FormatTime t => TimeLocale -> [Char] -> t -> [Char]
formatTime TimeLocale
a [Char]
b (UTCTime -> [Char]) -> UTCTime -> [Char]
forall a b. (a -> b) -> a -> b
$ NominalDiffTime -> UTCTime
posixSecondsToUTCTime (a -> NominalDiffTime
forall a b. (Real a, Fractional b) => a -> b
realToFrac a
c :: POSIXTime)
prettyShow :: a -> [Char]
prettyShow a
x
| a
x a -> a -> Bool
forall a. Ord a => a -> a -> Bool
> a
1024 = a -> [Char]
forall {a}. (Show a, Integral a) => a -> [Char]
prettyShowK (a -> [Char]) -> a -> [Char]
forall a b. (a -> b) -> a -> b
$ a
x a -> a -> a
forall a. Integral a => a -> a -> a
`div` a
1024
| Bool
otherwise = [Char] -> a -> [Char]
forall {a}. Show a => [Char] -> a -> [Char]
addCommas [Char]
"B" a
x
prettyShowK :: a -> [Char]
prettyShowK a
x
| a
x a -> a -> Bool
forall a. Ord a => a -> a -> Bool
> a
1024 = a -> [Char]
forall {a}. (Show a, Integral a) => a -> [Char]
prettyShowM (a -> [Char]) -> a -> [Char]
forall a b. (a -> b) -> a -> b
$ a
x a -> a -> a
forall a. Integral a => a -> a -> a
`div` a
1024
| Bool
otherwise = [Char] -> a -> [Char]
forall {a}. Show a => [Char] -> a -> [Char]
addCommas [Char]
"KB" a
x
prettyShowM :: a -> [Char]
prettyShowM a
x
| a
x a -> a -> Bool
forall a. Ord a => a -> a -> Bool
> a
1024 = a -> [Char]
forall {a}. Show a => a -> [Char]
prettyShowG (a -> [Char]) -> a -> [Char]
forall a b. (a -> b) -> a -> b
$ a
x a -> a -> a
forall a. Integral a => a -> a -> a
`div` a
1024
| Bool
otherwise = [Char] -> a -> [Char]
forall {a}. Show a => [Char] -> a -> [Char]
addCommas [Char]
"MB" a
x
prettyShowG :: a -> [Char]
prettyShowG a
x = [Char] -> a -> [Char]
forall {a}. Show a => [Char] -> a -> [Char]
addCommas [Char]
"GB" a
x
addCommas :: [Char] -> a -> [Char]
addCommas [Char]
s = ([Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ (Char
' ' Char -> [Char] -> [Char]
forall a. a -> [a] -> [a]
: [Char]
s)) ([Char] -> [Char]) -> (a -> [Char]) -> a -> [Char]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Char] -> [Char]
forall a. [a] -> [a]
reverse ([Char] -> [Char]) -> (a -> [Char]) -> a -> [Char]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Char] -> [Char]
addCommas' ([Char] -> [Char]) -> (a -> [Char]) -> a -> [Char]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Char] -> [Char]
forall a. [a] -> [a]
reverse ([Char] -> [Char]) -> (a -> [Char]) -> a -> [Char]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> [Char]
forall {a}. Show a => a -> [Char]
show
addCommas' :: [Char] -> [Char]
addCommas' (Char
a : Char
b : Char
c : Char
d : [Char]
e) = Char
a Char -> [Char] -> [Char]
forall a. a -> [a] -> [a]
: Char
b Char -> [Char] -> [Char]
forall a. a -> [a] -> [a]
: Char
c Char -> [Char] -> [Char]
forall a. a -> [a] -> [a]
: Char
',' Char -> [Char] -> [Char]
forall a. a -> [a] -> [a]
: [Char] -> [Char]
addCommas' (Char
d Char -> [Char] -> [Char]
forall a. a -> [a] -> [a]
: [Char]
e)
addCommas' [Char]
x = [Char]
x