Close tmp in-memory file after usage

This commit is contained in:
Marcin Kulik 2017-06-01 09:35:47 +02:00
parent 18b31747ee
commit bb9fd9c644
4 changed files with 18 additions and 8 deletions

View File

@ -4,4 +4,7 @@ defmodule Asciinema.FileStore do
@doc "Opens the given path in store" @doc "Opens the given path in store"
@callback open(path :: String.t) :: {:ok, File.io_device} | {:error, File.posix} @callback open(path :: String.t) :: {:ok, File.io_device} | {:error, File.posix}
@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
end end

View File

@ -22,6 +22,9 @@ defmodule Asciinema.FileStore.Local do
def open(path) do def open(path) do
File.open(base_path() <> path, [:binary, :read]) File.open(base_path() <> path, [:binary, :read])
end end
def open(path, function) do
File.open(base_path() <> path, [:binary, :read], function)
end
defp config do defp config do
Application.get_env(:asciinema, Asciinema.FileStore.Local) Application.get_env(:asciinema, Asciinema.FileStore.Local)

View File

@ -19,12 +19,16 @@ defmodule Asciinema.FileStore.S3 do
|> redirect(external: url) |> redirect(external: url)
end end
def open(path) do def open(path, function \\ nil) do
response = S3.get_object(bucket(), base_path() <> path) |> ExAws.request(region: region()) response = S3.get_object(bucket(), base_path() <> path) |> ExAws.request(region: region())
case response do case response do
{:ok, %{body: body}} -> {:ok, %{body: body}} ->
File.open(body, [:ram, :binary, :read]) if function do
File.open(body, [:ram, :binary, :read], function)
else
File.open(body, [:ram, :binary, :read])
end
{:error, reason} -> {:error, reason} ->
{:error, reason} {:error, reason}
end end

View File

@ -65,12 +65,12 @@ defmodule Asciinema.PngGenerator.A2png do
Integer.to_string(png_params.scale) Integer.to_string(png_params.scale)
] ]
with {:ok, file} <- file_store().open(path), {:ok, {:ok, _}} = file_store().open(path, &(:file.copy(&1, json_path)))
{:ok, _} <- :file.copy(file, json_path), process = Porcelain.spawn(bin_path(), args, err: :string)
process <- Porcelain.spawn(bin_path(), args, err: :string),
{:ok, %{status: 0}} <- Porcelain.Process.await(process, @a2png_timeout) do case Porcelain.Process.await(process, @a2png_timeout) do
{:ok, png_path} {:ok, %{status: 0}} ->
else {:ok, png_path}
{:ok, %Porcelain.Result{} = result} -> {:ok, %Porcelain.Result{} = result} ->
{:error, result} {:error, result}
otherwise -> otherwise ->