Something went wrong. Try again.
A music player that connects to your cloud/distributed storage. forked from tokono.ma/diffuse
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319module Sources.Services.Ipfs exposing (..)
{-| IPFS Service.
Resources:
- <https://ipfs.io/docs/api/>
-}
import Common exposing (boolFromString, boolToString)import Conditional exposing (ifThenElse)import Dictimport Dict.Ext as Dictimport Httpimport Json.Decode as Jsonimport Sources exposing (Property, SourceData)import Sources.Processing exposing (..)import Sources.Services.Ipfs.Marker as Markerimport Sources.Services.Ipfs.Parser as Parserimport String.Ext as Stringimport Taskimport Timeimport Url
-- PROPERTIES-- 📟
defaults = { gateway = "" , local = boolToString False , name = "Music from IPFS" , ipns = boolToString False }
defaultGateway = "https://ipfs.io"
{-| The list of properties we need from the user.
Tuple: (property, label, placeholder, isPassword)Will be used for the forms.
-}properties : List Propertyproperties = [ { key = "directoryHash" , label = "Directory hash / DNSLink domain" , placeholder = "QmVLDAhCY3X9P2u" , password = False } , { key = "ipns" , label = "Resolve using IPNS" , placeholder = defaults.ipns , password = False } , { key = "gateway" , label = "Gateway (Optional)" , placeholder = defaultGateway , password = False } , { key = "local" , label = "Resolve IPNS locally" , placeholder = defaults.local , password = False } ]
{-| Initial data set.-}initialData : SourceDatainitialData = Dict.fromList [ ( "directoryHash", "" ) , ( "gateway", defaults.gateway ) , ( "ipns", defaults.ipns ) , ( "local", defaults.local ) , ( "name", defaults.name ) ]
-- PREPARATION
prepare : String -> SourceData -> Marker -> (Result Http.Error String -> msg) -> Maybe (Cmd msg)prepare _ srcData _ toMsg = let domainName = srcData |> Dict.get "directoryHash" |> Maybe.withDefault "" |> String.chopStart "http://" |> String.chopStart "https://" |> String.chopEnd "/" |> String.chopStart "_dnslink." in if isDnsLink srcData then (Just << Http.request) { method = "POST" , headers = [] , url = extractGateway srcData ++ "/api/v0/dns?arg=" ++ domainName , body = Http.emptyBody , expect = Http.expectString toMsg , timeout = Nothing , tracker = Nothing }
else Nothing
-- TREE
{-| Create a directory tree.-}makeTree : SourceData -> Marker -> Time.Posix -> (Result Http.Error String -> msg) -> Cmd msgmakeTree srcData marker _ resultMsg = let gateway = extractGateway srcData
resolveWithIpns = case marker of InProgress _ -> False
_ -> srcData |> Dict.fetch "ipns" defaults.ipns |> boolFromString
resolveLocally = srcData |> Dict.fetch "local" defaults.local |> boolFromString |> (\b -> ifThenElse b "true" "false")
root = rootHash srcData
path = case marker of InProgress _ -> marker |> Marker.takeOne |> Maybe.map (\p -> root ++ "/" ++ p) |> Maybe.withDefault ""
_ -> root in (if resolveWithIpns then Http.task { method = "POST" , headers = [] , url = gateway ++ "/api/v0/name/resolve?arg=" ++ encodedPath path ++ "&local=" ++ resolveLocally ++ "&encoding=json" , body = Http.emptyBody , resolver = Http.stringResolver ipnsResolver , timeout = Just (60 * 15 * 1000) }
else Task.succeed { ipfsPath = path } ) |> Task.andThen (\{ ipfsPath } -> Http.task { method = "POST" , headers = [] , url = gateway ++ "/api/v0/ls?arg=" ++ encodedPath ipfsPath ++ "&encoding=json" , body = Http.emptyBody , resolver = Http.stringResolver Common.translateHttpResponse , timeout = Just (60 * 15 * 1000) } ) |> Task.attempt resultMsg
ipnsResolver : Http.Response String -> Result Http.Error { ipfsPath : String }ipnsResolver response = case response of Http.BadUrl_ u -> Err (Http.BadUrl u)
Http.Timeout_ -> Err Http.Timeout
Http.NetworkError_ -> Err Http.NetworkError
Http.BadStatus_ _ body -> Err (Http.BadBody body)
Http.GoodStatus_ _ body -> body |> Json.decodeString (Json.field "Path" Json.string) |> Result.map (\path -> { ipfsPath = String.chopStart "/ipfs/" path }) |> Result.mapError (Json.errorToString >> Http.BadBody)
{-| Re-export parser functions.-}parsePreparationResponse : String -> Time.Posix -> SourceData -> Marker -> PrepationAnswer MarkerparsePreparationResponse = Parser.parseDnsLookup
parseTreeResponse : String -> Marker -> TreeAnswer MarkerparseTreeResponse = Parser.parseTreeResponse
parseErrorResponse : String -> Maybe StringparseErrorResponse = Parser.parseErrorResponse
-- POST
{-| Post process the tree results.
!!! Make sure we only use music files that we can use.
-}postProcessTree : List String -> List StringpostProcessTree = identity
-- TRACK URL
{-| Create a public url for a file.
We need this to play the track.
-}makeTrackUrl : Time.Posix -> String -> SourceData -> HttpMethod -> String -> StringmakeTrackUrl _ _ srcData _ path = if not (String.contains "/" path) && not (String.contains "." path) then -- If it still uses the old way of doing things -- (ie. each path was a cid) extractGateway srcData ++ "/ipfs/" ++ path
else -- Or the new way extractGateway srcData ++ "/ipfs/" ++ rootHash srcData ++ "/" ++ encodedPath path
-- ⚗️
encodedPath : String -> StringencodedPath path = path |> String.split "/" |> List.map Url.percentEncode |> String.join "/"
extractGateway : SourceData -> StringextractGateway srcData = srcData |> Dict.get "gateway" |> Maybe.map String.trim |> Maybe.andThen (\s -> case s of "" -> Nothing
_ -> Just s ) |> Maybe.map (String.chopEnd "/") |> Maybe.withDefault defaultGateway
isDnsLink : SourceData -> BoolisDnsLink srcData = srcData |> Dict.get "directoryHash" |> Maybe.map pathIsDnsLink |> Maybe.withDefault False
pathIsDnsLink : String -> BoolpathIsDnsLink = String.contains "."
rootHash : SourceData -> StringrootHash srcData = srcData |> Dict.get "directoryHash" |> Maybe.andThen (\path -> if pathIsDnsLink path then Dict.get "directoryHashFromDnsLink" srcData
else Just path ) |> Maybe.withDefault "" |> String.chopEnd "/"