viddl/src/Main.hs

52 lines
1.3 KiB
Haskell
Raw Permalink Normal View History

2021-06-24 12:24:07 +02:00
{-# LANGUAGE OverloadedStrings #-}
module Main where
import Templates.Index
import Templates.Error
2021-07-02 14:45:20 +02:00
import YTDL
import Helpers
2021-07-06 16:25:55 +02:00
import Config
2021-07-06 16:25:55 +02:00
import Control.Monad.Trans.Reader
import Control.Monad.Trans.Class
2021-06-24 12:24:07 +02:00
import qualified Data.Text.Lazy as TL
2021-07-06 16:25:55 +02:00
import System.Environment
import Web.Scotty.Trans
2021-07-06 16:25:55 +02:00
type Scotty = ScottyT TL.Text (ReaderT ViddlConfig IO)
type Action = ActionT TL.Text (ReaderT ViddlConfig IO)
safeDownloadAction :: Action ()
2021-07-04 17:04:39 +02:00
safeDownloadAction = downloadAction `rescue` (html . errorPage)
2021-06-24 12:24:07 +02:00
2021-07-06 16:25:55 +02:00
downloadAction :: Action ()
2021-07-04 17:04:39 +02:00
downloadAction = do
2021-06-24 12:24:07 +02:00
url <- param "url"
res <- param "resolution"
if (isURL url) && (isRes res)
then do
2021-07-04 17:04:39 +02:00
let (Just res') = getRes res -- safe cause we checked with isRes
2021-07-06 16:25:55 +02:00
ytdlRes <- lift $ ytdl (TL.unpack url) res'
2021-07-04 17:04:39 +02:00
case ytdlRes of
(Right filePath) -> do
setHeader "content-type" "video/mp4"
file filePath
(Left err) -> html $ errorPage (TL.pack err)
else
html $ errorPage "Invalid input!"
2021-06-24 12:24:07 +02:00
2021-07-04 17:04:39 +02:00
-- todo ReaderT
2021-07-06 16:25:55 +02:00
app :: Scotty ()
2021-07-04 17:04:39 +02:00
app = do
get "/" $ html indexPage
get "/video.mp4" safeDownloadAction
get "/audio.mp3" safeDownloadAction
2021-06-24 12:24:07 +02:00
main :: IO ()
2021-07-06 16:25:55 +02:00
main = do
args <- getArgs
defCfg <- defConfig
cfg <- parseArgs defCfg args
scottyT (webPort cfg) (\(ReaderT ma) -> ma cfg) app