2012-11-17 19:30:11 +00:00
|
|
|
{- git-annex assistant webapp configurators for WebDAV remotes
|
|
|
|
-
|
|
|
|
- Copyright 2012 Joey Hess <joey@kitenet.net>
|
|
|
|
-
|
|
|
|
- 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
|
2012-11-17 19:30:11 +00:00
|
|
|
import Assistant.MakeRemote
|
|
|
|
import Assistant.Sync
|
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
|
2012-11-25 18:36:24 +00:00
|
|
|
#endif
|
2012-11-17 19:30:11 +00:00
|
|
|
import qualified Remote
|
|
|
|
import Types.Remote (RemoteConfig)
|
|
|
|
import Types.StandardGroups
|
|
|
|
import Logs.PreferredContent
|
2012-11-17 19:58:27 +00:00
|
|
|
import Logs.Remote
|
2012-11-28 17:31:49 +00:00
|
|
|
import Creds
|
2012-11-17 19:30:11 +00:00
|
|
|
|
|
|
|
import qualified Data.Text as T
|
|
|
|
import qualified Data.Map as M
|
|
|
|
|
2012-11-17 19:58:27 +00:00
|
|
|
webDAVConfigurator :: Widget -> Handler RepHtml
|
2012-12-30 03:10:18 +00:00
|
|
|
webDAVConfigurator = page "Add a WebDAV repository" (Just Configuration)
|
2012-11-17 19:58:27 +00:00
|
|
|
|
2012-11-17 19:30:11 +00:00
|
|
|
boxConfigurator :: Widget -> Handler RepHtml
|
2012-12-30 03:10:18 +00:00
|
|
|
boxConfigurator = page "Add a Box.com repository" (Just Configuration)
|
2012-11-17 19:30:11 +00:00
|
|
|
|
|
|
|
data WebDAVInput = WebDAVInput
|
|
|
|
{ user :: Text
|
|
|
|
, password :: Text
|
2012-11-28 17:31:49 +00:00
|
|
|
, embedCreds :: Bool
|
2012-11-17 19:30:11 +00:00
|
|
|
, directory :: Text
|
2012-12-04 17:28:22 +00:00
|
|
|
, enableEncryption :: EnableEncryption
|
2012-11-17 19:30:11 +00:00
|
|
|
}
|
|
|
|
|
2012-11-28 17:31:49 +00:00
|
|
|
toCredPair :: WebDAVInput -> CredPair
|
|
|
|
toCredPair input = (T.unpack $ user input, T.unpack $ password input)
|
|
|
|
|
2012-11-17 19:30:11 +00:00
|
|
|
boxComAForm :: AForm WebApp WebApp WebDAVInput
|
|
|
|
boxComAForm = WebDAVInput
|
|
|
|
<$> areq textField "Username or Email" Nothing
|
|
|
|
<*> areq passwordField "Box.com Password" Nothing
|
2012-11-28 17:31:49 +00:00
|
|
|
<*> areq checkBoxField "Share this account with friends?" (Just True)
|
2012-11-17 19:30:11 +00:00
|
|
|
<*> areq textField "Directory" (Just "annex")
|
2012-12-04 17:28:22 +00:00
|
|
|
<*> enableEncryptionField
|
2012-11-17 19:30:11 +00:00
|
|
|
|
2012-11-17 19:58:27 +00:00
|
|
|
webDAVCredsAForm :: AForm WebApp WebApp WebDAVInput
|
|
|
|
webDAVCredsAForm = WebDAVInput
|
|
|
|
<$> areq textField "Username or Email" Nothing
|
|
|
|
<*> areq passwordField "Password" Nothing
|
2012-11-28 17:31:49 +00:00
|
|
|
<*> pure False
|
|
|
|
<*> pure T.empty
|
2012-12-04 17:28:22 +00:00
|
|
|
<*> pure NoEncryption -- not used!
|
2012-11-17 19:58:27 +00:00
|
|
|
|
2012-11-17 19:30:11 +00:00
|
|
|
getAddBoxComR :: Handler RepHtml
|
2012-11-25 18:36:24 +00:00
|
|
|
#ifdef WITH_WEBDAV
|
2012-11-17 19:30:11 +00:00
|
|
|
getAddBoxComR = boxConfigurator $ do
|
|
|
|
((result, form), enctype) <- lift $
|
|
|
|
runFormGet $ renderBootstrap boxComAForm
|
|
|
|
case result of
|
|
|
|
FormSuccess input -> lift $
|
2012-11-28 17:31:49 +00:00
|
|
|
makeWebDavRemote "box.com" (toCredPair input) setgroup $ M.fromList
|
2012-12-04 17:28:22 +00:00
|
|
|
[ configureEncryption $ enableEncryption input
|
2012-11-28 17:31:49 +00:00
|
|
|
, ("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 = runAnnex () $
|
|
|
|
setStandardGroup (Remote.uuid r) TransferGroup
|
2012-11-25 18:36:24 +00:00
|
|
|
#else
|
|
|
|
getAddBoxComR = error "WebDAV not supported by this build"
|
|
|
|
#endif
|
2012-11-17 19:30:11 +00:00
|
|
|
|
2012-11-17 19:58:27 +00:00
|
|
|
getEnableWebDAVR :: UUID -> Handler RepHtml
|
2012-11-25 18:36:24 +00:00
|
|
|
#ifdef WITH_WEBDAV
|
2012-11-17 19:58:27 +00:00
|
|
|
getEnableWebDAVR uuid = do
|
|
|
|
m <- runAnnex M.empty readRemoteLog
|
|
|
|
let c = fromJust $ M.lookup uuid m
|
|
|
|
let name = fromJust $ M.lookup "name" c
|
|
|
|
let url = fromJust $ M.lookup "url" c
|
2012-11-28 17:31:49 +00:00
|
|
|
mcreds <- runAnnex Nothing $
|
|
|
|
getRemoteCredPairFor "webdav" c (WebDAV.davCreds uuid)
|
|
|
|
case mcreds of
|
|
|
|
Just creds -> webDAVConfigurator $ lift $
|
|
|
|
makeWebDavRemote name creds (const noop) M.empty
|
|
|
|
Nothing
|
|
|
|
| "box.com/" `isInfixOf` url ->
|
|
|
|
boxConfigurator $ showform name url
|
|
|
|
| otherwise ->
|
|
|
|
webDAVConfigurator $ showform name url
|
2012-11-17 19:58:27 +00:00
|
|
|
where
|
2012-11-28 17:31:49 +00:00
|
|
|
showform name url = do
|
2012-11-17 19:58:27 +00:00
|
|
|
((result, form), enctype) <- lift $
|
|
|
|
runFormGet $ renderBootstrap webDAVCredsAForm
|
|
|
|
case result of
|
2012-11-28 17:31:49 +00:00
|
|
|
FormSuccess input -> lift $
|
|
|
|
makeWebDavRemote name (toCredPair input) (const noop) M.empty
|
2012-11-17 19:58:27 +00:00
|
|
|
_ -> do
|
|
|
|
description <- lift $ runAnnex "" $
|
|
|
|
T.pack . concat <$> Remote.prettyListUUIDs [uuid]
|
|
|
|
$(widgetFile "configurators/enablewebdav")
|
2012-11-25 18:36:24 +00:00
|
|
|
#else
|
|
|
|
getEnableWebDAVR _ = error "WebDAV not supported by this build"
|
|
|
|
#endif
|
2012-11-17 19:58:27 +00:00
|
|
|
|
2012-11-25 18:36:24 +00:00
|
|
|
#ifdef WITH_WEBDAV
|
2012-11-28 17:31:49 +00:00
|
|
|
makeWebDavRemote :: String -> CredPair -> (Remote -> Handler ()) -> RemoteConfig -> Handler ()
|
|
|
|
makeWebDavRemote name creds setup config = do
|
2012-11-17 19:30:11 +00:00
|
|
|
remotename <- runAnnex name $ fromRepo $ uniqueRemoteName name 0
|
2012-11-28 17:31:49 +00:00
|
|
|
liftIO $ WebDAV.setCredsEnv creds
|
2012-11-17 19:30:11 +00:00
|
|
|
r <- liftAssistant $ liftAnnex $ addRemote $ do
|
|
|
|
makeSpecialRemote name WebDAV.remote config
|
|
|
|
return remotename
|
|
|
|
setup r
|
|
|
|
liftAssistant $ syncNewRemote r
|
|
|
|
redirect $ EditNewCloudRepositoryR $ Remote.uuid r
|
2012-11-25 18:36:24 +00:00
|
|
|
#endif
|