Packages
plug
0.13.1
1.20.3
1.20.2
1.20.1
retired
1.20.0
retired
1.19.5
1.19.4
1.19.3
1.19.2
1.19.1
1.19.0
1.18.5
1.18.4
1.18.3
1.18.2
1.18.1
1.18.0
1.17.4
1.17.3
1.17.2
1.17.1
1.17.0
1.16.6
1.16.5
1.16.4
1.16.3
1.16.2
1.16.1
1.16.0
1.15.6
1.15.5
1.15.4
1.15.3
1.15.2
1.15.1
1.15.0
1.14.2
1.14.1
1.14.0
1.13.6
1.13.5
1.13.4
1.13.3
1.13.2
1.13.1
retired
1.13.0
retired
1.12.1
1.12.0
1.11.1
1.11.0
1.10.4
1.10.3
1.10.2
1.10.1
1.10.0
1.9.0
1.8.3
1.8.2
1.8.1
1.8.0
1.7.2
1.7.1
1.7.0
1.6.4
1.6.3
1.6.2
1.6.1
1.6.0
1.5.1
1.5.0
1.5.0-rc.2
1.5.0-rc.1
1.5.0-rc.0
1.4.5
1.4.4
1.4.3
1.4.2
1.4.1
1.4.0
1.4.0-rc.0
1.3.6
1.3.5
1.3.4
1.3.3
1.3.2
1.3.1
1.3.0
1.2.6
1.2.5
1.2.4
1.2.3
1.2.2
1.2.1
1.2.0
1.2.0-rc.0
1.1.9
1.1.8
1.1.7
1.1.6
1.1.5
1.1.4
1.1.3
1.1.2
1.1.1
1.1.0
1.0.6
1.0.5
1.0.4
1.0.3
1.0.2
1.0.1
1.0.0
0.14.0
0.13.1
0.13.0
0.12.2
0.12.1
0.12.0
0.11.3
0.11.2
0.11.1
0.11.0
0.10.0
0.9.0
0.8.4
0.8.3
0.8.2
0.8.1
0.8.0
0.7.0
0.6.0
0.5.3
0.5.2
0.5.1
0.5.0
0.4.4
0.4.3
0.4.2
0.4.1
Compose web applications with functions
Security advisory:
This version has known vulnerabilities.
View advisories
Current section
Files
Jump to
Current section
Files
lib/plug/upload.ex
defmodule Plug.Upload do
@moduledoc """
A server (a `GenServer` specifically) that manages uploaded files.
Uploaded files are stored in a temporary directory
and removed from that directory after the process that
requested the file dies.
During the request, files are represented with
a `Plug.Upload` struct that contains three fields:
* `:path` - the path to the uploaded file on the filesystem
* `:content_type` - the content type of the uploaded file
* `:filename` - the filename of the uploaded file given in the request
**Note**: as mentioned in the documentation for `Plug.Parsers`, the `:plug`
application has to be started in order to upload files and use the
`Plug.Upload` module.
"""
defstruct [:path, :content_type, :filename]
@type t :: %__MODULE__{
path: Path.t,
filename: binary,
content_type: binary | nil
}
@doc """
Requests a random file to be created in the upload directory
with the given prefix.
"""
@spec random_file(binary) ::
{:ok, binary} |
{:too_many_attempts, binary, pos_integer} |
{:no_tmp, [binary]}
def random_file(prefix) do
GenServer.call(plug_server, {:random, prefix})
end
@doc """
Requests a random file to be created in the upload directory
with the given prefix. Raises on failure.
"""
@spec random_file!(binary) :: binary | no_return
def random_file!(prefix) do
case random_file(prefix) do
{:ok, path} ->
path
{:too_many_attempts, tmp, attempts} ->
raise "tried #{attempts} times to create an uploaded file at #{tmp} but failed. What gives?"
{:no_tmp, _tmps} ->
raise "could not create a tmp directory to store uploads. Set PLUG_TMPDIR to a directory with write permission"
end
end
defp plug_server do
Process.whereis(__MODULE__) ||
raise "could not find process Plug.Upload. Have you started the :plug application?"
end
use GenServer
@doc """
Starts the upload handling server.
"""
def start_link() do
GenServer.start_link(__MODULE__, :ok, [name: __MODULE__])
end
## Callbacks
@temp_env_vars ~w(PLUG_TMPDIR TMPDIR TMP TEMP)s
@max_attempts 10
@doc false
def init(:ok) do
tmp = Enum.find_value @temp_env_vars, "/tmp", &System.get_env/1
cwd = Path.join(File.cwd!, "tmp")
ets = :ets.new(:plug_uploads, [:private])
{:ok, {[tmp, cwd], ets}}
end
@doc false
def handle_call({:random, prefix}, {pid, _ref}, {tmps, ets} = state) do
case find_tmp_dir(pid, tmps, ets) do
{:ok, tmp, paths} ->
{:reply, open_random_file(prefix, tmp, 0, pid, ets, paths), state}
{:no_tmp, _} = error ->
{:reply, error, state}
end
end
def handle_call(msg, from, state) do
super(msg, from, state)
end
@doc false
def handle_info({:DOWN, _ref, :process, pid, _reason}, {_, ets} = state) do
case :ets.lookup(ets, pid) do
[{pid, _tmp, paths}] ->
:ets.delete(ets, pid)
Enum.each paths, &:file.delete/1
[] ->
:ok
end
{:noreply, state}
end
def handle_info(msg, state) do
super(msg, state)
end
## Helpers
defp find_tmp_dir(pid, tmps, ets) do
case :ets.lookup(ets, pid) do
[{^pid, tmp, paths}] ->
{:ok, tmp, paths}
[] ->
if tmp = ensure_tmp_dir(tmps) do
:erlang.monitor(:process, pid)
:ets.insert(ets, {pid, tmp, []})
{:ok, tmp, []}
else
{:no_tmp, tmps}
end
end
end
defp ensure_tmp_dir(tmps) do
{mega, _, _} = :os.timestamp
subdir = "/plug-" <> i(mega)
Enum.find_value(tmps, &write_tmp_dir(&1 <> subdir))
end
defp write_tmp_dir(path) do
case File.mkdir_p(path) do
:ok -> path
{:error, _} -> nil
end
end
defp open_random_file(prefix, tmp, attempts, pid, ets, paths) when attempts < @max_attempts do
path = path(prefix, tmp)
case :file.write_file(path, "", [:write, :raw, :exclusive, :binary]) do
:ok ->
:ets.update_element(ets, pid, {3, [path|paths]})
{:ok, path}
{:error, reason} when reason in [:eexist, :eaccess] ->
open_random_file(prefix, tmp, attempts + 1, pid, ets, paths)
end
end
defp open_random_file(_prefix, tmp, attempts, _pid, _ets, _paths) do
{:too_many_attempts, tmp, attempts}
end
@compile {:inline, i: 1}
defp i(integer), do: Integer.to_string(integer)
defp path(prefix, tmp) do
{_mega, sec, micro} = :os.timestamp
scheduler_id = :erlang.system_info(:scheduler_id)
tmp <> "/" <> prefix <> "-" <> i(sec) <> "-" <> i(micro) <> "-" <> i(scheduler_id)
end
end