Skip to content

Commit c8b6bf1

Browse files
authored
Merge pull request #1 from oka4shi/feat/progress
進捗表示を追加
2 parents 538e109 + 15a9280 commit c8b6bf1

File tree

4 files changed

+53
-8
lines changed

4 files changed

+53
-8
lines changed

app/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
<body>
99
<h1>GitHub Artifact プレビュー</h1>
1010
<div>
11+
<progress id="progress" value="0"></progress>
1112
<p id="result" />
1213
</div>
1314
<script type="module" src="/src/main.ts"></script>

app/src/lib/fetch.ts

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,46 @@ export const fetchDownloadUrl = async (
2727

2828
export const downloadFile = async (
2929
url: string,
30-
): Promise<Result<ArrayBuffer>> => {
30+
onProgress: (progress: number) => void = () => {},
31+
): Promise<Result<Response>> => {
3132
try {
3233
const resp = await fetch(url);
3334

3435
if (!resp.ok) {
3536
return {
3637
ok: false,
37-
message: "zipファイルの取得に失敗しました",
38+
message: `zipファイルの取得に失敗しました: ${resp.statusText}`,
3839
error: new Error(`HTTP error!: ${resp.statusText}`),
3940
};
4041
}
4142

42-
return { ok: true, value: await resp.arrayBuffer() };
43+
const size = Number(resp.headers.get("Content-Length") ?? "-1");
44+
45+
return {
46+
ok: true,
47+
value: new Response(
48+
new ReadableStream({
49+
async start(controller) {
50+
const reader = resp.body?.getReader();
51+
let chunk = 0;
52+
53+
async function read() {
54+
const result = await reader?.read();
55+
56+
if (result && !result.done) {
57+
chunk += result.value.length;
58+
onProgress(chunk / size);
59+
60+
controller.enqueue(result.value);
61+
return await read();
62+
}
63+
}
64+
await read();
65+
controller.close();
66+
},
67+
}),
68+
),
69+
};
4370
} catch (error) {
4471
return {
4572
ok: false,

app/src/main.ts

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import "./style.css";
12
import { downloadFile, fetchDownloadUrl } from "./lib/fetch";
23

34
const main = async () => {
@@ -10,13 +11,29 @@ const main = async () => {
1011
return result.message;
1112
}
1213

14+
const progressBar = document.getElementById("progress");
15+
const status = document.getElementById("result");
16+
1317
const downloadUrl = result.value;
14-
const result1 = await downloadFile(downloadUrl);
18+
const result1 = await downloadFile(downloadUrl, (progress) => {
19+
if (progress < 0) {
20+
return;
21+
}
22+
23+
if (progressBar) {
24+
progressBar.setAttribute("value", String(progress));
25+
progressBar.innerText = `${Math.floor(progress * 100)}%`;
26+
}
27+
if (status) {
28+
status.innerText = `ファイルをダウンロードしています……: ${Math.floor(progress * 100)}%`;
29+
}
30+
});
31+
1532
if (!result1.ok) {
1633
return result1.message;
1734
}
1835

19-
const buf = result1.value;
36+
const buf = await result1.value.arrayBuffer();
2037
const view = new DataView(buf);
2138

2239
// end of central directory record(EOCD)を探す
@@ -100,7 +117,6 @@ const main = async () => {
100117
// blobに変換して遷移
101118
const blob = new Blob([fileData], { type: "application/pdf" });
102119
const blobUrl = window.URL.createObjectURL(blob);
103-
history.pushState(null, "", `?dist=${encodeURIComponent(url)}`);
104120
window.location.href = blobUrl;
105121
};
106122

@@ -111,6 +127,7 @@ const main = async () => {
111127
}
112128

113129
const error = await main();
130+
114131
if (error) {
115132
if (elm) {
116133
elm.innerText = error;

workers/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
"private": true,
55
"scripts": {
66
"deploy": "wrangler deploy",
7-
"dev": "wrangler dev",
8-
"start": "wrangler dev",
7+
"dev": "wrangler dev --env local",
8+
"start": "wrangler dev --env local",
99
"test": "vitest",
1010
"cf-typegen": "wrangler types"
1111
},

0 commit comments

Comments
 (0)