1module Sources exposing (..)
2
3import Conditional exposing (..)
4import Dict exposing (Dict)
5import Json.Decode
6import Time
7
8
9
10-- 🌳
11
12
13type alias Source =
14 { id : String
15 , data : SourceData
16 , directoryPlaylists : Bool
17 , enabled : Bool
18 , service : Service
19 }
20
21
22
23-- PIECES
24
25
26type alias Property =
27 { key : String
28 , label : String
29 , placeholder : String
30 , password : Bool
31 }
32
33
34type alias SourceData =
35 Dict String String
36
37
38
39-- SERVICES
40
41
42type Service
43 = AmazonS3
44 | AzureBlob
45 | AzureFile
46 | Dropbox
47 | Google
48 | Ipfs
49 | WebDav
50
51
52serviceDictionary : Dict String Service
53serviceDictionary =
54 Dict.fromList
55 [ ( "amazons3", AmazonS3 )
56 , ( "amazon_s3", AmazonS3 )
57 , ( "azureblob", AzureBlob )
58 , ( "azure_blob", AzureBlob )
59 , ( "azurefile", AzureFile )
60 , ( "azure_file", AzureFile )
61 , ( "dropbox", Dropbox )
62 , ( "google", Google )
63 , ( "ipfs", Ipfs )
64 , ( "webdav", WebDav )
65 , ( "web_dav", WebDav )
66 ]
67
68
69serviceDecoder : Json.Decode.Decoder Service
70serviceDecoder =
71 Json.Decode.andThen
72 (\string ->
73 serviceDictionary
74 |> Dict.get string
75 |> Maybe.map Json.Decode.succeed
76 |> Maybe.withDefault (Json.Decode.fail "Invalid source kind")
77 )
78 Json.Decode.string
79
80
81
82--- 🔱
83
84
85enabledSourceIds : List Source -> List String
86enabledSourceIds =
87 List.filterMap (\s -> ifThenElse s.enabled (Just s.id) Nothing)
88
89
90setProperId : Int -> Time.Posix -> Source -> Source
91setProperId n time source =
92 { source | id = String.fromInt (Time.posixToMillis time) ++ String.fromInt n }
93
94
95worksOffline : Source -> Bool
96worksOffline source =
97 case source.service of
98 AmazonS3 ->
99 False
100
101 AzureBlob ->
102 False
103
104 AzureFile ->
105 False
106
107 Dropbox ->
108 False
109
110 Google ->
111 False
112
113 Ipfs ->
114 True
115
116 WebDav ->
117 source.data
118 |> Dict.get "url"
119 |> Maybe.map (\u -> String.contains "localhost" u || String.contains "127.0.0.1" u)
120 |> Maybe.withDefault False