-- Handlers --------------------------------------------------------------------

-- | Shows the home page with the upload form.
getUploadR :: Handler Html
getUploadR = do
    ((filesWidget, optsWidget), enctype) <- generateFormPost uploadForm

    topImages <- runDB getTopImages

    extras <- getExtra
    let maxFileSize    = extraMaxFileSize extras
    let maxRequestSize = extraMaxRequestSize extras

    defaultLayout $ do
        setTitle "Upload a file | getwebb"
        $(widgetFile "upload")
  where
    -- Searchs the ten most popular images. Returns a list of uploads and the
    -- URL to their miniatures.
    getTopImages = do
        let sql = T.unlines [
                  "SELECT ??"
                , "FROM Upload AS upload"
                , "INNER JOIN File AS f ON f.id = upload.file"
                , "WHERE f.type = 'Image' AND upload.public = 1"
                , "ORDER BY upload.score DESC"
                , "LIMIT 40;"
                ]
        imgs <- rawSql sql []

        return [ (i, DownloadMiniatureR (uploadHmac i)) | Entity _ i <- imgs ]

-- | Uploads a file to the server. Returns a 201 Created with a JSON object
-- which contains the id of the upload, or a 400/413/429 with a JSON array of
-- errors.
getUploadR :: Handler Html
getUploadR = do
    ((filesWidget, optsWidget), enctype) <- generateFormPost uploadForm

    topImages <- runDB getTopImages

    extras <- getExtra
    let maxFileSize    = extraMaxFileSize extras
    let maxRequestSize = extraMaxRequestSize extras

    defaultLayout $ do
        setTitle "Upload a file | getwebb"
        $(widgetFile "upload")
  where
    -- Searchs the ten most popular images. Returns a list of uploads and the
    -- URL to their miniatures.
    getTopImages = do
        let sql = T.unlines [
                  "SELECT ??"
                , "FROM Upload AS upload"
                , "INNER JOIN File AS f ON f.id = upload.file"
                , "WHERE f.type = 'Image' AND upload.public = 1"
                , "ORDER BY upload.score DESC"
                , "LIMIT 40;"
                ]
        imgs <- rawSql sql []

        return [ (i, DownloadMiniatureR (uploadHmac i)) | Entity _ i <- imgs ]

-- | Uploads a file to the server. Returns a 201 Created with a JSON object
-- which contains the id of the upload, or a 400/413/429 with a JSON array of
-- errors.
getUploadR = do
    ((filesWidget, optsWidget), enctype) <- generateFormPost uploadForm

    topImages <- runDB getTopImages

    extras <- getExtra
    let maxFileSize    = extraMaxFileSize extras
    let maxRequestSize = extraMaxRequestSize extras

    defaultLayout $ do
        setTitle "Upload a file | getwebb"
        $(widgetFile "upload")
  where
    -- Searchs the ten most popular images. Returns a list of uploads and the
    -- URL to their miniatures.
    getTopImages = do
        let sql = T.unlines [
                  "SELECT ??"
                , "FROM Upload AS upload"
                , "INNER JOIN File AS f ON f.id = upload.file"
                , "WHERE f.type = 'Image' AND upload.public = 1"
                , "ORDER BY upload.score DESC"
                , "LIMIT 40;"
                ]
        imgs <- rawSql sql []

        return [ (i, DownloadMiniatureR (uploadHmac i)) | Entity _ i <- imgs ]

-- | Uploads a file to the server. Returns a 201 Created with a JSON object
-- which contains the id of the upload, or a 400/413/429 with a JSON array of
-- errors.