URL-shape recognition for the Cloudinary delivery URL grammar.
Cloudinary URLs have the shape:
<host>/<account>/<resource-type>/<delivery>/[<signature>/]<transforms>/<source>with:
<account>— the cloud name (e.g.demo).<resource-type>—image,video,raw. v0.1 supportsimage.<delivery>—upload,fetch,private,authenticated,sprite,facebook, etc. v0.1 recognises any value but onlyuploadandfetchare exercised by tests.<signature>— optionals--<hex>--segment for signed URLs.<transforms>— zero or more comma-separated transform stages, each separated by/. v0.1 collapses multi-stage transforms by concatenating with commas; the canonical IR doesn't model chained transforms as v0.1.<source>— the public id (with extension); fordelivery=fetchit's a percent-encoded HTTPS URL.
Unlike imgix and Cloudflare, Cloudinary embeds account + delivery segments in the path, so the recogniser strips them before reaching the source.
Summary
Types
The recognised URL shape.
Types
@type recognised() :: %{ shape: :cloudinary, options: String.t(), source: Image.Plug.Source.t(), account: String.t() | nil, resource_type: String.t(), delivery: String.t(), signature: String.t() | nil }
The recognised URL shape.
:optionsis the joined transform string (multiple stages comma- flattened) and may be empty.:sourceis the resolved source.:account,:resource_type,:delivery,:signatureare the parsed path segments. Used by the wiring module to verify signatures and to reconstruct the canonical-string for HMAC verification.
Functions
@spec parse( Plug.Conn.t(), keyword() ) :: {:ok, recognised()} | {:error, Image.Plug.Error.t()}
Parses the request path of a Plug.Conn into a recognised URL shape.
Arguments
connis aPlug.Connstruct.optionsis a keyword list. The following keys are honoured:
Options
:mount— string path prefix the plug is mounted under. Stripped before treating the rest as the cloudinary path. Defaults to"".:account— when set, asserts the URL's account segment matches this value. Whennil(default), any account is accepted and the parsed value is reported in the recognised shape.
Returns
{:ok, recognised}on a successful match.{:error, %Image.Plug.Error{tag: :malformed_url}}when the path doesn't sit under the mount or has too few segments.
Examples
iex> conn = %Plug.Conn{
...> path_info: ["demo", "image", "upload", "w_200,c_fill", "sample.jpg"],
...> request_path: "/demo/image/upload/w_200,c_fill/sample.jpg",
...> query_string: ""
...> }
iex> {:ok, parsed} = Image.Plug.Provider.Cloudinary.URL.parse(conn, [])
iex> parsed.options
"w_200,c_fill"
iex> parsed.delivery
"upload"
iex> parsed.source.ref
"/sample.jpg"