2017-05-16 12:54:13 +00:00
|
|
|
defmodule Asciinema.FileStore do
|
2017-06-07 17:53:12 +00:00
|
|
|
@doc "Puts file at given path in store"
|
|
|
|
@callback put_file(dst_path :: String.t, src_local_path :: String.t, content_type :: String.t) :: :ok | {:error, term}
|
|
|
|
|
2017-05-16 12:54:13 +00:00
|
|
|
@doc "Serves file at given path in store"
|
|
|
|
@callback serve_file(conn :: %Plug.Conn{}, path :: String.t, filename :: String.t) :: %Plug.Conn{}
|
2017-05-21 11:26:41 +00:00
|
|
|
|
|
|
|
@doc "Opens the given path in store"
|
|
|
|
@callback open(path :: String.t) :: {:ok, File.io_device} | {:error, File.posix}
|
2017-06-01 07:35:47 +00:00
|
|
|
|
|
|
|
@doc "Opens the given path in store, executes given fn and closes the file"
|
|
|
|
@callback open(path :: String.t, function :: (File.io_device -> res)) :: {:ok, res} | {:error, File.posix} when res: var
|
2017-06-07 17:53:12 +00:00
|
|
|
|
|
|
|
def put_file(dst_path, src_local_path, content_type) do
|
2017-07-07 12:09:49 +00:00
|
|
|
instance().put_file(dst_path, src_local_path, content_type)
|
|
|
|
end
|
|
|
|
|
|
|
|
def open_file(path, f) do
|
|
|
|
instance().open(path, f)
|
|
|
|
end
|
|
|
|
|
|
|
|
def download_file(store_path, local_path) do
|
|
|
|
case open_file(store_path, &(:file.copy(&1, local_path))) do
|
|
|
|
{:ok, {:ok, _}} -> :ok
|
|
|
|
otherwise -> otherwise
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defp instance do
|
|
|
|
Application.get_env(:asciinema, :file_store)
|
2017-06-07 17:53:12 +00:00
|
|
|
end
|
2017-05-16 12:54:13 +00:00
|
|
|
end
|