-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathpage_controller.ex
196 lines (158 loc) · 4.9 KB
/
page_controller.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
190
191
192
193
194
195
196
defmodule DiffWeb.PageController do
use DiffWeb, :controller
require Logger
@chunk_size 64 * 1024
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
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_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)
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
end