Skip to content

Instantly share code, notes, and snippets.

@myobie
Last active March 14, 2018 17:13
Show Gist options
  • Select an option

  • Save myobie/39e2bf2b3e9a12ce5db6477220327b3c to your computer and use it in GitHub Desktop.

Select an option

Save myobie/39e2bf2b3e9a12ce5db6477220327b3c to your computer and use it in GitHub Desktop.
defmodule Someapp.FileStore do
@type key :: String.t()
@type data :: binary
@type options :: keyword
@callback put(key, data, options) :: :ok | {:error, atom}
@callback get(key) :: {:ok, data} | {:error, atom}
@callback download(key, Path.t()) :: :ok | {:error, atom}
@callback upload(key, Path.t(), options) :: :ok | {:error, atom}
@callback stream_down(Plug.Conn.t(), key, options) :: Plug.Conn.t() | no_return
@spec config() :: keyword
def config do
Application.get_env(:someapp, __MODULE__, [])
end
@spec adapter() :: __MODULE__
def adapter do
config() |> Keyword.get(:adapter, Shareup.MissingFileStore)
end
end
defmodule Shareup.MissingFileStore do
@behaviour Shareup.FileStore
@error {:error, :no_file_store_adapter_specified_in_config}
def put(_key, _data, _options), do: @error
def get(_key), do: @error
def download(_key, _path), do: @error
def upload(_key, _path, _options), do: @error
def stream_down(conn, _key, _options) do
Plug.Conn.send_resp(conn, 500, inspect(@error))
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment