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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215module Sources.Services.WebDav exposing (defaults, initialData, makeTrackUrl, makeTree, parseErrorResponse, parsePreparationResponse, parseTreeResponse, postProcessTree, prepare, properties)
{-| IPFS Service.
Resources:
- <https://ipfs.io/docs/api/>
-}
import Base64import Commonimport Dictimport Dict.Ext as Dictimport Httpimport Sources exposing (Property, SourceData)import Sources.Pick exposing (selectMusicFiles)import Sources.Processing exposing (..)import Sources.Services.Common exposing (noPrep)import Sources.Services.WebDav.Marker as Markerimport Sources.Services.WebDav.Parser as Parserimport String.Ext as Stringimport Timeimport Url
-- PROPERTIES-- 📟
defaults = { name = "Music from WebDAV" }
{-| The list of properties we need from the user.
Tuple: (property, label, placeholder, isPassword)Will be used for the forms.
-}properties : List Propertyproperties = [ { key = "url" , label = "Host URL" , placeholder = "https://demo.nextcloud.com" , password = False } , { key = "directoryPath" , label = "Directory (Optional)" , placeholder = "/icidasset/remote.php/webdav/" , password = False } , { key = "username" , label = "Username (Optional)" , placeholder = "" , password = False } , { key = "password" , label = "Password (Optional)" , placeholder = "" , password = True } ]
{-| Initial data set.-}initialData : SourceDatainitialData = Dict.fromList [ ( "directoryPath", "" ) , ( "url", "" ) , ( "username", "" ) , ( "password", "" ) , ( "name", defaults.name ) ]
-- PREPARATION
prepare : String -> SourceData -> Marker -> (Result Http.Error String -> msg) -> Maybe (Cmd msg)prepare _ _ _ _ = Nothing
-- TREE
{-| Create a directory tree.-}makeTree : SourceData -> Marker -> Time.Posix -> (Result Http.Error String -> msg) -> Cmd msgmakeTree srcData marker _ resultMsg = let directory = case marker of InProgress _ -> marker |> Marker.takeOne |> Maybe.withDefault ""
_ -> srcData |> Dict.get "directoryPath" |> Maybe.withDefault ""
username = Dict.fetch "username" "" srcData
password = Dict.fetch "password" "" srcData
auth = "Basic " ++ Base64.encode (username ++ ":" ++ password) in Http.request { method = "PROPFIND" , headers = [ Http.header "Authorization" auth, Http.header "Depth" "1" ] , url = url { addAuth = False } srcData directory , body = Http.emptyBody , expect = Http.expectStringResponse resultMsg Common.translateHttpResponse , timeout = Nothing , tracker = Nothing }
{-| Re-export parser functions.-}parsePreparationResponse : String -> Time.Posix -> SourceData -> Marker -> PrepationAnswer MarkerparsePreparationResponse = noPrep
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 = selectMusicFiles
-- 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 _ filePath = url { addAuth = True } srcData filePath
-- COMMON
url : { addAuth : Bool } -> SourceData -> String -> Stringurl { addAuth } srcData path = let host = String.chopEnd "/" (Dict.fetch "url" "" srcData)
username = Dict.fetch "username" "" srcData
password = Dict.fetch "password" "" srcData
authPrefix = case ( username, password ) of ( "", "" ) -> ""
( u, p ) -> u ++ ":" ++ p
authBit = if addAuth && String.length authPrefix > 0 then "?basic_auth=" ++ Url.percentEncode (Base64.encode authPrefix)
else ""
encodedPath = path |> String.chopStart "/" |> String.split "/" |> List.map Url.percentEncode |> String.join "/" in host ++ "/" ++ encodedPath ++ authBit