This is an alternative site for discovering Elm packages. You may be looking for the official Elm package site instead.
README

Cloudinary

This library provides a user friendly interface to upload your photos to Cloudinary.

Definition

type alias Settings = { username : String , apiKey : String , timestamp : Int , signature : String }

Handle all Cloudinary settings used in upload request to Cloudinary API. The signature must be produced by the backend, using public API key, and timestamp. It is a SHA1 algorithm, consisting of the string "timestamp=XXXXXXX{APIKEY}", where XXXXXXX is the timestamp, and {APIKEY} is your private API key (which should not be released on your website). Such a signature remains valid one hour after the timestamp.

Upload

uploadPhoto : Settings -> (Result Http.Error String -> msg) -> String -> Cmd msg

Upload the photo provided into parameter, and returns it's public ID. At the moment, only the public ID is returned, but it can be improved in a near future. uploadPhoto accepts any file, encoded in base64. You can get it easily via ports for your uploads.

trackUploadPhoto : Settings -> (Progress.Progress String -> msg) -> String -> Sub msg

Upload the photo provided into parameter, and returns it's public ID. trackUploadPhoto accepts any file, encoded in base64. You can get it easily via ports for your uploads. trackUploadPhoto allows to track the HTTP transfer. You should provide a msg to get those informations.

Access Uploaded Images

toUrl : Settings -> Id -> Url

Access the uploaded photo on Cloudinary servers. Provide the ID to get the access link. Does not handle transformation on the fly for the moment.

module Cloudinary exposing
  ( Settings
  , uploadPhoto
  , trackUploadPhoto
  , toUrl
  )

{-| This library provides a user friendly interface to upload your photos to Cloudinary.

# Definition
@docs Settings

# Upload
@docs uploadPhoto
@docs trackUploadPhoto

# Access Uploaded Images
@docs toUrl

-}

import Http
import Http.Progress as Progress
import Json.Decode as Decode exposing (Decoder)
import Json.Encode as Encode


type alias Url =
  String
type alias Id =
  String

{-| Handle all Cloudinary settings used in upload request to Cloudinary API.
The signature must be produced by the backend, using public API key, and timestamp. It is a SHA1 algorithm, consisting of the string "timestamp=XXXXXXX{APIKEY}", where XXXXXXX is the timestamp, and {APIKEY} is your private API key (which should not be released on your website). Such a signature remains valid one hour after the timestamp.
-}
type alias Settings =
  { username : String
  , apiKey : String
  , timestamp : Int
  , signature : String
  }


{-| Upload the photo provided into parameter, and returns it's public ID.
At the moment, only the public ID is returned, but it can be improved in a near future.
uploadPhoto accepts any file, encoded in base64. You can get it easily via ports for your uploads. -}
uploadPhoto : Settings -> (Result Http.Error String -> msg) -> String -> Cmd msg
uploadPhoto ({ username } as settings) msg =
  Http.send msg
    << upload (uploadUrl username) decodeResponse
    << encodeUploadBody settings

{-| Upload the photo provided into parameter, and returns it's public ID.
trackUploadPhoto accepts any file, encoded in base64. You can get it easily via ports for your uploads.
trackUploadPhoto allows to track the HTTP transfer. You should provide a msg to get those informations. -}
trackUploadPhoto : Settings -> (Progress.Progress String -> msg) -> String -> Sub msg
trackUploadPhoto ({ username } as settings) msg =
  let url = uploadUrl username in
  encodeUploadBody settings
    >> upload url decodeResponse
    >> Progress.track url msg

upload : Url -> Decoder String -> Http.Body -> Http.Request String
upload baseUrl =
  flip (Http.post baseUrl)

uploadUrl : String -> String
uploadUrl username =
  "https://api.cloudinary.com/v1_1/" ++ username ++ "/auto/upload"

encodeUploadBody : Settings -> String -> Http.Body
encodeUploadBody { apiKey, timestamp, signature } file =
  Http.jsonBody <|
    Encode.object
      [ ( "file", Encode.string file )
      , ( "api_key", Encode.string apiKey )
      , ( "timestamp", Encode.int timestamp )
      , ( "signature", Encode.string signature )
      ]

decodeResponse : Decoder Id
decodeResponse =
  Decode.field "public_id" Decode.string

{-| Access the uploaded photo on Cloudinary servers. Provide the ID to get the
access link. Does not handle transformation on the fly for the moment. -}
toUrl : Settings -> Id -> Url
toUrl { username } id =
  "https://res.cloudinary.com/" ++ username ++ "/image/upload/" ++ id