git-annex/Assistant/WebApp/Configurators/WebDAV.hs

151 lines
4.9 KiB
Haskell
Raw Normal View History

2012-11-17 19:30:11 +00:00
{- git-annex assistant webapp configurators for WebDAV remotes
-
- Copyright 2012, 2013 Joey Hess <joey@kitenet.net>
2012-11-17 19:30:11 +00:00
-
- Licensed under the GNU AGPL version 3 or higher.
-}
2012-11-25 18:36:24 +00:00
{-# LANGUAGE CPP, TypeFamilies, QuasiQuotes, MultiParamTypeClasses, TemplateHaskell, OverloadedStrings, RankNTypes #-}
2012-11-17 19:30:11 +00:00
module Assistant.WebApp.Configurators.WebDAV where
2012-11-25 04:26:46 +00:00
import Assistant.WebApp.Common
2013-04-20 23:25:50 +00:00
import Creds
2012-11-25 18:36:24 +00:00
#ifdef WITH_WEBDAV
2012-11-17 19:30:11 +00:00
import qualified Remote.WebDAV as WebDAV
2013-04-20 23:25:50 +00:00
import Assistant.MakeRemote
import Assistant.Sync
2012-11-17 19:30:11 +00:00
import qualified Remote
import Types.Remote (RemoteConfig)
import Types.StandardGroups
import Logs.PreferredContent
import Logs.Remote
2012-11-17 19:30:11 +00:00
import qualified Data.Map as M
2013-04-20 23:25:50 +00:00
#endif
import qualified Data.Text as T
import Network.URI
2012-11-17 19:30:11 +00:00
webDAVConfigurator :: Widget -> Handler RepHtml
webDAVConfigurator = page "Add a WebDAV repository" (Just Configuration)
2012-11-17 19:30:11 +00:00
boxConfigurator :: Widget -> Handler RepHtml
boxConfigurator = page "Add a Box.com repository" (Just Configuration)
2012-11-17 19:30:11 +00:00
data WebDAVInput = WebDAVInput
{ user :: Text
, password :: Text
, embedCreds :: Bool
2012-11-17 19:30:11 +00:00
, directory :: Text
, enableEncryption :: EnableEncryption
2012-11-17 19:30:11 +00:00
}
toCredPair :: WebDAVInput -> CredPair
toCredPair input = (T.unpack $ user input, T.unpack $ password input)
2013-06-03 20:33:05 +00:00
boxComAForm :: Maybe CredPair -> MkAForm WebDAVInput
boxComAForm defcreds = WebDAVInput
<$> areq textField "Username or Email" (T.pack . fst <$> defcreds)
<*> areq passwordField "Box.com Password" (T.pack . snd <$> defcreds)
2013-03-27 19:46:08 +00:00
<*> areq checkBoxField "Share this account with other devices and friends?" (Just True)
2012-11-17 19:30:11 +00:00
<*> areq textField "Directory" (Just "annex")
<*> enableEncryptionField
2012-11-17 19:30:11 +00:00
2013-06-03 20:33:05 +00:00
webDAVCredsAForm :: Maybe CredPair -> MkAForm WebDAVInput
webDAVCredsAForm defcreds = WebDAVInput
<$> areq textField "Username or Email" (T.pack . fst <$> defcreds)
<*> areq passwordField "Password" (T.pack . snd <$> defcreds)
<*> pure False
<*> pure T.empty
<*> pure NoEncryption -- not used!
2012-11-17 19:30:11 +00:00
getAddBoxComR :: Handler RepHtml
2013-03-16 22:48:23 +00:00
getAddBoxComR = postAddBoxComR
postAddBoxComR :: Handler RepHtml
2012-11-25 18:36:24 +00:00
#ifdef WITH_WEBDAV
2013-03-16 22:48:23 +00:00
postAddBoxComR = boxConfigurator $ do
defcreds <- liftAnnex $ previouslyUsedWebDAVCreds "box.com"
((result, form), enctype) <- liftH $
runFormPost $ renderBootstrap $ boxComAForm defcreds
2012-11-17 19:30:11 +00:00
case result of
FormSuccess input -> liftH $
makeWebDavRemote "box.com" (toCredPair input) setgroup $ M.fromList
[ configureEncryption $ enableEncryption input
, ("embedcreds", if embedCreds input then "yes" else "no")
2012-11-17 19:30:11 +00:00
, ("type", "webdav")
, ("url", "https://www.box.com/dav/" ++ T.unpack (directory input))
-- Box.com has a max file size of 100 mb, but
-- using smaller chunks has better memory
-- performance.
, ("chunksize", "10mb")
]
2012-11-25 04:38:11 +00:00
_ -> $(widgetFile "configurators/addbox.com")
2012-11-17 19:30:11 +00:00
where
setgroup r = liftAnnex $
2012-11-17 19:30:11 +00:00
setStandardGroup (Remote.uuid r) TransferGroup
2012-11-25 18:36:24 +00:00
#else
2013-03-16 22:48:23 +00:00
postAddBoxComR = error "WebDAV not supported by this build"
2012-11-25 18:36:24 +00:00
#endif
2012-11-17 19:30:11 +00:00
getEnableWebDAVR :: UUID -> Handler RepHtml
2013-03-16 22:48:23 +00:00
getEnableWebDAVR = postEnableWebDAVR
postEnableWebDAVR :: UUID -> Handler RepHtml
2012-11-25 18:36:24 +00:00
#ifdef WITH_WEBDAV
2013-03-16 22:48:23 +00:00
postEnableWebDAVR uuid = do
m <- liftAnnex readRemoteLog
let c = fromJust $ M.lookup uuid m
let name = fromJust $ M.lookup "name" c
let url = fromJust $ M.lookup "url" c
mcreds <- liftAnnex $
getRemoteCredPairFor "webdav" c (WebDAV.davCreds uuid)
case mcreds of
Just creds -> webDAVConfigurator $ liftH $
makeWebDavRemote name creds (const noop) M.empty
Nothing
| "box.com/" `isInfixOf` url ->
boxConfigurator $ showform name url
| otherwise ->
webDAVConfigurator $ showform name url
where
showform name url = do
defcreds <- liftAnnex $
maybe (pure Nothing) previouslyUsedWebDAVCreds $
urlHost url
((result, form), enctype) <- liftH $
runFormPost $ renderBootstrap $ webDAVCredsAForm defcreds
case result of
FormSuccess input -> liftH $
makeWebDavRemote name (toCredPair input) (const noop) M.empty
_ -> do
description <- liftAnnex $
T.pack <$> Remote.prettyUUID uuid
$(widgetFile "configurators/enablewebdav")
2012-11-25 18:36:24 +00:00
#else
2013-03-16 22:48:23 +00:00
postEnableWebDAVR _ = error "WebDAV not supported by this build"
2012-11-25 18:36:24 +00:00
#endif
2012-11-25 18:36:24 +00:00
#ifdef WITH_WEBDAV
makeWebDavRemote :: String -> CredPair -> (Remote -> Handler ()) -> RemoteConfig -> Handler ()
makeWebDavRemote name creds setup config = do
remotename <- liftAnnex $ fromRepo $ uniqueRemoteName name 0
liftIO $ WebDAV.setCredsEnv creds
r <- liftAnnex $ addRemote $ do
2012-11-17 19:30:11 +00:00
makeSpecialRemote name WebDAV.remote config
return remotename
setup r
liftAssistant $ syncRemote r
2012-11-17 19:30:11 +00:00
redirect $ EditNewCloudRepositoryR $ Remote.uuid r
{- Only returns creds previously used for the same hostname. -}
previouslyUsedWebDAVCreds :: String -> Annex (Maybe CredPair)
previouslyUsedWebDAVCreds hostname =
previouslyUsedCredPair WebDAV.davCreds WebDAV.remote samehost
where
samehost url = case urlHost =<< WebDAV.configUrl url of
Nothing -> False
Just h -> h == hostname
2013-05-08 16:20:10 +00:00
#endif
urlHost :: String -> Maybe String
urlHost url = uriRegName <$> (uriAuthority =<< parseURI url)