Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions lib/plug/upload.ex
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,22 @@ defmodule Plug.Upload do
end
end

@doc """
Deletes the given upload file.

Uploads are automatically removed when the current process terminates,
but you may invoke this to request the file to be removed sooner.
"""
@spec delete(t | binary) :: :ok | {:error, term}
def delete(%__MODULE__{path: path}), do: delete(path)

def delete(path) when is_binary(path) do
with :ok <- :file.delete(path, [:raw]) do
:ets.delete_object(@path_table, {self(), path})
:ok
end
end

@doc """
Assign ownership of the given upload file to another process.

Expand Down
7 changes: 7 additions & 0 deletions test/plug/upload_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ defmodule Plug.UploadTest do
end
end

test "removes the random file on request" do
{:ok, path} = Plug.Upload.random_file("sample")
File.open!(path)
:ok = Plug.Upload.delete(path)
wait_until(fn -> not File.exists?(path) end)
end

defp wait_until(fun) do
if fun.() do
:ok
Expand Down
Loading