2012-11-17 19:30:11 +00:00
|
|
|
{- git-annex assistant webapp configurators for WebDAV remotes
|
|
|
|
-
|
2013-04-27 19:16:06 +00:00
|
|
|
- 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.
|
|
|
|
-}
|
|
|
|
|
2013-10-02 05:06:59 +00:00
|
|
|
{-# LANGUAGE CPP, TemplateHaskell, OverloadedStrings #-}
|
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-10-28 15:33:14 +00:00
|
|
|
import Assistant.WebApp.MakeRemote
|
2012-11-17 19:30:11 +00:00
|
|
|
import qualified Remote
|
|
|
|
import Types.Remote (RemoteConfig)
|
|
|
|
import Types.StandardGroups
|
2012-11-17 19:58:27 +00:00
|
|
|
import Logs.Remote
|
2013-11-07 22:02:00 +00:00
|
|
|
import Git.Types (RemoteName)
|
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
|
2013-04-27 19:16:06 +00:00
|
|
|
import Network.URI
|
2013-10-13 18:19:58 +00:00
|
|
|
import Assistant.Gpg
|
2012-11-17 19:30:11 +00:00
|
|
|
|
2013-06-27 05:15:28 +00:00
|
|
|
webDAVConfigurator :: Widget -> Handler Html
|
2012-12-30 03:10:18 +00:00
|
|
|
webDAVConfigurator = page "Add a WebDAV repository" (Just Configuration)
|
2012-11-17 19:58:27 +00:00
|
|
|
|
2013-06-27 05:15:28 +00:00
|
|
|
boxConfigurator :: Widget -> Handler Html
|
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)
|
|
|
|
|
2013-06-03 20:33:05 +00:00
|
|
|
boxComAForm :: Maybe CredPair -> MkAForm WebDAVInput
|
2013-04-27 19:16:06 +00:00
|
|
|
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")
|
2012-12-04 17:28:22 +00:00
|
|
|
<*> enableEncryptionField
|
2012-11-17 19:30:11 +00:00
|
|
|
|
2013-06-03 20:33:05 +00:00
|
|
|
webDAVCredsAForm :: Maybe CredPair -> MkAForm WebDAVInput
|
2013-04-27 19:16:06 +00:00
|
|
|
webDAVCredsAForm defcreds = WebDAVInput
|
|
|
|
<$> areq textField "Username or Email" (T.pack . fst <$> defcreds)
|
|
|
|
<*> areq passwordField "Password" (T.pack . snd <$> defcreds)
|
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
|
|
|
|
2013-06-27 05:15:28 +00:00
|
|
|
getAddBoxComR :: Handler Html
|
2013-03-16 22:48:23 +00:00
|
|
|
getAddBoxComR = postAddBoxComR
|
2013-06-27 05:15:28 +00:00
|
|
|
postAddBoxComR :: Handler Html
|
2012-11-25 18:36:24 +00:00
|
|
|
#ifdef WITH_WEBDAV
|
2013-03-16 22:48:23 +00:00
|
|
|
postAddBoxComR = boxConfigurator $ do
|
2013-04-27 19:16:06 +00:00
|
|
|
defcreds <- liftAnnex $ previouslyUsedWebDAVCreds "box.com"
|
2013-06-03 17:51:54 +00:00
|
|
|
((result, form), enctype) <- liftH $
|
2013-10-14 16:19:11 +00:00
|
|
|
runFormPostNoToken $ renderBootstrap $ boxComAForm defcreds
|
2012-11-17 19:30:11 +00:00
|
|
|
case result of
|
2013-06-03 17:51:54 +00:00
|
|
|
FormSuccess input -> liftH $
|
2013-09-27 04:15:50 +00:00
|
|
|
makeWebDavRemote initSpecialRemote "box.com" (toCredPair input) $ 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-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
|
|
|
|
2013-06-27 05:15:28 +00:00
|
|
|
getEnableWebDAVR :: UUID -> Handler Html
|
2013-03-16 22:48:23 +00:00
|
|
|
getEnableWebDAVR = postEnableWebDAVR
|
2013-06-27 05:15:28 +00:00
|
|
|
postEnableWebDAVR :: UUID -> Handler Html
|
2012-11-25 18:36:24 +00:00
|
|
|
#ifdef WITH_WEBDAV
|
2013-03-16 22:48:23 +00:00
|
|
|
postEnableWebDAVR uuid = do
|
2013-03-04 20:36:38 +00:00
|
|
|
m <- liftAnnex readRemoteLog
|
2012-11-17 19:58:27 +00:00
|
|
|
let c = fromJust $ M.lookup uuid m
|
|
|
|
let name = fromJust $ M.lookup "name" c
|
|
|
|
let url = fromJust $ M.lookup "url" c
|
2013-03-04 20:36:38 +00:00
|
|
|
mcreds <- liftAnnex $
|
2012-11-28 17:31:49 +00:00
|
|
|
getRemoteCredPairFor "webdav" c (WebDAV.davCreds uuid)
|
|
|
|
case mcreds of
|
2013-06-03 17:51:54 +00:00
|
|
|
Just creds -> webDAVConfigurator $ liftH $
|
2013-09-27 04:15:50 +00:00
|
|
|
makeWebDavRemote enableSpecialRemote name creds M.empty
|
2012-11-28 17:31:49 +00:00
|
|
|
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
|
2013-04-27 19:16:06 +00:00
|
|
|
defcreds <- liftAnnex $
|
|
|
|
maybe (pure Nothing) previouslyUsedWebDAVCreds $
|
|
|
|
urlHost url
|
2013-06-03 17:51:54 +00:00
|
|
|
((result, form), enctype) <- liftH $
|
2013-10-14 16:19:11 +00:00
|
|
|
runFormPostNoToken $ renderBootstrap $ webDAVCredsAForm defcreds
|
2012-11-17 19:58:27 +00:00
|
|
|
case result of
|
2013-06-03 17:51:54 +00:00
|
|
|
FormSuccess input -> liftH $
|
2013-09-27 04:15:50 +00:00
|
|
|
makeWebDavRemote enableSpecialRemote name (toCredPair input) M.empty
|
2012-11-17 19:58:27 +00:00
|
|
|
_ -> do
|
2013-03-16 04:12:28 +00:00
|
|
|
description <- liftAnnex $
|
2013-04-03 21:01:40 +00:00
|
|
|
T.pack <$> Remote.prettyUUID uuid
|
2012-11-17 19:58:27 +00:00
|
|
|
$(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-17 19:58:27 +00:00
|
|
|
|
2012-11-25 18:36:24 +00:00
|
|
|
#ifdef WITH_WEBDAV
|
2013-09-27 04:15:50 +00:00
|
|
|
makeWebDavRemote :: SpecialRemoteMaker -> RemoteName -> CredPair -> RemoteConfig -> Handler ()
|
|
|
|
makeWebDavRemote maker name creds config = do
|
2012-11-28 17:31:49 +00:00
|
|
|
liftIO $ WebDAV.setCredsEnv creds
|
2013-09-29 18:39:10 +00:00
|
|
|
setupCloudRemote TransferGroup Nothing $
|
|
|
|
maker name WebDAV.remote config
|
2013-04-27 19:16:06 +00:00
|
|
|
|
|
|
|
{- 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
|
2013-04-27 19:16:06 +00:00
|
|
|
|
|
|
|
urlHost :: String -> Maybe String
|
|
|
|
urlHost url = uriRegName <$> (uriAuthority =<< parseURI url)
|