2014-08-07 19:45:56 +00:00
|
|
|
{- WebDAV locations.
|
|
|
|
-
|
2015-01-21 16:50:09 +00:00
|
|
|
- Copyright 2014 Joey Hess <id@joeyh.name>
|
2014-08-07 19:45:56 +00:00
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
|
|
|
{-# LANGUAGE CPP #-}
|
|
|
|
{-# LANGUAGE FlexibleContexts #-}
|
|
|
|
|
|
|
|
module Remote.WebDAV.DavLocation where
|
|
|
|
|
|
|
|
import Types
|
|
|
|
import Locations
|
|
|
|
import Utility.Url (URLString)
|
|
|
|
|
|
|
|
import System.FilePath.Posix -- for manipulating url paths
|
|
|
|
import Network.Protocol.HTTP.DAV (inDAVLocation, DAVT)
|
|
|
|
import Control.Monad.IO.Class (MonadIO)
|
2015-01-28 19:55:17 +00:00
|
|
|
import Data.Default
|
2014-08-07 19:45:56 +00:00
|
|
|
#ifdef mingw32_HOST_OS
|
|
|
|
import Data.String.Utils
|
|
|
|
#endif
|
|
|
|
|
|
|
|
-- Relative to the top of the DAV url.
|
|
|
|
type DavLocation = String
|
|
|
|
|
|
|
|
{- Runs action in subdirectory, relative to the current location. -}
|
|
|
|
inLocation :: (MonadIO m) => DavLocation -> DAVT m a -> DAVT m a
|
|
|
|
inLocation d = inDAVLocation (</> d)
|
|
|
|
|
|
|
|
{- The directory where files(s) for a key are stored. -}
|
2014-08-08 17:40:55 +00:00
|
|
|
keyDir :: Key -> DavLocation
|
|
|
|
keyDir k = addTrailingPathSeparator $ hashdir </> keyFile k
|
2014-08-07 19:45:56 +00:00
|
|
|
where
|
|
|
|
#ifndef mingw32_HOST_OS
|
2015-01-28 19:55:17 +00:00
|
|
|
hashdir = hashDirLower def k
|
2014-08-07 19:45:56 +00:00
|
|
|
#else
|
2015-01-28 19:55:17 +00:00
|
|
|
hashdir = replace "\\" "/" (hashDirLower def k)
|
2014-08-07 19:45:56 +00:00
|
|
|
#endif
|
|
|
|
|
2014-08-08 17:40:55 +00:00
|
|
|
keyLocation :: Key -> DavLocation
|
|
|
|
keyLocation k = keyDir k ++ keyFile k
|
|
|
|
|
2014-08-07 19:45:56 +00:00
|
|
|
{- Where we store temporary data for a key as it's being uploaded. -}
|
|
|
|
keyTmpLocation :: Key -> DavLocation
|
|
|
|
keyTmpLocation = addTrailingPathSeparator . tmpLocation . keyFile
|
|
|
|
|
|
|
|
tmpLocation :: FilePath -> DavLocation
|
|
|
|
tmpLocation f = tmpDir </> f
|
|
|
|
|
|
|
|
tmpDir :: DavLocation
|
|
|
|
tmpDir = "tmp"
|
|
|
|
|
|
|
|
locationParent :: String -> Maybe String
|
|
|
|
locationParent loc
|
|
|
|
| loc `elem` tops = Nothing
|
|
|
|
| otherwise = Just (takeDirectory loc)
|
|
|
|
where
|
|
|
|
tops = ["/", "", "."]
|
|
|
|
|
|
|
|
locationUrl :: URLString -> DavLocation -> URLString
|
|
|
|
locationUrl baseurl loc = baseurl </> loc
|