2012-11-15 00:25:00 +00:00
|
|
|
{- WebDAV remotes.
|
|
|
|
-
|
2014-02-11 18:06:50 +00:00
|
|
|
- Copyright 2012-2014 Joey Hess <joey@kitenet.net>
|
2012-11-15 00:25:00 +00:00
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
2014-07-30 15:19:05 +00:00
|
|
|
{-# LANGUAGE ScopedTypeVariables #-}
|
2013-02-27 04:07:28 +00:00
|
|
|
|
2014-02-11 18:06:50 +00:00
|
|
|
module Remote.WebDAV (remote, davCreds, configUrl) where
|
2012-11-15 00:25:00 +00:00
|
|
|
|
|
|
|
import Network.Protocol.HTTP.DAV
|
|
|
|
import qualified Data.Map as M
|
2012-11-15 17:46:16 +00:00
|
|
|
import qualified Data.ByteString.UTF8 as B8
|
2012-11-16 21:58:58 +00:00
|
|
|
import qualified Data.ByteString.Lazy.UTF8 as L8
|
2012-11-15 00:25:00 +00:00
|
|
|
import qualified Data.ByteString.Lazy as L
|
2012-11-16 04:09:22 +00:00
|
|
|
import qualified Control.Exception as E
|
2014-02-24 22:21:51 +00:00
|
|
|
import qualified Control.Exception.Lifted as EL
|
2014-02-25 05:55:01 +00:00
|
|
|
import Network.HTTP.Client (HttpException(..))
|
2012-11-16 04:09:22 +00:00
|
|
|
import Network.HTTP.Types
|
2014-02-25 19:35:45 +00:00
|
|
|
import System.Log.Logger (debugM)
|
2012-11-17 03:16:18 +00:00
|
|
|
import System.IO.Error
|
2012-11-15 00:25:00 +00:00
|
|
|
|
2014-02-25 20:09:50 +00:00
|
|
|
import Common.Annex
|
2012-11-15 00:25:00 +00:00
|
|
|
import Types.Remote
|
|
|
|
import qualified Git
|
|
|
|
import Config
|
2013-03-13 20:16:01 +00:00
|
|
|
import Config.Cost
|
2012-11-15 00:25:00 +00:00
|
|
|
import Remote.Helper.Special
|
|
|
|
import Remote.Helper.Encryptable
|
2014-07-24 18:49:22 +00:00
|
|
|
import qualified Remote.Helper.Chunked.Legacy as Legacy
|
2012-11-15 00:25:00 +00:00
|
|
|
import Crypto
|
|
|
|
import Creds
|
2013-03-28 21:03:04 +00:00
|
|
|
import Utility.Metered
|
2013-01-06 18:29:01 +00:00
|
|
|
import Annex.Content
|
2013-09-07 22:38:00 +00:00
|
|
|
import Annex.UUID
|
2014-02-25 20:09:50 +00:00
|
|
|
import Remote.WebDAV.DavUrl
|
2012-11-15 00:25:00 +00:00
|
|
|
|
2012-11-15 17:46:16 +00:00
|
|
|
type DavUser = B8.ByteString
|
|
|
|
type DavPass = B8.ByteString
|
|
|
|
|
2012-11-15 00:25:00 +00:00
|
|
|
remote :: RemoteType
|
|
|
|
remote = RemoteType {
|
|
|
|
typename = "webdav",
|
|
|
|
enumerate = findSpecialRemotes "webdav",
|
|
|
|
generate = gen,
|
|
|
|
setup = webdavSetup
|
|
|
|
}
|
|
|
|
|
2013-09-12 19:54:35 +00:00
|
|
|
gen :: Git.Repo -> UUID -> RemoteConfig -> RemoteGitConfig -> Annex (Maybe Remote)
|
2013-01-01 17:52:47 +00:00
|
|
|
gen r u c gc = new <$> remoteCost gc expensiveRemoteCost
|
2012-11-30 04:55:59 +00:00
|
|
|
where
|
2013-09-12 19:54:35 +00:00
|
|
|
new cst = Just $ encryptableRemote c
|
2012-11-15 00:25:00 +00:00
|
|
|
(storeEncrypted this)
|
|
|
|
(retrieveEncrypted this)
|
|
|
|
this
|
2012-11-30 04:55:59 +00:00
|
|
|
where
|
|
|
|
this = Remote {
|
|
|
|
uuid = u,
|
|
|
|
cost = cst,
|
|
|
|
name = Git.repoDescribe r,
|
2012-12-13 04:45:27 +00:00
|
|
|
storeKey = store this,
|
2012-11-30 04:55:59 +00:00
|
|
|
retrieveKeyFile = retrieve this,
|
|
|
|
retrieveKeyFileCheap = retrieveCheap this,
|
|
|
|
removeKey = remove this,
|
2014-08-06 17:45:19 +00:00
|
|
|
checkPresent = checkKey this,
|
|
|
|
checkPresentCheap = False,
|
2012-11-30 04:55:59 +00:00
|
|
|
whereisKey = Nothing,
|
2013-10-11 20:03:18 +00:00
|
|
|
remoteFsck = Nothing,
|
2013-10-27 19:38:59 +00:00
|
|
|
repairRepo = Nothing,
|
2012-11-30 04:55:59 +00:00
|
|
|
config = c,
|
|
|
|
repo = r,
|
2013-01-01 17:52:47 +00:00
|
|
|
gitconfig = gc,
|
2012-11-30 04:55:59 +00:00
|
|
|
localpath = Nothing,
|
|
|
|
readonly = False,
|
2014-01-13 18:41:10 +00:00
|
|
|
availability = GloballyAvailable,
|
2012-11-30 04:55:59 +00:00
|
|
|
remotetype = remote
|
|
|
|
}
|
2012-11-15 00:25:00 +00:00
|
|
|
|
2014-02-11 18:06:50 +00:00
|
|
|
webdavSetup :: Maybe UUID -> Maybe CredPair -> RemoteConfig -> Annex (RemoteConfig, UUID)
|
|
|
|
webdavSetup mu mcreds c = do
|
2013-09-07 22:38:00 +00:00
|
|
|
u <- maybe (liftIO genUUID) return mu
|
2012-11-15 00:25:00 +00:00
|
|
|
let url = fromMaybe (error "Specify url=") $
|
|
|
|
M.lookup "url" c
|
|
|
|
c' <- encryptionSetup c
|
2014-02-24 19:14:44 +00:00
|
|
|
creds <- maybe (getCreds c' u) (return . Just) mcreds
|
2012-11-15 00:25:00 +00:00
|
|
|
testDav url creds
|
|
|
|
gitConfigSpecialRemote u c' "webdav" "true"
|
2014-02-24 19:14:44 +00:00
|
|
|
c'' <- setRemoteCredPair c' (davCreds u) creds
|
2013-09-07 22:38:00 +00:00
|
|
|
return (c'', u)
|
2012-11-15 00:25:00 +00:00
|
|
|
|
|
|
|
store :: Remote -> Key -> AssociatedFile -> MeterUpdate -> Annex Bool
|
2012-11-19 00:06:28 +00:00
|
|
|
store r k _f p = metered (Just p) k $ \meterupdate ->
|
2013-01-09 22:42:29 +00:00
|
|
|
davAction r False $ \(baseurl, user, pass) ->
|
|
|
|
sendAnnex k (void $ remove r k) $ \src ->
|
|
|
|
liftIO $ withMeteredFile src meterupdate $
|
|
|
|
storeHelper r k baseurl user pass
|
2012-11-15 00:25:00 +00:00
|
|
|
|
|
|
|
storeEncrypted :: Remote -> (Cipher, Key) -> Key -> MeterUpdate -> Annex Bool
|
2012-11-19 00:06:28 +00:00
|
|
|
storeEncrypted r (cipher, enck) k p = metered (Just p) k $ \meterupdate ->
|
2013-01-09 22:42:29 +00:00
|
|
|
davAction r False $ \(baseurl, user, pass) ->
|
|
|
|
sendAnnex k (void $ remove r enck) $ \src ->
|
2013-09-04 22:00:02 +00:00
|
|
|
liftIO $ encrypt (getGpgEncParams r) cipher
|
2013-03-11 01:33:13 +00:00
|
|
|
(streamMeteredFile src meterupdate) $
|
2013-01-09 22:42:29 +00:00
|
|
|
readBytes $ storeHelper r enck baseurl user pass
|
2012-11-16 21:58:58 +00:00
|
|
|
|
2012-11-19 17:18:23 +00:00
|
|
|
storeHelper :: Remote -> Key -> DavUrl -> DavUser -> DavPass -> L.ByteString -> IO Bool
|
2012-11-19 18:08:39 +00:00
|
|
|
storeHelper r k baseurl user pass b = catchBoolIO $ do
|
2014-02-24 22:21:51 +00:00
|
|
|
mkdirRecursiveDAV tmpurl user pass
|
2014-07-24 18:49:22 +00:00
|
|
|
case chunkconfig of
|
2014-07-27 03:01:44 +00:00
|
|
|
NoChunks -> flip catchNonAsync (\e -> warningIO (show e) >> return False) $ do
|
2014-07-24 18:49:22 +00:00
|
|
|
storehttp tmpurl b
|
|
|
|
finalizer tmpurl keyurl
|
|
|
|
return True
|
2014-07-24 19:08:07 +00:00
|
|
|
UnpaddedChunks _ -> error "TODO: storeHelper with UnpaddedChunks"
|
|
|
|
LegacyChunks chunksize -> do
|
2014-07-24 18:49:22 +00:00
|
|
|
let storer urls = Legacy.storeChunked chunksize urls storehttp b
|
|
|
|
let recorder url s = storehttp url (L8.fromString s)
|
|
|
|
Legacy.storeChunks k tmpurl keyurl storer recorder finalizer
|
|
|
|
|
2012-11-16 21:58:58 +00:00
|
|
|
where
|
2012-11-19 18:08:39 +00:00
|
|
|
tmpurl = tmpLocation baseurl k
|
|
|
|
keyurl = davLocation baseurl k
|
2014-08-03 19:35:23 +00:00
|
|
|
chunkconfig = getChunkConfig $ config r
|
2012-11-19 18:08:39 +00:00
|
|
|
finalizer srcurl desturl = do
|
2014-02-24 22:21:51 +00:00
|
|
|
void $ tryNonAsync (deleteDAV desturl user pass)
|
|
|
|
mkdirRecursiveDAV (urlParent desturl) user pass
|
|
|
|
moveDAV srcurl desturl user pass
|
|
|
|
storehttp url = putDAV url user pass
|
2012-11-15 00:25:00 +00:00
|
|
|
|
|
|
|
retrieveCheap :: Remote -> Key -> FilePath -> Annex Bool
|
|
|
|
retrieveCheap _ _ _ = return False
|
|
|
|
|
2013-04-11 21:15:45 +00:00
|
|
|
retrieve :: Remote -> Key -> AssociatedFile -> FilePath -> MeterUpdate -> Annex Bool
|
|
|
|
retrieve r k _f d p = metered (Just p) k $ \meterupdate ->
|
2012-11-17 03:16:18 +00:00
|
|
|
davAction r False $ \(baseurl, user, pass) -> liftIO $ catchBoolIO $
|
|
|
|
withStoredFiles r k baseurl user pass onerr $ \urls -> do
|
2014-07-27 00:21:10 +00:00
|
|
|
Legacy.meteredWriteFileChunks meterupdate d urls $ \url -> do
|
2014-02-24 22:21:51 +00:00
|
|
|
mb <- getDAV url user pass
|
2012-11-18 22:27:53 +00:00
|
|
|
case mb of
|
|
|
|
Nothing -> throwIO "download failed"
|
|
|
|
Just b -> return b
|
2012-11-17 03:16:18 +00:00
|
|
|
return True
|
|
|
|
where
|
|
|
|
onerr _ = return False
|
2012-11-16 17:32:18 +00:00
|
|
|
|
2013-04-11 21:15:45 +00:00
|
|
|
retrieveEncrypted :: Remote -> (Cipher, Key) -> Key -> FilePath -> MeterUpdate -> Annex Bool
|
|
|
|
retrieveEncrypted r (cipher, enck) k d p = metered (Just p) k $ \meterupdate ->
|
2012-11-17 03:16:18 +00:00
|
|
|
davAction r False $ \(baseurl, user, pass) -> liftIO $ catchBoolIO $
|
|
|
|
withStoredFiles r enck baseurl user pass onerr $ \urls -> do
|
2012-11-18 19:27:44 +00:00
|
|
|
decrypt cipher (feeder user pass urls) $
|
|
|
|
readBytes $ meteredWriteFile meterupdate d
|
2012-11-17 03:16:18 +00:00
|
|
|
return True
|
2012-11-16 17:32:18 +00:00
|
|
|
where
|
2012-11-17 03:16:18 +00:00
|
|
|
onerr _ = return False
|
|
|
|
|
2012-11-18 19:27:44 +00:00
|
|
|
feeder _ _ [] _ = noop
|
|
|
|
feeder user pass (url:urls) h = do
|
2014-02-24 22:21:51 +00:00
|
|
|
mb <- getDAV url user pass
|
2012-11-17 03:16:18 +00:00
|
|
|
case mb of
|
2012-11-17 19:30:11 +00:00
|
|
|
Nothing -> throwIO "download failed"
|
2012-11-18 19:27:44 +00:00
|
|
|
Just b -> do
|
|
|
|
L.hPut h b
|
|
|
|
feeder user pass urls h
|
2012-11-15 00:25:00 +00:00
|
|
|
|
|
|
|
remove :: Remote -> Key -> Annex Bool
|
2012-11-16 22:09:28 +00:00
|
|
|
remove r k = davAction r False $ \(baseurl, user, pass) -> liftIO $ do
|
|
|
|
-- Delete the key's whole directory, including any chunked
|
|
|
|
-- files, etc, in a single action.
|
2012-11-19 18:08:39 +00:00
|
|
|
let url = davLocation baseurl k
|
2014-02-24 22:21:51 +00:00
|
|
|
isJust . eitherToMaybe <$> tryNonAsync (deleteDAV url user pass)
|
2012-11-15 00:25:00 +00:00
|
|
|
|
2014-08-06 17:45:19 +00:00
|
|
|
checkKey :: Remote -> Key -> Annex Bool
|
|
|
|
checkKey r k = davAction r noconn (either error id <$$> go)
|
2012-11-15 00:25:00 +00:00
|
|
|
where
|
2014-08-06 17:45:19 +00:00
|
|
|
noconn = error $ name r ++ " not configured"
|
2012-11-15 00:25:00 +00:00
|
|
|
|
2012-11-17 03:16:18 +00:00
|
|
|
go (baseurl, user, pass) = do
|
|
|
|
showAction $ "checking " ++ name r
|
|
|
|
liftIO $ withStoredFiles r k baseurl user pass onerr check
|
|
|
|
where
|
|
|
|
check [] = return $ Right True
|
|
|
|
check (url:urls) = do
|
2014-02-24 22:21:51 +00:00
|
|
|
v <- existsDAV url user pass
|
2012-11-17 03:16:18 +00:00
|
|
|
if v == Right True
|
|
|
|
then check urls
|
|
|
|
else return v
|
|
|
|
|
|
|
|
{- Failed to read the chunkcount file; see if it's missing,
|
|
|
|
- or if there's a problem accessing it,
|
|
|
|
- or perhaps this was an intermittent error. -}
|
|
|
|
onerr url = do
|
2014-02-24 22:21:51 +00:00
|
|
|
v <- existsDAV url user pass
|
2013-09-26 03:19:01 +00:00
|
|
|
return $ if v == Right True
|
|
|
|
then Left $ "failed to read " ++ url
|
|
|
|
else v
|
2012-11-17 03:16:18 +00:00
|
|
|
|
|
|
|
withStoredFiles
|
|
|
|
:: Remote
|
|
|
|
-> Key
|
|
|
|
-> DavUrl
|
|
|
|
-> DavUser
|
|
|
|
-> DavPass
|
|
|
|
-> (DavUrl -> IO a)
|
|
|
|
-> ([DavUrl] -> IO a)
|
|
|
|
-> IO a
|
2014-07-24 18:49:22 +00:00
|
|
|
withStoredFiles r k baseurl user pass onerr a = case chunkconfig of
|
|
|
|
NoChunks -> a [keyurl]
|
2014-07-24 19:08:07 +00:00
|
|
|
UnpaddedChunks _ -> error "TODO: withStoredFiles with UnpaddedChunks"
|
|
|
|
LegacyChunks _ -> do
|
2014-07-24 18:49:22 +00:00
|
|
|
let chunkcount = keyurl ++ Legacy.chunkCount
|
2014-02-24 22:21:51 +00:00
|
|
|
v <- getDAV chunkcount user pass
|
2013-10-26 19:03:12 +00:00
|
|
|
case v of
|
2014-07-24 18:49:22 +00:00
|
|
|
Just s -> a $ Legacy.listChunks keyurl $ L8.toString s
|
2013-10-26 19:03:12 +00:00
|
|
|
Nothing -> do
|
2014-07-24 18:49:22 +00:00
|
|
|
chunks <- Legacy.probeChunks keyurl $ \u -> (== Right True) <$> existsDAV u user pass
|
2013-10-26 19:03:12 +00:00
|
|
|
if null chunks
|
|
|
|
then onerr chunkcount
|
|
|
|
else a chunks
|
2012-11-17 03:16:18 +00:00
|
|
|
where
|
2012-11-19 18:08:39 +00:00
|
|
|
keyurl = davLocation baseurl k ++ keyFile k
|
2014-08-03 19:35:23 +00:00
|
|
|
chunkconfig = getChunkConfig $ config r
|
2012-11-17 03:16:18 +00:00
|
|
|
|
2012-11-15 17:46:16 +00:00
|
|
|
davAction :: Remote -> a -> ((DavUrl, DavUser, DavPass) -> Annex a) -> Annex a
|
2012-11-30 04:55:59 +00:00
|
|
|
davAction r unconfigured action = do
|
|
|
|
mcreds <- getCreds (config r) (uuid r)
|
2013-04-27 19:16:06 +00:00
|
|
|
case (mcreds, configUrl r) of
|
2012-11-30 04:55:59 +00:00
|
|
|
(Just (user, pass), Just url) ->
|
|
|
|
action (url, toDavUser user, toDavPass pass)
|
|
|
|
_ -> return unconfigured
|
2012-11-15 17:46:16 +00:00
|
|
|
|
2013-04-27 19:16:06 +00:00
|
|
|
configUrl :: Remote -> Maybe DavUrl
|
2013-12-02 20:01:20 +00:00
|
|
|
configUrl r = fixup <$> M.lookup "url" (config r)
|
|
|
|
where
|
|
|
|
-- box.com DAV url changed
|
|
|
|
fixup = replace "https://www.box.com/dav/" "https://dav.box.com/dav/"
|
2013-04-27 19:16:06 +00:00
|
|
|
|
2012-11-15 17:46:16 +00:00
|
|
|
toDavUser :: String -> DavUser
|
|
|
|
toDavUser = B8.fromString
|
2012-11-15 00:25:00 +00:00
|
|
|
|
2012-11-15 17:46:16 +00:00
|
|
|
toDavPass :: String -> DavPass
|
|
|
|
toDavPass = B8.fromString
|
|
|
|
|
2012-11-16 04:09:22 +00:00
|
|
|
{- Creates a directory in WebDAV, if not already present; also creating
|
|
|
|
- any missing parent directories. -}
|
2014-02-24 22:21:51 +00:00
|
|
|
mkdirRecursiveDAV :: DavUrl -> DavUser -> DavPass -> IO ()
|
|
|
|
mkdirRecursiveDAV url user pass = go url
|
2012-11-16 04:09:22 +00:00
|
|
|
where
|
2014-02-24 22:21:51 +00:00
|
|
|
make u = mkdirDAV u user pass
|
2012-11-16 04:09:22 +00:00
|
|
|
|
|
|
|
go u = do
|
|
|
|
r <- E.try (make u) :: IO (Either E.SomeException Bool)
|
|
|
|
case r of
|
|
|
|
{- Parent directory is missing. Recurse to create
|
|
|
|
- it, and try once more to create the directory. -}
|
|
|
|
Right False -> do
|
|
|
|
go (urlParent u)
|
|
|
|
void $ make u
|
|
|
|
{- Directory created successfully -}
|
|
|
|
Right True -> return ()
|
|
|
|
{- Directory already exists, or some other error
|
|
|
|
- occurred. In the latter case, whatever wanted
|
|
|
|
- to use this directory will fail. -}
|
|
|
|
Left _ -> return ()
|
|
|
|
|
2012-11-15 00:25:00 +00:00
|
|
|
{- Test if a WebDAV store is usable, by writing to a test file, and then
|
2012-11-17 19:30:11 +00:00
|
|
|
- deleting the file. Exits with an IO error if not. -}
|
2012-11-15 00:25:00 +00:00
|
|
|
testDav :: String -> Maybe CredPair -> Annex ()
|
|
|
|
testDav baseurl (Just (u, p)) = do
|
|
|
|
showSideAction "testing WebDAV server"
|
2014-02-24 22:21:51 +00:00
|
|
|
test "make directory" $ mkdirRecursiveDAV baseurl user pass
|
|
|
|
test "write file" $ putDAV testurl user pass L.empty
|
|
|
|
test "delete file" $ deleteDAV testurl user pass
|
2012-11-17 19:30:11 +00:00
|
|
|
where
|
2012-12-01 18:32:50 +00:00
|
|
|
test desc a = liftIO $
|
2014-02-24 22:21:51 +00:00
|
|
|
either (\e -> throwIO $ "WebDAV failed to " ++ desc ++ ": " ++ show e)
|
2012-12-01 18:32:50 +00:00
|
|
|
(const noop)
|
2014-02-24 22:21:51 +00:00
|
|
|
=<< tryNonAsync a
|
2012-11-17 19:30:11 +00:00
|
|
|
|
2012-11-16 04:09:22 +00:00
|
|
|
user = toDavUser u
|
|
|
|
pass = toDavPass p
|
2012-11-15 00:25:00 +00:00
|
|
|
testurl = davUrl baseurl "git-annex-test"
|
2012-11-16 17:32:18 +00:00
|
|
|
testDav _ Nothing = error "Need to configure webdav username and password."
|
2012-11-15 00:25:00 +00:00
|
|
|
|
|
|
|
getCreds :: RemoteConfig -> UUID -> Annex (Maybe CredPair)
|
2012-11-28 17:31:49 +00:00
|
|
|
getCreds c u = getRemoteCredPairFor "webdav" c (davCreds u)
|
2012-11-15 00:25:00 +00:00
|
|
|
|
|
|
|
davCreds :: UUID -> CredPairStorage
|
|
|
|
davCreds u = CredPairStorage
|
2012-12-13 04:45:27 +00:00
|
|
|
{ credPairFile = fromUUID u
|
|
|
|
, credPairEnvironment = ("WEBDAV_USERNAME", "WEBDAV_PASSWORD")
|
|
|
|
, credPairRemoteKey = Just "davcreds"
|
|
|
|
}
|
2014-02-24 22:21:51 +00:00
|
|
|
|
|
|
|
{- Content-Type to use for files uploaded to WebDAV. -}
|
|
|
|
contentType :: Maybe B8.ByteString
|
|
|
|
contentType = Just $ B8.fromString "application/octet-stream"
|
|
|
|
|
|
|
|
throwIO :: String -> IO a
|
|
|
|
throwIO msg = ioError $ mkIOError userErrorType msg Nothing Nothing
|
|
|
|
|
2014-02-25 19:35:45 +00:00
|
|
|
debugDAV :: DavUrl -> String -> IO ()
|
|
|
|
debugDAV msg url = debugM "DAV" $ msg ++ " " ++ url
|
|
|
|
|
2014-02-24 22:21:51 +00:00
|
|
|
{---------------------------------------------------------------------
|
2014-07-30 15:19:05 +00:00
|
|
|
- Low-level DAV operations.
|
2014-02-24 22:21:51 +00:00
|
|
|
---------------------------------------------------------------------}
|
|
|
|
|
|
|
|
putDAV :: DavUrl -> DavUser -> DavPass -> L.ByteString -> IO ()
|
2014-02-25 19:35:45 +00:00
|
|
|
putDAV url user pass b = do
|
|
|
|
debugDAV "PUT" url
|
2014-02-24 22:21:51 +00:00
|
|
|
goDAV url user pass $ putContentM (contentType, b)
|
|
|
|
|
|
|
|
getDAV :: DavUrl -> DavUser -> DavPass -> IO (Maybe L.ByteString)
|
2014-02-25 19:35:45 +00:00
|
|
|
getDAV url user pass = do
|
|
|
|
debugDAV "GET" url
|
|
|
|
eitherToMaybe <$> tryNonAsync go
|
2014-02-24 22:21:51 +00:00
|
|
|
where
|
|
|
|
go = goDAV url user pass $ snd <$> getContentM
|
|
|
|
|
|
|
|
deleteDAV :: DavUrl -> DavUser -> DavPass -> IO ()
|
2014-02-25 19:35:45 +00:00
|
|
|
deleteDAV url user pass = do
|
|
|
|
debugDAV "DELETE" url
|
2014-02-24 22:21:51 +00:00
|
|
|
goDAV url user pass delContentM
|
|
|
|
|
|
|
|
moveDAV :: DavUrl -> DavUrl -> DavUser -> DavPass -> IO ()
|
2014-02-25 19:35:45 +00:00
|
|
|
moveDAV url newurl user pass = do
|
|
|
|
debugDAV ("MOVE to " ++ newurl ++ " from ") url
|
2014-02-24 22:21:51 +00:00
|
|
|
goDAV url user pass $ moveContentM newurl'
|
|
|
|
where
|
|
|
|
newurl' = B8.fromString newurl
|
|
|
|
|
|
|
|
mkdirDAV :: DavUrl -> DavUser -> DavPass -> IO Bool
|
2014-02-25 19:35:45 +00:00
|
|
|
mkdirDAV url user pass = do
|
|
|
|
debugDAV "MKDIR" url
|
2014-02-24 22:21:51 +00:00
|
|
|
goDAV url user pass mkCol
|
|
|
|
|
|
|
|
existsDAV :: DavUrl -> DavUser -> DavPass -> IO (Either String Bool)
|
2014-02-25 19:35:45 +00:00
|
|
|
existsDAV url user pass = do
|
|
|
|
debugDAV "EXISTS" url
|
|
|
|
either (Left . show) id <$> tryNonAsync check
|
2014-02-24 22:21:51 +00:00
|
|
|
where
|
2014-02-25 05:55:01 +00:00
|
|
|
ispresent = return . Right
|
2014-02-24 22:21:51 +00:00
|
|
|
check = goDAV url user pass $ do
|
|
|
|
setDepth Nothing
|
|
|
|
EL.catchJust
|
|
|
|
(matchStatusCodeException notFound404)
|
|
|
|
(getPropsM >> ispresent True)
|
|
|
|
(const $ ispresent False)
|
|
|
|
|
|
|
|
matchStatusCodeException :: Status -> HttpException -> Maybe ()
|
|
|
|
matchStatusCodeException want (StatusCodeException s _ _)
|
|
|
|
| s == want = Just ()
|
|
|
|
| otherwise = Nothing
|
|
|
|
matchStatusCodeException _ _ = Nothing
|
|
|
|
|
|
|
|
goDAV :: DavUrl -> DavUser -> DavPass -> DAVT IO a -> IO a
|
|
|
|
goDAV url user pass a = choke $ evalDAVT url $ do
|
2014-03-05 17:51:54 +00:00
|
|
|
setResponseTimeout Nothing -- disable default (5 second!) timeout
|
2014-02-24 22:21:51 +00:00
|
|
|
setCreds user pass
|
|
|
|
a
|
|
|
|
where
|
|
|
|
choke :: IO (Either String a) -> IO a
|
|
|
|
choke f = do
|
|
|
|
x <- f
|
|
|
|
case x of
|
|
|
|
Left e -> error e
|
|
|
|
Right r -> return r
|