Builds URLs for any disk, served by Fil.Plug from your application.
disk =
Fil.disk(adapter: Fil.Adapter.Local, root: "priv/storage/uploads")
|> Fil.Plugin.URL.attach(base_url: "http://localhost:4000/storage/uploads", secret: secret)
Fil.url(disk, "avatars/1.png")
#=> {:ok, "http://localhost:4000/storage/uploads/avatars/1.png"}
Fil.signed_url(disk, "avatars/1.png")
#=> {:ok, "http://localhost:4000/storage/uploads/avatars/1.png?expires=...&signature=..."}The plugin answers Fil.url/2 and, with a :secret, Fil.signed_url/3 and Fil.signed_upload/3 itself, so the
adapter never runs. That's how local and memory disks get URLs, since there is no storage service that could build
them. A public URL only works where Fil.Plug serves the disk with public: true (or something else serves the files
at :base_url).
On an S3 disk, :base_url replaces the bucket URL in Fil.url/2, for a CDN in front of the bucket. Without a
:secret, S3 still presigns Fil.signed_url/3 and Fil.signed_upload/3 itself. With one, signed URLs go to your
application, which then streams the file through: useful when the bucket shouldn't be reachable from outside, but
every download and upload runs through your application.
A signed URL is :base_url, the path, and the query parameters expires (Unix seconds), disposition (the
content-disposition header value, only with disposition:), content_type, size and if_exists=error (only for
uploads with those options, which Fil.Plug enforces), those of query:, and signature: an HMAC-SHA256 over the
method, the URL path, the expiry and every other parameter. A request with a parameter that wasn't signed is refused,
as on S3. expires_in: is capped at 7 days, as on S3, so a URL that works on one disk works on every disk.
Options
:base_url(String.t/0) - Required. The URL the disk root is served at, e.g."http://localhost:4000/storage/uploads":Fil.Plugin your application, or a CDN in front of the disk.:secret- The key URLs are signed with, at least 32 random bytes (mix phx.gen.secretprints 64).Fil.Plugreads it from the disk, so it's only configured here. Without it, the plugin only builds public URLs, andFil.signed_url/3goes to the adapter.
Summary
Functions
Attaches the plugin to disk under the name Fil.Plugin.URL.
Functions
@spec attach(Fil.Disk.t(), keyword()) :: Fil.Disk.t()
Attaches the plugin to disk under the name Fil.Plugin.URL.
The same as plugins: [{Fil.Plugin.URL, :call, opts}] in Fil.disk/1, except that the options are validated here
instead of on the first URL.