Skip to content

Commit 4fc1d1a

Browse files
committed
Fix progress bar
1 parent f8a997b commit 4fc1d1a

File tree

7 files changed

+85
-17
lines changed

7 files changed

+85
-17
lines changed

lib/hex/api/release.ex

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,14 @@ defmodule Hex.API.Release do
1111

1212
def publish(repo, tar, auth, progress \\ fn _ -> nil end, replace \\ false)
1313

14-
def publish(repo, tar, auth, _progress, replace?) do
14+
def publish(repo, tar, auth, progress, replace?) do
1515
config = build_config(repo, auth)
16-
params = [{:replace, replace?}]
1716

18-
# TODO: Progress callback needs to be handled differently with hex_core
17+
# Pass progress callback through adapter config
18+
adapter_config = %{progress_callback: progress}
19+
config = Map.put(config, :http_adapter, {Hex.HTTP, adapter_config})
20+
21+
params = [{:replace, replace?}]
1922
:mix_hex_api_release.publish(config, tar, params)
2023
end
2124

lib/hex/api/release_docs.ex

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,51 @@ defmodule Hex.API.ReleaseDocs do
55

66
def get(repo, name, version) do
77
config = build_config(repo, [])
8-
path = :mix_hex_api.build_repository_path(config, ["packages", to_string(name), "releases", to_string(version), "docs"])
8+
9+
path =
10+
:mix_hex_api.build_repository_path(config, [
11+
"packages",
12+
to_string(name),
13+
"releases",
14+
to_string(version),
15+
"docs"
16+
])
917

1018
:mix_hex_api.get(config, path)
1119
end
1220

13-
def publish(repo, name, version, tar, auth, _progress \\ fn _ -> nil end) do
21+
def publish(repo, name, version, tar, auth, progress \\ fn _ -> nil end) do
1422
config = build_config(repo, auth)
15-
path = :mix_hex_api.build_repository_path(config, ["packages", to_string(name), "releases", to_string(version), "docs"])
23+
24+
# Pass progress callback through adapter config
25+
adapter_config = %{progress_callback: progress}
26+
config = Map.put(config, :http_adapter, {Hex.HTTP, adapter_config})
27+
28+
path =
29+
:mix_hex_api.build_repository_path(config, [
30+
"packages",
31+
to_string(name),
32+
"releases",
33+
to_string(version),
34+
"docs"
35+
])
1636

1737
body = {"application/octet-stream", tar}
1838

19-
# TODO: Progress callback needs to be handled differently
2039
:mix_hex_api.post(config, path, body)
2140
end
2241

2342
def delete(repo, name, version, auth) do
2443
config = build_config(repo, auth)
25-
path = :mix_hex_api.build_repository_path(config, ["packages", to_string(name), "releases", to_string(version), "docs"])
44+
45+
path =
46+
:mix_hex_api.build_repository_path(config, [
47+
"packages",
48+
to_string(name),
49+
"releases",
50+
to_string(version),
51+
"docs"
52+
])
2653

2754
:mix_hex_api.delete(config, path)
2855
end
@@ -32,5 +59,4 @@ defmodule Hex.API.ReleaseDocs do
3259
opts = if auth, do: Keyword.merge(opts, auth), else: opts
3360
Client.config(opts)
3461
end
35-
3662
end

lib/hex/api/short_url.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ defmodule Hex.API.ShortURL do
99
case :mix_hex_api_short_url.create(config, to_string(url)) do
1010
{:ok, {201, _headers, %{"url" => short_url}}} ->
1111
{:ok, short_url}
12+
1213
_error ->
1314
:error
1415
end

lib/hex/http.ex

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ defmodule Hex.HTTP do
4747
Hex.State.fetch!(:http_timeout, fn val -> if is_integer(val), do: val * 1000 end) ||
4848
@request_timeout
4949

50+
# Handle progress callback for uploads
51+
progress_callback = Map.get(adapter_config, :progress_callback)
52+
{body, extra_headers} = wrap_body_with_progress(body, progress_callback)
53+
headers = Map.merge(headers, extra_headers)
54+
5055
http_opts = build_http_opts(url, timeout)
5156
opts = [body_format: :binary]
5257
request = build_request(url, headers, body)
@@ -389,4 +394,37 @@ defmodule Hex.HTTP do
389394
headers
390395
end
391396
end
397+
398+
@chunk_size 10_000
399+
400+
defp wrap_body_with_progress(body, progress_callback) do
401+
case body do
402+
{content_type, binary_body}
403+
when is_binary(binary_body) and is_function(progress_callback, 1) ->
404+
# Create streaming function for httpc chunked upload - match old API pattern exactly
405+
total_size = byte_size(binary_body)
406+
# Initialize progress bar
407+
progress_callback.(0)
408+
409+
body_fn = fn
410+
size when size < total_size ->
411+
new_size = min(size + @chunk_size, total_size)
412+
chunk = new_size - size
413+
progress_callback.(new_size)
414+
{:ok, :binary.part(binary_body, size, chunk), new_size}
415+
416+
_size ->
417+
:eof
418+
end
419+
420+
# Return chunked body AND content-length header (required for chunked uploads)
421+
body_result = {content_type, {body_fn, 0}}
422+
headers = %{"content-length" => Integer.to_string(total_size)}
423+
{body_result, headers}
424+
425+
_other ->
426+
# No progress callback or not a binary body, use as-is
427+
{body, %{}}
428+
end
429+
end
392430
end

lib/hex/registry/server.ex

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,6 @@ defmodule Hex.Registry.Server do
360360

361361
defp write_result({:ok, {code, headers, %{releases: releases}}}, repo, package, %{ets: tid})
362362
when code in 200..299 do
363-
364363
delete_package(repo, package, tid)
365364
now = :calendar.universal_time()
366365

lib/hex/repo.ex

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,6 @@ defmodule Hex.Repo do
225225
:mix_hex_repo.get_hex_installs(config)
226226
end
227227

228-
229228
def find_new_version_from_csv(body) do
230229
body
231230
|> parse_csv()
@@ -238,7 +237,6 @@ defmodule Hex.Repo do
238237
config.url <> "/tarballs/#{URI.encode(package)}-#{URI.encode(version)}.tar"
239238
end
240239

241-
242240
defp parse_csv(body) do
243241
body
244242
|> :binary.split("\n", [:global, :trim])
@@ -351,11 +349,12 @@ defmodule Hex.Repo do
351349
http_user_agent_fragment: user_agent_fragment()
352350
}
353351

354-
config = if repo.auth_key && Map.get(repo, :trusted, true) do
355-
Map.put(config, :repo_key, repo.auth_key)
356-
else
357-
config
358-
end
352+
config =
353+
if repo.auth_key && Map.get(repo, :trusted, true) do
354+
Map.put(config, :repo_key, repo.auth_key)
355+
else
356+
config
357+
end
359358

360359
if etag do
361360
Map.put(config, :http_etag, etag)

lib/mix/tasks/hex.user.ex

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,8 +287,10 @@ defmodule Mix.Tasks.Hex.User do
287287
item when is_map(item) -> Map.get(item, "authing_key") == true
288288
_ -> false
289289
end)
290+
290291
is_map(body) ->
291292
Map.get(body, "authing_key") == true
293+
292294
true ->
293295
false
294296
end

0 commit comments

Comments
 (0)