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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147module Sources.Services.Ipfs.Parser exposing (Link, linkDecoder, parseDnsLookup, parseErrorResponse, parseTreeResponse, treeDecoder)
import Dictimport Json.Decode exposing (..)import Sources exposing (SourceData)import Sources.Pick exposing (isMusicFile)import Sources.Processing exposing (Marker(..), PrepationAnswer, TreeAnswer)import Sources.Services.Ipfs.Marker as Markerimport String.Ext as Stringimport Time
-- PREPARATION
parseDnsLookup : String -> Time.Posix -> SourceData -> Marker -> PrepationAnswer MarkerparseDnsLookup response _ srcData _ = case decodeString dnsResultDecoder response of Ok path -> srcData |> Dict.insert "directoryHashFromDnsLink" (String.chopStart "/ipfs/" path) |> (\s -> { sourceData = s, marker = TheEnd })
Err _ -> { sourceData = srcData, marker = TheEnd }
dnsResultDecoder : Decoder StringdnsResultDecoder = oneOf [ at [ "Path" ] string , cloudflareDnsResultDecoder ]
cloudflareDnsResultDecoder : Decoder StringcloudflareDnsResultDecoder = string |> at [ "Answer", "0", "data" ] |> map (\txt -> txt |> String.chopEnd "\"" |> String.chopStart "\"" |> String.chopStart "dnslink=/ipfs/" )
-- TREE
parseTreeResponse : String -> Marker -> TreeAnswer MarkerparseTreeResponse response previousMarker = let prefix = case previousMarker of TheBeginning -> ""
_ -> response |> decodeString prefixDecoder |> Result.map (String.chopStart "/ipfs/" >> String.split "/" >> List.drop 1 >> String.join "/" ) |> Result.map (\s -> if String.isEmpty s then ""
else s ++ "/" ) |> Result.withDefault ""
links = case decodeString treeDecoder response of Ok l -> l
Err _ -> []
dirs = links |> List.filter (.typ >> (==) 1) |> List.map (\l -> prefix ++ l.name)
files = links |> List.filter (.typ >> (==) 2) |> List.filter (.name >> isMusicFile) |> List.map (\l -> prefix ++ l.name) in { filePaths = files , marker = previousMarker |> Marker.removeOne |> Marker.concat dirs }
prefixDecoder : Decoder StringprefixDecoder = field "Objects" <| index 0 <| field "Hash" <| string
treeDecoder : Decoder (List Link)treeDecoder = field "Objects" <| index 0 <| field "Links" <| list linkDecoder
-- LINKS
type alias Link = { hash : String , name : String , typ : Int }
linkDecoder : Decoder LinklinkDecoder = map3 Link (field "Hash" string) (field "Name" string) (field "Type" int)
-- ERRORS
parseErrorResponse : String -> Maybe StringparseErrorResponse response = response |> decodeString (field "Message" string) |> Result.toMaybe