A music player that connects to your cloud/distributed storage.
1module Sources.Services.AmazonS3 exposing (defaults, initialData, makeTrackUrl, makeTree, parseErrorResponse, parsePreparationResponse, parseTreeResponse, postProcessTree, prepare, properties)
2
3{-| Amazon S3 Service.
4
5Resources:
6
7 - <http://docs.aws.amazon.com/AmazonS3/latest/API/sigv4-query-string-auth.html>
8
9-}
10
11import Common
12import Dict
13import Http
14import Sources exposing (Property, SourceData)
15import Sources.Pick
16import Sources.Processing exposing (..)
17import Sources.Services.AmazonS3.Parser as Parser
18import Sources.Services.AmazonS3.Presign exposing (..)
19import Sources.Services.Common exposing (cleanPath, noPrep)
20import Time
21
22
23
24-- PROPERTIES
25-- 📟
26
27
28defaults =
29 { bucketName = "music"
30 , name = "Music from Amazon S3"
31 , region = "eu-west-1"
32 }
33
34
35{-| The list of properties we need from the user.
36-}
37properties : List Property
38properties =
39 [ { key = "accessKey"
40 , label = "Access key"
41 , placeholder = "Fv6EWfLfCcMo"
42 , password = True
43 }
44 , { key = "secretKey"
45 , label = "Secret key"
46 , placeholder = "qeNcqiMpgqC8"
47 , password = True
48 }
49 , { key = "bucketName"
50 , label = "Bucket name"
51 , placeholder = "music"
52 , password = False
53 }
54 , { key = "region"
55 , label = "Region"
56 , placeholder = defaults.region
57 , password = False
58 }
59 , { key = "directoryPath"
60 , label = "Directory (Optional)"
61 , placeholder = "/"
62 , password = False
63 }
64 , { key = "host"
65 , label = "Host (Optional)"
66 , placeholder = "http://localhost:9000"
67 , password = False
68 }
69 ]
70
71
72{-| Initial data set.
73-}
74initialData : SourceData
75initialData =
76 Dict.fromList
77 [ ( "accessKey", "" )
78 , ( "bucketName", defaults.bucketName )
79 , ( "directoryPath", "" )
80 , ( "host", "" )
81 , ( "name", defaults.name )
82 , ( "region", defaults.region )
83 , ( "secretKey", "" )
84 ]
85
86
87
88-- PREPARATION
89
90
91prepare : String -> SourceData -> Marker -> (Result Http.Error String -> msg) -> Maybe (Cmd msg)
92prepare _ _ _ _ =
93 Nothing
94
95
96
97-- TREE
98
99
100{-| Create a directory tree.
101
102List all the tracks in the bucket.
103Or a specific directory in the bucket.
104
105-}
106makeTree : SourceData -> Marker -> Time.Posix -> (Result Http.Error String -> msg) -> Cmd msg
107makeTree srcData marker currentTime resultMsg =
108 let
109 directoryPath =
110 srcData
111 |> Dict.get "directoryPath"
112 |> Maybe.withDefault ""
113 |> cleanPath
114
115 initialParams =
116 [ ( "list-type", "2" )
117 , ( "max-keys", "500" )
118 ]
119
120 prefix =
121 if String.length directoryPath > 0 then
122 [ ( "prefix", directoryPath ) ]
123
124 else
125 []
126
127 continuation =
128 case marker of
129 InProgress s ->
130 [ ( "continuation-token", s ) ]
131
132 _ ->
133 []
134
135 params =
136 initialParams ++ prefix ++ continuation
137
138 url =
139 presignedUrl Get (60 * 5) params currentTime srcData "/"
140 in
141 Http.get
142 { url = url
143 , expect = Http.expectStringResponse resultMsg Common.translateHttpResponse
144 }
145
146
147{-| Re-export parser functions.
148-}
149parsePreparationResponse : String -> Time.Posix -> SourceData -> Marker -> PrepationAnswer Marker
150parsePreparationResponse =
151 noPrep
152
153
154parseTreeResponse : String -> Marker -> TreeAnswer Marker
155parseTreeResponse =
156 Parser.parseTreeResponse
157
158
159parseErrorResponse : String -> Maybe String
160parseErrorResponse =
161 Parser.parseErrorResponse
162
163
164
165-- POST
166
167
168{-| Post process the tree results.
169
170Make sure we only use music files that we can use.
171
172-}
173postProcessTree : List String -> List String
174postProcessTree =
175 Sources.Pick.selectMusicFiles
176
177
178
179-- TRACK URL
180
181
182{-| Create a public url for a file.
183
184We need this to play the track.
185Creates a presigned url that's valid for 48 hours
186
187-}
188makeTrackUrl : Time.Posix -> String -> SourceData -> HttpMethod -> String -> String
189makeTrackUrl currentTime _ srcData method pathToFile =
190 presignedUrl method 172800 [] currentTime srcData pathToFile