A music player that connects to your cloud/distributed storage.
1module Sources.Services.AzureBlob exposing (defaults, initialData, makeTrackUrl, makeTree, parseErrorResponse, parsePreparationResponse, parseTreeResponse, postProcessTree, prepare, properties)
2
3{-| Microsoft Azure Blob Service.
4
5Resources:
6
7 - <https://docs.microsoft.com/en-us/rest/api/storageservices/blob-service-rest-api>
8
9-}
10
11import Common
12import Dict
13import Http
14import Sources exposing (Property, SourceData)
15import Sources.Pick
16import Sources.Processing exposing (..)
17import Sources.Services.Azure.Authorization exposing (..)
18import Sources.Services.Azure.BlobParser as Parser
19import Sources.Services.Common exposing (cleanPath, noPrep)
20import Time
21
22
23
24-- PROPERTIES
25-- 📟
26
27
28defaults =
29 { name = "Music from Azure Blob Storage"
30 }
31
32
33{-| The list of properties we need from the user.
34
35Tuple: (property, label, placeholder, isPassword)
36Will be used for the forms.
37
38-}
39properties : List Property
40properties =
41 [ { key = "accountName"
42 , label = "Account name"
43 , placeholder = "myaccount"
44 , password = False
45 }
46 , { key = "accountKey"
47 , label = "Account key"
48 , placeholder = "MXFPDkaN4KBT"
49 , password = True
50 }
51 , { key = "container"
52 , label = "Container"
53 , placeholder = "music"
54 , password = False
55 }
56 , { key = "directoryPath"
57 , label = "Directory (aka. Prefix, Optional)"
58 , placeholder = "/"
59 , password = False
60 }
61 ]
62
63
64{-| Initial data set.
65-}
66initialData : SourceData
67initialData =
68 Dict.fromList
69 [ ( "accountName", "" )
70 , ( "accountKey", "" )
71 , ( "container", "" )
72 , ( "directoryPath", "" )
73 , ( "name", defaults.name )
74 ]
75
76
77
78-- PREPARATION
79
80
81prepare : String -> SourceData -> Marker -> (Result Http.Error String -> msg) -> Maybe (Cmd msg)
82prepare _ _ _ _ =
83 Nothing
84
85
86
87-- TREE
88
89
90{-| Create a directory tree.
91
92List all the tracks in the container.
93Or a specific directory in the container.
94
95-}
96makeTree : SourceData -> Marker -> Time.Posix -> (Result Http.Error String -> msg) -> Cmd msg
97makeTree srcData marker currentTime resultMsg =
98 let
99 directoryPath =
100 srcData
101 |> Dict.get "directoryPath"
102 |> Maybe.withDefault ""
103 |> cleanPath
104
105 baseParams =
106 [ ( "maxresults", "1000" ) ]
107
108 params =
109 case marker of
110 InProgress s ->
111 [ ( "marker", s ) ]
112
113 _ ->
114 []
115
116 url =
117 presignedUrl Blob List Get 1 currentTime srcData directoryPath (baseParams ++ params)
118 in
119 Http.get
120 { url = url
121 , expect = Http.expectStringResponse resultMsg Common.translateHttpResponse
122 }
123
124
125{-| Re-export parser functions.
126-}
127parsePreparationResponse : String -> Time.Posix -> SourceData -> Marker -> PrepationAnswer Marker
128parsePreparationResponse =
129 noPrep
130
131
132parseTreeResponse : String -> Marker -> TreeAnswer Marker
133parseTreeResponse =
134 Parser.parseTreeResponse
135
136
137parseErrorResponse : String -> Maybe String
138parseErrorResponse =
139 Parser.parseErrorResponse
140
141
142
143-- POST
144
145
146{-| Post process the tree results.
147
148!!! Make sure we only use music files that we can use.
149
150-}
151postProcessTree : List String -> List String
152postProcessTree =
153 Sources.Pick.selectMusicFiles
154
155
156
157-- TRACK URL
158
159
160{-| Create a public url for a file.
161
162We need this to play the track.
163(!) Creates a presigned url that's valid for 48 hours
164
165-}
166makeTrackUrl : Time.Posix -> String -> SourceData -> HttpMethod -> String -> String
167makeTrackUrl currentTime _ srcData _ pathToFile =
168 presignedUrl Blob Read Get 48 currentTime srcData pathToFile []