Fix parseUrl
This commit is contained in:
parent
03b0c2ef12
commit
379260d457
|
@ -19,7 +19,9 @@ to generate this file without the comments in this block.
|
||||||
, "effect"
|
, "effect"
|
||||||
, "either"
|
, "either"
|
||||||
, "fetch"
|
, "fetch"
|
||||||
|
, "maybe"
|
||||||
, "prelude"
|
, "prelude"
|
||||||
|
, "strings"
|
||||||
]
|
]
|
||||||
, packages = ./packages.dhall
|
, packages = ./packages.dhall
|
||||||
, sources = [ "src/**/*.purs", "test/**/*.purs" ]
|
, sources = [ "src/**/*.purs", "test/**/*.purs" ]
|
||||||
|
|
|
@ -10,12 +10,18 @@ import Fetch (fetch)
|
||||||
import Data.Binary.Base64 (decode)
|
import Data.Binary.Base64 (decode)
|
||||||
import Data.Either (Either, hush, note)
|
import Data.Either (Either, hush, note)
|
||||||
import Data.ArrayBuffer.Types (Uint8Array)
|
import Data.ArrayBuffer.Types (Uint8Array)
|
||||||
|
import Data.Maybe (Maybe(..))
|
||||||
|
import Data.ArrayBuffer.Types (ArrayBuffer, Uint8Array)
|
||||||
|
import Data.String (drop, indexOf', indexOf, splitAt)
|
||||||
|
import Data.String.Pattern (Pattern(..))
|
||||||
|
|
||||||
data AviaryError
|
data AviaryError
|
||||||
= MalformedKey
|
= MalformedKey
|
||||||
|
| MalformedUrl
|
||||||
|
|
||||||
instance showAviaryError :: Show AviaryError where
|
instance showAviaryError :: Show AviaryError where
|
||||||
show MalformedKey = "Malformed key!!"
|
show MalformedKey = "Malformed key!!"
|
||||||
|
show MalformedUrl = "Malformed url!!"
|
||||||
|
|
||||||
type UrlInfo =
|
type UrlInfo =
|
||||||
{ server :: String
|
{ server :: String
|
||||||
|
@ -24,9 +30,12 @@ type UrlInfo =
|
||||||
}
|
}
|
||||||
|
|
||||||
parseUrl :: String -> Either AviaryError UrlInfo
|
parseUrl :: String -> Either AviaryError UrlInfo
|
||||||
parseUrl url =
|
parseUrl url = note MalformedUrl do
|
||||||
let { after, before } = splitAt (indexOf' (Pattern "/") 8 url) url
|
slashIndex <- indexOf' (Pattern "/") 8 url
|
||||||
in { server: before, fileId: after, key: after }
|
let { after, before: server } = splitAt (1 + slashIndex) url
|
||||||
|
poundIndex <- indexOf (Pattern "#") after
|
||||||
|
let { after: key, before: fileId } = splitAt poundIndex after
|
||||||
|
Just { server, fileId, key: drop 1 key }
|
||||||
|
|
||||||
decodeKey :: String -> Either AviaryError Uint8Array
|
decodeKey :: String -> Either AviaryError Uint8Array
|
||||||
decodeKey key = note MalformedKey $ hush $ decode key
|
decodeKey key = note MalformedKey $ hush $ decode key
|
||||||
|
|
Loading…
Reference in a new issue