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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191module Sources.Services.AmazonS3 exposing (defaults, initialData, makeTrackUrl, makeTree, parseErrorResponse, parsePreparationResponse, parseTreeResponse, postProcessTree, prepare, properties)
{-| Amazon S3 Service.
Resources:
- <http://docs.aws.amazon.com/AmazonS3/latest/API/sigv4-query-string-auth.html>
-}
import Commonimport Dictimport Httpimport Sources exposing (Property, SourceData)import Sources.Pickimport Sources.Processing exposing (..)import Sources.Services.AmazonS3.Parser as Parserimport Sources.Services.AmazonS3.Presign exposing (..)import Sources.Services.Common exposing (cleanPath, noPrep)import Time
-- PROPERTIES-- 📟
defaults = { bucketName = "music" , name = "Music from Amazon S3" , region = "eu-west-1" }
{-| The list of properties we need from the user.-}properties : List Propertyproperties = [ { key = "accessKey" , label = "Access key" , placeholder = "Fv6EWfLfCcMo" , password = True } , { key = "secretKey" , label = "Secret key" , placeholder = "qeNcqiMpgqC8" , password = True } , { key = "bucketName" , label = "Bucket name" , placeholder = "music" , password = False } , { key = "region" , label = "Region" , placeholder = defaults.region , password = False } , { key = "directoryPath" , label = "Directory (Optional)" , placeholder = "/" , password = False } , { key = "host" , label = "Host (Optional)" , placeholder = "http://localhost:9000" , password = False } ]
{-| Initial data set.-}initialData : SourceDatainitialData = Dict.fromList [ ( "accessKey", "" ) , ( "bucketName", defaults.bucketName ) , ( "directoryPath", "" ) , ( "host", "" ) , ( "name", defaults.name ) , ( "region", defaults.region ) , ( "secretKey", "" ) ]
-- 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 currentTime resultMsg = let directoryPath = srcData |> Dict.get "directoryPath" |> Maybe.withDefault "" |> cleanPath
initialParams = [ ( "list-type", "2" ) , ( "max-keys", "500" ) ]
prefix = if String.length directoryPath > 0 then [ ( "prefix", directoryPath ) ]
else []
continuation = case marker of InProgress s -> [ ( "continuation-token", s ) ]
_ -> []
params = initialParams ++ prefix ++ continuation
url = presignedUrl Get (60 * 5) params currentTime srcData "/" in Http.get { url = url , expect = Http.expectStringResponse resultMsg Common.translateHttpResponse }
{-| 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.Creates a presigned url that's valid for 48 hours
-}makeTrackUrl : Time.Posix -> String -> SourceData -> HttpMethod -> String -> StringmakeTrackUrl currentTime _ srcData method pathToFile = presignedUrl method 172800 [] currentTime srcData pathToFile