-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathpage_controller.ex
More file actions
269 lines (218 loc) · 6.65 KB
/
page_controller.ex
File metadata and controls
269 lines (218 loc) · 6.65 KB
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
defmodule DiffWeb.PageController do
use DiffWeb, :controller
require Logger
@chunk_size 64 * 1024
def diffs(conn, %{"diffs" => diffs}) do
diffs =
diffs
|> Enum.map(&parse_diff/1)
|> Enum.reject(&is_nil/1)
conn
|> assign(:diffs, diffs)
|> render()
end
def diffs(conn, _params), do: render_error(conn, 400)
def diff(conn, %{"package" => package, "versions" => versions}) do
case parse_versions(versions) do
{:ok, from, to} ->
maybe_cached_diff(conn, package, from, to)
:error ->
render_error(conn, 400)
end
end
def expand_context(conn, %{"file_name" => _file_name} = params) do
case parse_version(params["version"]) do
{:ok, version} ->
version = to_string(version)
params = Map.update!(params, "version", fn _ -> version end)
do_expand_context(conn, params)
:error ->
conn
|> put_status(400)
|> json(%{error: "Bad Request"})
end
end
def expand_context(conn, _params) do
conn
|> put_status(400)
|> json(%{error: "missing query parameter: file_name"})
end
defp maybe_cached_diff(conn, _package, version, version) do
render_error(conn, 400)
end
defp maybe_cached_diff(conn, _package, :latest, _version) do
render_error(conn, 400)
end
defp maybe_cached_diff(conn, package, from, :latest) do
case Diff.Package.Store.get_versions(package) do
{:ok, versions} ->
to =
versions
|> Enum.map(&Version.parse!/1)
|> Enum.filter(&(&1.pre == []))
|> Enum.max(Version)
maybe_cached_diff(conn, package, from, to)
{:error, :not_found} ->
render_error(conn, 404)
end
end
defp maybe_cached_diff(conn, package, from, to) do
from = to_string(from)
to = to_string(to)
case Diff.Storage.get(package, from, to) do
{:ok, stream} ->
Logger.debug("cache hit for #{package}/#{from}..#{to}")
conn
|> put_resp_content_type("text/html")
|> stream_diff(stream)
{:error, :not_found} ->
Logger.debug("cache miss for #{package}/#{from}..#{to}")
do_diff(conn, package, from, to)
end
end
defp do_expand_context(conn, params) do
chunk_extractor_params =
for {key, val} <- params, into: %{} do
{String.to_existing_atom(key), val}
end
case Diff.Hex.get_chunk(chunk_extractor_params) do
{:ok, chunk} ->
rendered_chunk =
Phoenix.View.render_to_string(
DiffWeb.RenderView,
"render_context_chunk.html",
chunk: chunk
)
conn
|> put_status(200)
|> json(%{chunk: rendered_chunk, lines: length(chunk)})
{:error, %{errors: errors}} ->
conn
|> put_status(400)
|> json(%{errors: Enum.into(errors, %{})})
end
end
defp do_diff(conn, package, from, to) do
case Diff.Hex.diff(package, from, to) do
{:ok, stream} ->
path = render_diff(package, from, to, stream)
stream = File.stream!(path, [:read_ahead], @chunk_size)
cache_diff(package, from, to, stream)
delete_diff(path)
conn
|> put_resp_content_type("text/html")
|> stream_diff(stream)
:error ->
render(conn, "500.html")
end
catch
:throw, {:diff, :invalid_diff} ->
render(conn, "500.html")
end
defp stream_diff(conn, stream) do
header = [
Phoenix.View.render_to_iodata(DiffWeb.LayoutView, "header.html", conn: conn),
Phoenix.View.render_to_iodata(DiffWeb.PageView, "diff_header.html", [])
]
footer = [
Phoenix.View.render_to_iodata(DiffWeb.PageView, "diff_footer.html", []),
Phoenix.View.render_to_iodata(DiffWeb.LayoutView, "footer.html", conn: conn)
]
conn = send_chunked(conn, 200)
with {:ok, conn} <- chunk(conn, header),
{:ok, conn} <- stream_chunks(conn, stream),
{:ok, conn} <- chunk(conn, footer) do
conn
else
{:error, reason} ->
Logger.error("chunking failed: #{inspect(reason)}")
conn
end
end
defp stream_chunks(conn, stream) do
Enum.reduce_while(stream, {:ok, conn}, fn chunk, {:ok, conn} ->
case chunk(conn, chunk) do
{:ok, conn} ->
{:cont, {:ok, conn}}
{:error, reason} ->
{:halt, {:error, reason}}
end
end)
end
defp render_diff(package, from, to, stream) do
path = tmp_path("html-#{package}-#{from}-#{to}-")
File.open!(path, [:write, :raw, :binary, :write_delay], fn file ->
Enum.each(stream, fn
{:ok, patch} ->
html_patch =
Phoenix.View.render_to_iodata(DiffWeb.RenderView, "render.html",
patch: patch,
package: package,
from_version: from
)
IO.binwrite(file, html_patch)
{:error, error} ->
Logger.error("Failed to parse diff #{package} #{from}..#{to} with: #{inspect(error)}")
throw({:diff, :invalid_diff})
end)
end)
path
end
defp cache_diff(package, from, to, stream) do
Task.Supervisor.start_child(Diff.Tasks, fn ->
Diff.Storage.put(package, from, to, stream)
end)
end
# A bit of a hack to make sure both the GCS upload and chunked response
# has opened file before we delete it
defp delete_diff(path) do
Task.Supervisor.start_child(Diff.Tasks, fn ->
Process.sleep(10_000)
File.rm(path)
end)
end
defp parse_versions(input) do
with {:ok, [from, to]} <- versions_from_input(input),
{:ok, from} <- parse_version(from),
{:ok, to} <- parse_version(to) do
{:ok, from, to}
else
_ ->
:error
end
end
defp versions_from_input(input) when is_binary(input) do
input
|> String.split("..")
|> case do
[from] ->
[from, ""]
[from, to] ->
[from, to]
end
|> versions_from_input()
end
defp versions_from_input([_from, _to] = versions) do
versions = Enum.map(versions, &String.trim/1)
{:ok, versions}
end
defp versions_from_input(_), do: :error
defp parse_version(""), do: {:ok, :latest}
defp parse_version(input), do: Version.parse(input)
defp render_error(conn, status) do
conn
|> put_status(status)
|> render("#{status}.html")
end
defp tmp_path(prefix) do
random_string = Base.encode16(:crypto.strong_rand_bytes(4))
Path.join([System.tmp_dir!(), "diff", prefix <> random_string])
end
defp parse_diff(diff) do
case String.split(diff, ":", trim: true) do
[app, from, to] -> {app, from, to, build_url(app, from, to)}
_ -> nil
end
end
defp build_url(app, from, to), do: "/diff/#{app}/#{from}..#{to}"
end