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"
|
||||
, "either"
|
||||
, "fetch"
|
||||
, "maybe"
|
||||
, "prelude"
|
||||
, "strings"
|
||||
]
|
||||
, packages = ./packages.dhall
|
||||
, sources = [ "src/**/*.purs", "test/**/*.purs" ]
|
||||
|
|
|
@ -10,12 +10,18 @@ import Fetch (fetch)
|
|||
import Data.Binary.Base64 (decode)
|
||||
import Data.Either (Either, hush, note)
|
||||
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
|
||||
= MalformedKey
|
||||
| MalformedUrl
|
||||
|
||||
instance showAviaryError :: Show AviaryError where
|
||||
show MalformedKey = "Malformed key!!"
|
||||
show MalformedUrl = "Malformed url!!"
|
||||
|
||||
type UrlInfo =
|
||||
{ server :: String
|
||||
|
@ -24,9 +30,12 @@ type UrlInfo =
|
|||
}
|
||||
|
||||
parseUrl :: String -> Either AviaryError UrlInfo
|
||||
parseUrl url =
|
||||
let { after, before } = splitAt (indexOf' (Pattern "/") 8 url) url
|
||||
in { server: before, fileId: after, key: after }
|
||||
parseUrl url = note MalformedUrl do
|
||||
slashIndex <- indexOf' (Pattern "/") 8 url
|
||||
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 key = note MalformedKey $ hush $ decode key
|
||||
|
|
Loading…
Reference in a new issue