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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231module Sources.Services.Dropbox exposing (authorizationSourceData, authorizationUrl, defaults, getProperDirectoryPath, initialData, makeTrackUrl, makeTree, parseErrorResponse, parsePreparationResponse, parseTreeResponse, postProcessTree, prepare, properties)
{-| Dropbox Service.-}
import Base64import Commonimport Dictimport Dict.Ext as Dictimport Httpimport Json.Decodeimport Json.Encodeimport Sources exposing (Property, SourceData)import Sources.Pickimport Sources.Processing exposing (..)import Sources.Services.Common exposing (cleanPath, noPrep)import Sources.Services.Dropbox.Parser as Parserimport Time
-- PROPERTIES-- 📟
defaults = { appKey = "kwsydtrzban41zr" , name = "Music from Dropbox" }
{-| The list of properties we need from the user.
Tuple: (property, label, placeholder, isPassword)Will be used for the forms.
-}properties : List Propertyproperties = [ { key = "directoryPath" , label = "Directory (Optional)" , placeholder = "/" , password = False } , { key = "appKey" , label = "App key" , placeholder = defaults.appKey , password = False } ]
{-| Initial data set.-}initialData : SourceDatainitialData = Dict.fromList [ ( "appKey", defaults.appKey ) , ( "directoryPath", "" ) , ( "name", defaults.name ) ]
-- AUTHORIZATION
{-| Authorization url.-}authorizationUrl : SourceData -> String -> StringauthorizationUrl sourceData origin = let encodeData data = data |> Dict.toList |> List.map (Tuple.mapSecond Json.Encode.string) |> Json.Encode.object
state = sourceData |> encodeData |> Json.Encode.encode 0 |> Base64.encode in [ ( "response_type", "token" ) , ( "client_id", Dict.fetch "appKey" "unknown" sourceData ) , ( "redirect_uri", origin ++ "?path=sources/new/dropbox" ) , ( "state", state ) ] |> Common.queryString |> String.append "https://www.dropbox.com/oauth2/authorize"
{-| Authorization source data.-}authorizationSourceData : { codeOrToken : Maybe String, state : Maybe String } -> SourceDataauthorizationSourceData args = args.state |> Maybe.andThen (Base64.decode >> Result.toMaybe) |> Maybe.withDefault "{}" |> Json.Decode.decodeString (Json.Decode.dict Json.Decode.string) |> Result.withDefault Dict.empty |> Dict.unionFlipped initialData |> Dict.update "accessToken" (\_ -> args.codeOrToken)
-- PREPARATION
prepare : String -> SourceData -> Marker -> (Result Http.Error String -> msg) -> Maybe (Cmd msg)prepare _ _ _ _ = Nothing
-- TREE
{-| Create a directory tree.
List all the tracks in the bucket.Or a specific directory in the bucket.
-}makeTree : SourceData -> Marker -> Time.Posix -> (Result Http.Error String -> msg) -> Cmd msgmakeTree srcData marker _ resultMsg = let accessToken = Dict.fetch "accessToken" "" srcData
body = (case marker of TheBeginning -> [ ( "limit", Json.Encode.int 2000 ) , ( "path", Json.Encode.string (getProperDirectoryPath srcData) ) , ( "recursive", Json.Encode.bool True ) ]
InProgress cursor -> [ ( "cursor", Json.Encode.string cursor ) ]
TheEnd -> [] ) |> Json.Encode.object |> Http.jsonBody
url = case marker of TheBeginning -> "https://api.dropboxapi.com/2/files/list_folder"
InProgress _ -> "https://api.dropboxapi.com/2/files/list_folder/continue"
TheEnd -> "" in Http.request { method = "POST" , headers = [ Http.header "Authorization" ("Bearer " ++ accessToken) ] , url = url , body = body , expect = Http.expectStringResponse resultMsg Common.translateHttpResponse , timeout = Nothing , tracker = Nothing }
getProperDirectoryPath : SourceData -> StringgetProperDirectoryPath srcData = let path = srcData |> Dict.get "directoryPath" |> Maybe.withDefault "" |> cleanPath in if path == "" then ""
else "/" ++ path
{-| 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 = Sources.Pick.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 _ pathToFile = "dropbox://" ++ Dict.fetch "accessToken" "" srcData ++ "@" ++ pathToFile