2014-12-11 19:32:42 +00:00
|
|
|
{- git-annex URL contents
|
|
|
|
-
|
|
|
|
- Copyright 2014 Joey Hess <joey@kitenet.net>
|
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
2014-12-12 00:08:49 +00:00
|
|
|
module Types.UrlContents (
|
|
|
|
UrlContents(..),
|
|
|
|
SafeFilePath,
|
|
|
|
mkSafeFilePath,
|
|
|
|
fromSafeFilePath
|
|
|
|
) where
|
2014-12-11 19:32:42 +00:00
|
|
|
|
|
|
|
import Utility.Url
|
2014-12-12 00:08:49 +00:00
|
|
|
import Utility.Path
|
|
|
|
|
|
|
|
import System.FilePath
|
2014-12-11 19:32:42 +00:00
|
|
|
|
|
|
|
data UrlContents
|
|
|
|
-- An URL contains a file, whose size may be known.
|
2014-12-11 20:09:56 +00:00
|
|
|
-- There might be a nicer filename to use.
|
2014-12-12 00:08:49 +00:00
|
|
|
= UrlContents (Maybe Integer) (Maybe SafeFilePath)
|
2014-12-11 19:32:42 +00:00
|
|
|
-- Sometimes an URL points to multiple files, each accessible
|
|
|
|
-- by their own URL.
|
2014-12-12 00:08:49 +00:00
|
|
|
| UrlMulti [(URLString, Maybe Integer, SafeFilePath)]
|
|
|
|
|
|
|
|
-- This is a FilePath, from an untrusted source,
|
|
|
|
-- sanitized so it doesn't contain any directory traversal tricks
|
|
|
|
-- and is always relative. It can still contain subdirectories.
|
|
|
|
-- Any unusual characters are also filtered out.
|
|
|
|
newtype SafeFilePath = SafeFilePath FilePath
|
|
|
|
deriving (Show)
|
|
|
|
|
|
|
|
mkSafeFilePath :: FilePath -> SafeFilePath
|
|
|
|
mkSafeFilePath p = SafeFilePath $ if null p' then "file" else p'
|
|
|
|
where
|
|
|
|
p' = joinPath $ filter safe $ map sanitizeFilePath $ splitDirectories p
|
|
|
|
safe s
|
|
|
|
| isDrive s = False
|
|
|
|
| s == ".." = False
|
2014-12-12 00:13:37 +00:00
|
|
|
| s == ".git" = False
|
2014-12-12 00:08:49 +00:00
|
|
|
| null s = False
|
|
|
|
| otherwise = True
|
|
|
|
|
|
|
|
fromSafeFilePath :: SafeFilePath -> FilePath
|
|
|
|
fromSafeFilePath (SafeFilePath p) = p
|