1 {-# LANGUAGE OverloadedStrings #-}
    2 module Databrary.Controller.Format
    3   ( viewFormatsHandler
    4   , formatIcon
    5   ) where
    6 
    7 import Control.Monad.Reader (asks)
    8 import qualified Data.Invertible as I
    9 import Data.Monoid ((<>))
   10 import System.Posix.FilePath (splitFileName, splitExtension)
   11 import qualified Web.Route.Invertible as R
   12 
   13 import Databrary.Model.Format
   14 import Databrary.Action.Run
   15 import Databrary.Action
   16 import Databrary.Controller.Web
   17 import Databrary.Controller.Angular
   18 import Databrary.View.Format
   19 
   20 formatIcon :: ActionRoute Format
   21 formatIcon = (pf I.:<->: fp) `R.mapActionRoute` webFile where
   22   fp f = Just $ staticPath
   23     [ "images", "filetype", "16px"
   24     , case formatExtension f of { e:_ -> e ; _ -> "_blank" } <> ".svg"
   25     ]
   26   pf (Just (StaticPath p))
   27     | ("images/filetype/16px/", i) <- splitFileName p
   28     , (e, ".svg") <- splitExtension i
   29     , Just f <- getFormatByExtension e = f
   30   pf _ = unknownFormat
   31 
   32 viewFormatsHandler :: Action -- TODO: GET only
   33 viewFormatsHandler = withoutAuth $ do
   34   angular
   35   okResponse [] <$> asks htmlFormats