Close tmp in-memory file after usage

ex-png
Marcin Kulik 7 years ago
parent 18b31747ee
commit bb9fd9c644

@ -4,4 +4,7 @@ defmodule Asciinema.FileStore do
@doc "Opens the given path in store"
@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

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

@ -19,12 +19,16 @@ defmodule Asciinema.FileStore.S3 do
|> redirect(external: url)
end
def open(path) do
def open(path, function \\ nil) do
response = S3.get_object(bucket(), base_path() <> path) |> ExAws.request(region: region())
case response do
{: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}
end

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

Loading…
Cancel
Save