Fix parseUrl

This commit is contained in:
Emi Simpson 2022-10-19 17:39:04 -04:00
parent 03b0c2ef12
commit 379260d457
Signed by: Emi
GPG Key ID: A12F2C2FFDC3D847
2 changed files with 14 additions and 3 deletions

View File

@ -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" ]

View File

@ -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