corpus

self-hosted listenbrainz and last.fm proxy
Log | Files | Refs | README | LICENSE

S3.purs (2194B)


      1 module S3 where
      2 
      3 import Prelude
      4 
      5 import Config (S3Config)
      6 import Data.Either (Either(..))
      7 import Data.Function.Uncurried (Fn2, Fn3, Fn5, runFn2, runFn3, runFn5)
      8 import Data.Maybe (Maybe(..))
      9 import Data.Nullable (Nullable, toMaybe, toNullable)
     10 import Effect (Effect)
     11 import Effect.Aff (Aff, makeAff, nonCanceler)
     12 import Effect.Exception (Error)
     13 import Node.Buffer (Buffer)
     14 
     15 type S3ConfigJs =
     16   { bucket :: Nullable String
     17   , region :: String
     18   , accessKeyId :: Nullable String
     19   , secretAccessKey :: Nullable String
     20   , endpointUrl :: Nullable String
     21   , addressingStyle :: Nullable String
     22   }
     23 
     24 toJs :: S3Config -> S3ConfigJs
     25 toJs cfg =
     26   { bucket: toNullable cfg.bucket
     27   , region: cfg.region
     28   , accessKeyId: toNullable cfg.accessKeyId
     29   , secretAccessKey: toNullable cfg.secretAccessKey
     30   , endpointUrl: toNullable cfg.endpointUrl
     31   , addressingStyle: toNullable cfg.addressingStyle
     32   }
     33 
     34 foreign import uploadToS3Impl
     35   :: Fn5 S3ConfigJs String Buffer String (Nullable Error -> Effect Unit) (Effect Unit)
     36 
     37 foreign import existsInS3Impl
     38   :: Fn3 S3ConfigJs String (Nullable Error -> Boolean -> Effect Unit) (Effect Unit)
     39 
     40 foreign import getPresignedUrlImpl
     41   :: Fn3 S3ConfigJs String (Nullable Error -> String -> Effect Unit) (Effect Unit)
     42 
     43 foreign import getS3UrlImpl :: Fn2 S3ConfigJs String String
     44 
     45 uploadToS3 :: S3Config -> String -> Buffer -> String -> Aff Unit
     46 uploadToS3 cfg key body contentType = makeAff \cb -> do
     47   runFn5 uploadToS3Impl (toJs cfg) key body contentType \err ->
     48     case toMaybe err of
     49       Just e -> cb (Left e)
     50       Nothing -> cb (Right unit)
     51   pure nonCanceler
     52 
     53 existsInS3 :: S3Config -> String -> Aff Boolean
     54 existsInS3 cfg key = makeAff \cb -> do
     55   runFn3 existsInS3Impl (toJs cfg) key \err exists ->
     56     case toMaybe err of
     57       Just e -> cb (Left e)
     58       Nothing -> cb (Right exists)
     59   pure nonCanceler
     60 
     61 getPresignedUrl :: S3Config -> String -> Aff String
     62 getPresignedUrl cfg key = makeAff \cb -> do
     63   runFn3 getPresignedUrlImpl (toJs cfg) key \err url ->
     64     case toMaybe err of
     65       Just e -> cb (Left e)
     66       Nothing -> cb (Right url)
     67   pure nonCanceler
     68 
     69 getS3Url :: S3Config -> String -> String
     70 getS3Url cfg key = runFn2 getS3UrlImpl (toJs cfg) key