diff --git a/src/Clients.hs b/src/Clients.hs deleted file mode 100644 index 74aee34..0000000 --- a/src/Clients.hs +++ /dev/null @@ -1,33 +0,0 @@ -{-# LANGUAGE OverloadedStrings #-} - -module Clients where - -import Control.Monad (replicateM) -import System.Random (randomRIO) -import qualified Database.Redis as R -import qualified Data.ByteString.Char8 as BC - -idIsFree :: BC.ByteString -> R.Connection -> IO Bool -idIsFree id rConn = do - response <- R.runRedis rConn (R.get id) - pure $ case response of - (Left _) -> False -- maybe error here? look into what causes this more - (Right x) -> - case x of - (Just _) -> True - _ -> False - -genClientId :: IO String -genClientId = replicateM 8 (randomRIO ('a', 'z')) - --- todo: config file -maxClients :: Int -maxClients = 100 - -getClients :: IO Int -getClients = undefined - -acceptingClients :: IO Bool -acceptingClients = do - clients <- getClients - pure $ clients < maxClients diff --git a/src/Helpers.hs b/src/Helpers.hs index 8d9de32..7d666f6 100644 --- a/src/Helpers.hs +++ b/src/Helpers.hs @@ -1,10 +1,12 @@ +{-# LANGUAGE OverloadedStrings #-} + module Helpers where import YTDL import qualified Data.Text.Lazy as TL import Network.URI (parseURI) -getRes :: String -> Maybe Resolution +getRes :: TL.Text -> Maybe Resolution getRes ("144p") = Just P144 getRes ("240p") = Just P240 getRes ("360p") = Just P360 @@ -16,7 +18,7 @@ getRes ("audio") = Just Audio getRes _ = Nothing isRes :: TL.Text -> Bool -isRes res = case getRes (TL.unpack res) of +isRes res = case getRes res of (Just _) -> True _ -> False diff --git a/src/Main.hs b/src/Main.hs index 91add33..3d5a504 100644 --- a/src/Main.hs +++ b/src/Main.hs @@ -3,46 +3,40 @@ module Main where import Templates.Index -import Templates.Loading import Templates.Error import YTDL -import Clients import Helpers import Control.Monad.IO.Class -import qualified Database.Redis as R import qualified Data.Text.Lazy as TL import Web.Scotty --- todo ReaderT -app :: R.Connection -> ScottyM () -app rConn = do - get "/" $ do - html indexPage +safeDownloadAction :: ActionM () +safeDownloadAction = downloadAction `rescue` (html . errorPage) - post "/" $ do +downloadAction :: ActionM () +downloadAction = do url <- param "url" res <- param "resolution" if (isURL url) && (isRes res) then do - queueOK <- liftIO acceptingClients - if queueOK - then do - html loadingPage - -- set redis stuff and id here - -- redirect $ TL.pack $ '/':id - else - html $ errorPage "Too many clients right now. Try again later!" - else + let (Just res') = getRes res -- safe cause we checked with isRes + ytdlRes <- liftIO $ ytdl (TL.unpack url) res' + case ytdlRes of + (Right filePath) -> do + setHeader "content-type" "video/mp4" + file filePath + (Left err) -> html $ errorPage (TL.pack err) + -- liftIO $ ytdlClean ident + else html $ errorPage "Invalid input!" - get "/:id" $ do - -- grab id and process video if not already done - id <- param "id" - html id +-- todo ReaderT +app :: ScottyM () +app = do + get "/" $ html indexPage + get "/video.mp4" safeDownloadAction + get "/audio.mp3" safeDownloadAction main :: IO () -main = do - -- todo: parse connection config - rConn <- R.connect R.defaultConnectInfo - scotty 3000 (app rConn) +main = scotty 3000 app diff --git a/src/Templates/Index.hs b/src/Templates/Index.hs index 856e1dc..da3b731 100644 --- a/src/Templates/Index.hs +++ b/src/Templates/Index.hs @@ -22,7 +22,7 @@ indexPage = [r|