-
Notifications
You must be signed in to change notification settings - Fork 574
/
upload.ex
189 lines (154 loc) · 4.9 KB
/
upload.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
defmodule Plug.UploadError do
defexception [:message]
end
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.
"""
use GenServer
defstruct [:path, :content_type, :filename]
@type t :: %__MODULE__{
path: Path.t(),
filename: binary,
content_type: binary | nil
}
@table __MODULE__
@max_attempts 10
@temp_env_vars ~w(PLUG_TMPDIR TMPDIR TMP TEMP)s
@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
case ensure_tmp() do
{:ok, tmp, paths} ->
open_random_file(prefix, tmp, 0, paths)
{:no_tmp, tmps} ->
{:no_tmp, tmps}
end
end
defp ensure_tmp() do
pid = self()
server = plug_server()
case :ets.lookup(@table, pid) do
[{^pid, tmp, paths}] ->
{:ok, tmp, paths}
[] ->
{:ok, tmps} = GenServer.call(server, :upload)
{mega, _, _} = :os.timestamp()
subdir = "/plug-" <> i(mega)
if tmp = Enum.find_value(tmps, &make_tmp_dir(&1 <> subdir)) do
true = :ets.insert_new(@table, {pid, tmp, []})
{:ok, tmp, []}
else
{:no_tmp, tmps}
end
end
end
defp make_tmp_dir(path) do
case File.mkdir_p(path) do
:ok -> path
{:error, _} -> nil
end
end
defp open_random_file(prefix, tmp, attempts, paths) when attempts < @max_attempts do
path = path(prefix, tmp)
case :file.write_file(path, "", [:write, :raw, :exclusive, :binary]) do
:ok ->
:ets.update_element(@table, self(), {3, [path | paths]})
{:ok, path}
{:error, reason} when reason in [:eexist, :eacces] ->
open_random_file(prefix, tmp, attempts + 1, paths)
end
end
defp open_random_file(_prefix, tmp, attempts, _paths) do
{:too_many_attempts, tmp, attempts}
end
defp path(prefix, tmp) do
sec = :os.system_time(:seconds)
rand = :rand.uniform(999_999_999_999_999)
scheduler_id = :erlang.system_info(:scheduler_id)
tmp <> "/" <> prefix <> "-" <> i(sec) <> "-" <> i(rand) <> "-" <> i(scheduler_id)
end
@compile {:inline, i: 1}
defp i(integer), do: Integer.to_string(integer)
@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 Plug.UploadError,
"tried #{attempts} times to create an uploaded file at #{tmp} but failed. " <>
"Set PLUG_TMPDIR to a directory with write permission"
{:no_tmp, _tmps} ->
raise Plug.UploadError,
"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 Plug.UploadError,
"could not find process Plug.Upload. Have you started the :plug application?"
end
@doc """
Starts the upload handling server.
"""
def start_link() do
GenServer.start_link(__MODULE__, :ok, name: __MODULE__)
end
## Callbacks
def init(:ok) do
Process.flag(:trap_exit, true)
tmp = Enum.find_value(@temp_env_vars, "/tmp", &System.get_env/1)
cwd = Path.join(File.cwd!(), "tmp")
:ets.new(@table, [:named_table, :public, :set])
{:ok, [tmp, cwd]}
end
def handle_call(:upload, {pid, _ref}, dirs) do
Process.monitor(pid)
{:reply, {:ok, dirs}, dirs}
end
def handle_info({:DOWN, _ref, :process, pid, _reason}, state) do
case :ets.lookup(@table, pid) do
[{pid, _tmp, paths}] ->
:ets.delete(@table, pid)
delete_paths(paths)
[] ->
:ok
end
{:noreply, state}
end
def handle_info(_msg, state) do
{:noreply, state}
end
def terminate(_reason, _state) do
folder = fn {_pid, _tmp, paths}, _ -> delete_paths(paths) end
:ets.foldl(folder, :ok, @table)
:ok
end
defp delete_paths(paths) do
for path <- paths, do: :file.delete(path)
:ok
end
end