2013-04-25 16:23:36 +00:00
|
|
|
{- git-annex assistant webapp configurators for Internet Archive
|
|
|
|
-
|
|
|
|
- Copyright 2013 Joey Hess <joey@kitenet.net>
|
|
|
|
-
|
|
|
|
- Licensed under the GNU AGPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
2013-06-05 01:02:09 +00:00
|
|
|
{-# LANGUAGE CPP, QuasiQuotes, TemplateHaskell, OverloadedStrings #-}
|
2013-04-25 16:23:36 +00:00
|
|
|
|
|
|
|
module Assistant.WebApp.Configurators.IA where
|
|
|
|
|
|
|
|
import Assistant.WebApp.Common
|
|
|
|
import qualified Assistant.WebApp.Configurators.AWS as AWS
|
|
|
|
#ifdef WITH_S3
|
|
|
|
import qualified Remote.S3 as S3
|
2013-04-27 19:16:06 +00:00
|
|
|
import qualified Remote.Helper.AWS as AWS
|
2013-10-28 15:33:14 +00:00
|
|
|
import Assistant.WebApp.MakeRemote
|
2013-04-25 16:23:36 +00:00
|
|
|
#endif
|
|
|
|
import qualified Remote
|
2013-04-27 19:16:06 +00:00
|
|
|
import qualified Types.Remote as Remote
|
2013-04-25 16:23:36 +00:00
|
|
|
import Types.StandardGroups
|
2013-04-25 20:42:17 +00:00
|
|
|
import Types.Remote (RemoteConfig)
|
2013-04-25 17:14:49 +00:00
|
|
|
import Logs.Remote
|
2013-09-28 18:35:21 +00:00
|
|
|
import qualified Annex.Url as Url
|
2013-04-27 19:16:06 +00:00
|
|
|
import Creds
|
2013-09-26 20:09:45 +00:00
|
|
|
import Assistant.Gpg
|
2013-04-25 16:23:36 +00:00
|
|
|
|
|
|
|
import qualified Data.Text as T
|
|
|
|
import qualified Data.Map as M
|
|
|
|
import Data.Char
|
|
|
|
import Network.URI
|
|
|
|
|
2013-06-27 05:15:28 +00:00
|
|
|
iaConfigurator :: Widget -> Handler Html
|
2013-04-25 16:23:36 +00:00
|
|
|
iaConfigurator = page "Add an Internet Archive repository" (Just Configuration)
|
|
|
|
|
|
|
|
data IAInput = IAInput
|
|
|
|
{ accessKeyID :: Text
|
|
|
|
, secretAccessKey :: Text
|
|
|
|
, mediaType :: MediaType
|
2013-04-26 03:44:55 +00:00
|
|
|
, itemName :: Text
|
2013-04-25 16:23:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
extractCreds :: IAInput -> AWS.AWSCreds
|
|
|
|
extractCreds i = AWS.AWSCreds (accessKeyID i) (secretAccessKey i)
|
|
|
|
|
|
|
|
{- IA defines only a few media types currently, or the media type
|
|
|
|
- may be omitted
|
|
|
|
-
|
|
|
|
- We add a few other common types, mapped to what we've been told
|
|
|
|
- is the closest match.
|
|
|
|
-}
|
2013-04-26 05:46:07 +00:00
|
|
|
data MediaType = MediaImages | MediaAudio | MediaVideo | MediaText | MediaSoftware | MediaOmitted
|
2013-04-25 16:23:36 +00:00
|
|
|
deriving (Eq, Ord, Enum, Bounded)
|
|
|
|
|
|
|
|
{- Format a MediaType for entry into the IA metadata -}
|
|
|
|
formatMediaType :: MediaType -> String
|
|
|
|
formatMediaType MediaText = "texts"
|
|
|
|
formatMediaType MediaImages = "image"
|
|
|
|
formatMediaType MediaSoftware = "software"
|
2013-04-26 05:46:07 +00:00
|
|
|
formatMediaType MediaVideo = "movies"
|
2013-04-25 16:23:36 +00:00
|
|
|
formatMediaType MediaAudio = "audio"
|
|
|
|
formatMediaType MediaOmitted = ""
|
|
|
|
|
2013-04-26 05:46:07 +00:00
|
|
|
{- A default collection to use for each Mediatype. -}
|
2013-04-25 16:23:36 +00:00
|
|
|
collectionMediaType :: MediaType -> Maybe String
|
|
|
|
collectionMediaType MediaText = Just "opensource"
|
2013-04-26 05:46:07 +00:00
|
|
|
collectionMediaType MediaImages = Just "opensource" -- not ideal
|
2013-04-26 17:00:14 +00:00
|
|
|
collectionMediaType MediaSoftware = Just "opensource" -- not ideal
|
2013-04-26 05:46:07 +00:00
|
|
|
collectionMediaType MediaVideo = Just "opensource_movies"
|
2013-04-25 16:23:36 +00:00
|
|
|
collectionMediaType MediaAudio = Just "opensource_audio"
|
2013-04-26 17:00:14 +00:00
|
|
|
collectionMediaType MediaOmitted = Just "opensource"
|
2013-04-25 16:23:36 +00:00
|
|
|
|
|
|
|
{- Format a MediaType for user display. -}
|
|
|
|
showMediaType :: MediaType -> String
|
|
|
|
showMediaType MediaText = "texts"
|
|
|
|
showMediaType MediaImages = "photos & images"
|
|
|
|
showMediaType MediaSoftware = "software"
|
2013-04-26 05:46:07 +00:00
|
|
|
showMediaType MediaVideo = "videos & movies"
|
2013-04-25 16:23:36 +00:00
|
|
|
showMediaType MediaAudio = "audio & music"
|
|
|
|
showMediaType MediaOmitted = "other"
|
|
|
|
|
2013-06-03 20:33:05 +00:00
|
|
|
iaInputAForm :: Maybe CredPair -> MkAForm IAInput
|
2013-04-27 19:16:06 +00:00
|
|
|
iaInputAForm defcreds = IAInput
|
|
|
|
<$> accessKeyIDFieldWithHelp (T.pack . fst <$> defcreds)
|
|
|
|
<*> AWS.secretAccessKeyField (T.pack . snd <$> defcreds)
|
2014-04-18 00:07:09 +00:00
|
|
|
<*> areq (selectFieldList mediatypes) (bfs "Media Type") (Just MediaOmitted)
|
|
|
|
<*> areq (textField `withExpandableNote` ("Help", itemNameHelp)) (bfs "Item Name") Nothing
|
2013-04-25 16:23:36 +00:00
|
|
|
where
|
|
|
|
mediatypes :: [(Text, MediaType)]
|
|
|
|
mediatypes = map (\t -> (T.pack $ showMediaType t, t)) [minBound..]
|
|
|
|
|
2013-04-26 03:44:55 +00:00
|
|
|
itemNameHelp :: Widget
|
|
|
|
itemNameHelp = [whamlet|
|
|
|
|
<div>
|
|
|
|
Each item stored in the Internet Archive must have a unique name.
|
|
|
|
<div>
|
|
|
|
Once you create the item, a special directory will appear #
|
|
|
|
with a name matching the item name. Files you put in that directory #
|
|
|
|
will be uploaded to your Internet Archive item.
|
|
|
|
|]
|
|
|
|
|
2013-06-03 20:33:05 +00:00
|
|
|
iaCredsAForm :: Maybe CredPair -> MkAForm AWS.AWSCreds
|
2013-04-27 19:16:06 +00:00
|
|
|
iaCredsAForm defcreds = AWS.AWSCreds
|
2014-10-09 19:09:26 +00:00
|
|
|
<$> accessKeyIDFieldWithHelp (T.pack . fst <$> defcreds)
|
|
|
|
<*> AWS.secretAccessKeyField (T.pack . snd <$> defcreds)
|
2013-04-25 17:14:49 +00:00
|
|
|
|
2013-04-27 19:16:06 +00:00
|
|
|
#ifdef WITH_S3
|
|
|
|
previouslyUsedIACreds :: Annex (Maybe CredPair)
|
|
|
|
previouslyUsedIACreds = previouslyUsedCredPair AWS.creds S3.remote $
|
2014-10-23 19:56:35 +00:00
|
|
|
S3.configIA . Remote.config
|
2013-04-27 19:16:06 +00:00
|
|
|
#endif
|
|
|
|
|
2013-06-03 20:33:05 +00:00
|
|
|
accessKeyIDFieldWithHelp :: Maybe Text -> MkAForm Text
|
2013-10-02 05:06:59 +00:00
|
|
|
accessKeyIDFieldWithHelp = AWS.accessKeyIDField help
|
2013-04-25 16:23:36 +00:00
|
|
|
where
|
|
|
|
help = [whamlet|
|
|
|
|
<a href="http://archive.org/account/s3.php">
|
|
|
|
Get Internet Archive access keys
|
|
|
|
|]
|
|
|
|
|
2013-06-27 05:15:28 +00:00
|
|
|
getAddIAR :: Handler Html
|
2013-04-25 16:23:36 +00:00
|
|
|
getAddIAR = postAddIAR
|
|
|
|
|
2013-06-27 05:15:28 +00:00
|
|
|
postAddIAR :: Handler Html
|
2013-04-25 16:23:36 +00:00
|
|
|
#ifdef WITH_S3
|
|
|
|
postAddIAR = iaConfigurator $ do
|
2013-04-27 19:16:06 +00:00
|
|
|
defcreds <- liftAnnex previouslyUsedIACreds
|
2013-06-03 17:51:54 +00:00
|
|
|
((result, form), enctype) <- liftH $
|
2014-04-18 00:07:09 +00:00
|
|
|
runFormPostNoToken $ renderBootstrap3 bootstrapFormLayout $ iaInputAForm defcreds
|
2013-04-25 16:23:36 +00:00
|
|
|
case result of
|
2013-06-03 17:51:54 +00:00
|
|
|
FormSuccess input -> liftH $ do
|
2013-04-26 03:44:55 +00:00
|
|
|
let name = escapeBucket $ T.unpack $ itemName input
|
2013-09-27 04:15:50 +00:00
|
|
|
AWS.makeAWSRemote initSpecialRemote S3.remote PublicGroup (extractCreds input) name $
|
2013-04-25 16:23:36 +00:00
|
|
|
M.fromList $ catMaybes
|
|
|
|
[ Just $ configureEncryption NoEncryption
|
|
|
|
, Just ("type", "S3")
|
2013-04-25 17:14:49 +00:00
|
|
|
, Just ("host", S3.iaHost)
|
2013-04-25 16:23:36 +00:00
|
|
|
, Just ("bucket", escapeHeader name)
|
2013-04-26 03:44:55 +00:00
|
|
|
, Just ("x-archive-meta-title", escapeHeader $ T.unpack $ itemName input)
|
2013-04-25 16:23:36 +00:00
|
|
|
, if mediaType input == MediaOmitted
|
|
|
|
then Nothing
|
|
|
|
else Just ("x-archive-mediatype", formatMediaType $ mediaType input)
|
|
|
|
, (,) <$> pure "x-archive-meta-collection" <*> collectionMediaType (mediaType input)
|
|
|
|
-- Make item show up ASAP.
|
|
|
|
, Just ("x-archive-interactive-priority", "1")
|
2013-04-26 03:44:55 +00:00
|
|
|
, Just ("preferreddir", name)
|
2013-04-25 16:23:36 +00:00
|
|
|
]
|
|
|
|
_ -> $(widgetFile "configurators/addia")
|
|
|
|
#else
|
|
|
|
postAddIAR = error "S3 not supported by this build"
|
|
|
|
#endif
|
|
|
|
|
2013-06-27 05:15:28 +00:00
|
|
|
getEnableIAR :: UUID -> Handler Html
|
2013-04-25 16:23:36 +00:00
|
|
|
getEnableIAR = postEnableIAR
|
|
|
|
|
2013-06-27 05:15:28 +00:00
|
|
|
postEnableIAR :: UUID -> Handler Html
|
2013-04-25 16:23:36 +00:00
|
|
|
#ifdef WITH_S3
|
2013-04-25 17:14:49 +00:00
|
|
|
postEnableIAR = iaConfigurator . enableIARemote
|
2013-04-25 16:23:36 +00:00
|
|
|
#else
|
|
|
|
postEnableIAR _ = error "S3 not supported by this build"
|
|
|
|
#endif
|
|
|
|
|
2013-04-26 20:07:55 +00:00
|
|
|
#ifdef WITH_S3
|
2013-04-25 17:14:49 +00:00
|
|
|
enableIARemote :: UUID -> Widget
|
|
|
|
enableIARemote uuid = do
|
2013-04-27 19:16:06 +00:00
|
|
|
defcreds <- liftAnnex previouslyUsedIACreds
|
2013-06-03 17:51:54 +00:00
|
|
|
((result, form), enctype) <- liftH $
|
2014-04-18 00:07:09 +00:00
|
|
|
runFormPostNoToken $ renderBootstrap3 bootstrapFormLayout $ iaCredsAForm defcreds
|
2013-04-25 17:14:49 +00:00
|
|
|
case result of
|
2013-06-03 17:51:54 +00:00
|
|
|
FormSuccess creds -> liftH $ do
|
2013-04-25 17:14:49 +00:00
|
|
|
m <- liftAnnex readRemoteLog
|
|
|
|
let name = fromJust $ M.lookup "name" $
|
|
|
|
fromJust $ M.lookup uuid m
|
2013-09-27 04:15:50 +00:00
|
|
|
AWS.makeAWSRemote enableSpecialRemote S3.remote PublicGroup creds name M.empty
|
2013-04-25 17:14:49 +00:00
|
|
|
_ -> do
|
|
|
|
description <- liftAnnex $
|
|
|
|
T.pack <$> Remote.prettyUUID uuid
|
|
|
|
$(widgetFile "configurators/enableia")
|
2013-04-26 20:07:55 +00:00
|
|
|
#endif
|
2013-04-25 17:14:49 +00:00
|
|
|
|
2013-04-26 03:44:55 +00:00
|
|
|
{- Convert a description into a bucket item name, which will also be
|
|
|
|
- used as the repository name, and the preferreddir.
|
2013-04-25 16:23:36 +00:00
|
|
|
- IA seems to need only lower case, and no spaces. -}
|
|
|
|
escapeBucket :: String -> String
|
2013-04-26 03:44:55 +00:00
|
|
|
escapeBucket = map toLower . replace " " "-"
|
2013-04-25 16:23:36 +00:00
|
|
|
|
|
|
|
{- IA S3 API likes headers to be URI escaped, escaping spaces looks ugly. -}
|
|
|
|
escapeHeader :: String -> String
|
|
|
|
escapeHeader = escapeURIString (\c -> isUnescapedInURI c && c /= ' ')
|
2013-04-25 20:42:17 +00:00
|
|
|
|
|
|
|
getRepoInfo :: RemoteConfig -> Widget
|
|
|
|
getRepoInfo c = do
|
2014-02-25 02:00:25 +00:00
|
|
|
uo <- liftAnnex Url.getUrlOptions
|
|
|
|
exists <- liftIO $ catchDefaultIO False $ fst <$> Url.exists url uo
|
2013-04-25 20:42:17 +00:00
|
|
|
[whamlet|
|
|
|
|
<a href="#{url}">
|
2013-04-26 17:00:14 +00:00
|
|
|
Internet Archive item
|
2013-04-25 20:42:17 +00:00
|
|
|
$if (not exists)
|
|
|
|
<p>
|
2013-04-26 17:00:14 +00:00
|
|
|
The page will only be available once some files #
|
2013-04-25 20:42:17 +00:00
|
|
|
have been uploaded, and the Internet Archive has processed them.
|
|
|
|
|]
|
|
|
|
where
|
2014-10-09 18:53:13 +00:00
|
|
|
bucket = fromMaybe "" $ M.lookup "bucket" c
|
2013-04-25 20:42:17 +00:00
|
|
|
#ifdef WITH_S3
|
|
|
|
url = S3.iaItemUrl bucket
|
|
|
|
#else
|
|
|
|
url = ""
|
|
|
|
#endif
|