2011-07-01 17:15:46 -04:00
|
|
|
{- git-annex command
|
|
|
|
-
|
|
|
|
- Copyright 2011 Joey Hess <joey@kitenet.net>
|
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
|
|
|
module Command.AddUrl where
|
|
|
|
|
|
|
|
import Network.URI
|
|
|
|
|
2011-10-05 16:02:51 -04:00
|
|
|
import Common.Annex
|
2011-07-01 17:15:46 -04:00
|
|
|
import Command
|
|
|
|
import qualified Backend
|
|
|
|
import qualified Command.Add
|
2011-07-01 18:46:07 -04:00
|
|
|
import qualified Annex
|
2011-08-06 14:57:22 -04:00
|
|
|
import qualified Backend.URL
|
2012-02-10 19:23:23 -04:00
|
|
|
import qualified Utility.Url as Url
|
2011-10-04 00:40:47 -04:00
|
|
|
import Annex.Content
|
2011-10-15 16:36:56 -04:00
|
|
|
import Logs.Web
|
2012-02-08 15:35:18 -04:00
|
|
|
import qualified Option
|
2012-02-10 19:40:36 -04:00
|
|
|
import Types.Key
|
2012-04-22 01:13:09 -04:00
|
|
|
import Config
|
2011-07-01 17:15:46 -04:00
|
|
|
|
2011-10-29 15:19:05 -04:00
|
|
|
def :: [Command]
|
2012-02-16 12:25:19 -04:00
|
|
|
def = [withOptions [fileOption, pathdepthOption] $
|
2012-02-08 15:35:18 -04:00
|
|
|
command "addurl" (paramRepeating paramUrl) seek "add urls to annex"]
|
|
|
|
|
|
|
|
fileOption :: Option
|
|
|
|
fileOption = Option.field [] "file" paramFile "specify what file the url is added to"
|
2011-07-01 17:15:46 -04:00
|
|
|
|
2012-02-16 12:25:19 -04:00
|
|
|
pathdepthOption :: Option
|
2012-02-16 12:37:30 -04:00
|
|
|
pathdepthOption = Option.field [] "pathdepth" paramNumber "path components to use in filename"
|
2012-02-16 12:25:19 -04:00
|
|
|
|
2011-07-01 17:15:46 -04:00
|
|
|
seek :: [CommandSeek]
|
2012-02-08 15:35:18 -04:00
|
|
|
seek = [withField fileOption return $ \f ->
|
2012-02-16 12:25:19 -04:00
|
|
|
withField pathdepthOption (return . maybe Nothing readish) $ \d ->
|
|
|
|
withStrings $ start f d]
|
2011-07-01 17:15:46 -04:00
|
|
|
|
2012-02-16 12:25:19 -04:00
|
|
|
start :: Maybe FilePath -> Maybe Int -> String -> CommandStart
|
|
|
|
start optfile pathdepth s = notBareRepo $ go $ fromMaybe bad $ parseURI s
|
2011-10-31 16:46:51 -04:00
|
|
|
where
|
2012-02-09 14:19:58 -04:00
|
|
|
bad = fromMaybe (error $ "bad url " ++ s) $
|
|
|
|
parseURI $ escapeURIString isUnescapedInURI s
|
|
|
|
go url = do
|
2012-02-16 12:25:19 -04:00
|
|
|
let file = fromMaybe (url2file url pathdepth) optfile
|
2011-07-01 17:15:46 -04:00
|
|
|
showStart "addurl" file
|
2012-02-18 12:08:02 -04:00
|
|
|
next $ perform s file
|
2011-10-31 16:46:51 -04:00
|
|
|
|
2012-02-18 12:08:02 -04:00
|
|
|
perform :: String -> FilePath -> CommandPerform
|
|
|
|
perform url file = ifAnnexed file addurl geturl
|
2012-02-08 15:35:18 -04:00
|
|
|
where
|
|
|
|
geturl = do
|
2012-02-16 00:05:17 -04:00
|
|
|
liftIO $ createDirectoryIfMissing True (parentDir file)
|
2012-03-14 17:43:34 -04:00
|
|
|
ifM (Annex.getState Annex.fast)
|
|
|
|
( nodownload url file , download url file )
|
2012-04-22 01:13:09 -04:00
|
|
|
addurl (key, _backend) = do
|
|
|
|
headers <- getHttpHeaders
|
|
|
|
ifM (liftIO $ Url.check url headers $ keySize key)
|
2012-03-14 17:43:34 -04:00
|
|
|
( do
|
2012-02-18 12:08:02 -04:00
|
|
|
setUrlPresent key url
|
|
|
|
next $ return True
|
2012-03-14 17:43:34 -04:00
|
|
|
, do
|
2012-02-18 12:08:02 -04:00
|
|
|
warning $ "failed to verify url: " ++ url
|
|
|
|
stop
|
2012-03-14 17:43:34 -04:00
|
|
|
)
|
2011-08-06 14:57:22 -04:00
|
|
|
|
|
|
|
download :: String -> FilePath -> CommandPerform
|
|
|
|
download url file = do
|
2011-07-19 14:07:23 -04:00
|
|
|
showAction $ "downloading " ++ url ++ " "
|
2012-02-10 19:23:23 -04:00
|
|
|
let dummykey = Backend.URL.fromUrl url Nothing
|
2011-11-08 15:34:10 -04:00
|
|
|
tmp <- fromRepo $ gitAnnexTmpLocation dummykey
|
2011-07-01 18:46:07 -04:00
|
|
|
liftIO $ createDirectoryIfMissing True (parentDir tmp)
|
2012-01-02 14:20:20 -04:00
|
|
|
stopUnless (downloadUrl [url] tmp) $ do
|
2012-02-13 23:42:44 -04:00
|
|
|
backend <- Backend.chooseBackend file
|
2011-12-09 12:23:45 -04:00
|
|
|
k <- Backend.genKey tmp backend
|
|
|
|
case k of
|
|
|
|
Nothing -> stop
|
|
|
|
Just (key, _) -> do
|
|
|
|
moveAnnex key tmp
|
|
|
|
setUrlPresent key url
|
|
|
|
next $ Command.Add.cleanup file key True
|
2011-07-01 17:15:46 -04:00
|
|
|
|
2011-08-06 14:57:22 -04:00
|
|
|
nodownload :: String -> FilePath -> CommandPerform
|
|
|
|
nodownload url file = do
|
2012-04-22 01:13:09 -04:00
|
|
|
headers <- getHttpHeaders
|
|
|
|
(exists, size) <- liftIO $ Url.exists url headers
|
2012-02-18 11:44:21 -04:00
|
|
|
if exists
|
|
|
|
then do
|
|
|
|
let key = Backend.URL.fromUrl url size
|
|
|
|
setUrlPresent key url
|
|
|
|
next $ Command.Add.cleanup file key False
|
|
|
|
else do
|
|
|
|
warning $ "unable to access url: " ++ url
|
|
|
|
stop
|
2011-08-06 14:57:22 -04:00
|
|
|
|
2012-02-16 12:25:19 -04:00
|
|
|
url2file :: URI -> Maybe Int -> FilePath
|
|
|
|
url2file url pathdepth = case pathdepth of
|
|
|
|
Nothing -> filesize $ escape fullurl
|
|
|
|
Just depth
|
2012-02-16 14:26:53 -04:00
|
|
|
| depth > 0 -> frombits $ drop depth
|
2012-02-16 14:28:17 -04:00
|
|
|
| depth < 0 -> frombits $ reverse . take (negate depth) . reverse
|
|
|
|
| otherwise -> error "bad --pathdepth"
|
2011-07-01 17:15:46 -04:00
|
|
|
where
|
2012-02-16 12:25:19 -04:00
|
|
|
fullurl = uriRegName auth ++ uriPath url ++ uriQuery url
|
2012-02-16 19:37:02 -04:00
|
|
|
frombits a = join "/" $ a urlbits
|
|
|
|
urlbits = map (filesize . escape) $ filter (not . null) $ split "/" fullurl
|
2011-09-07 19:04:51 -04:00
|
|
|
auth = fromMaybe (error $ "bad url " ++ show url) $ uriAuthority url
|
2012-02-16 12:25:19 -04:00
|
|
|
filesize = take 255
|
|
|
|
escape = replace "/" "_" . replace "?" "_"
|