|
1 | 1 | import { useCallback, useMemo, useRef, useState } from 'react'; |
2 | 2 |
|
3 | | -export const resolver = ({ |
4 | | - setSize, |
5 | | - setControllerCallback, |
6 | | - setPercentageCallback, |
7 | | - setErrorCallback, |
8 | | -}) => (response) => { |
9 | | - if (!response.ok) { |
10 | | - throw Error(`${response.status} ${response.type} ${response.statusText}`); |
11 | | - } |
| 3 | +export const resolver = |
| 4 | + ({ |
| 5 | + setSize, |
| 6 | + setControllerCallback, |
| 7 | + setPercentageCallback, |
| 8 | + setErrorCallback, |
| 9 | + }) => |
| 10 | + (response) => { |
| 11 | + if (!response.ok) { |
| 12 | + throw Error(`${response.status} ${response.type} ${response.statusText}`); |
| 13 | + } |
12 | 14 |
|
13 | | - if (!response.body) { |
14 | | - throw Error('ReadableStream not yet supported in this browser.'); |
15 | | - } |
| 15 | + if (!response.body) { |
| 16 | + throw Error('ReadableStream not yet supported in this browser.'); |
| 17 | + } |
16 | 18 |
|
17 | | - const contentEncoding = response.headers.get('content-encoding'); |
18 | | - const contentLength = response.headers.get( |
19 | | - contentEncoding ? 'x-file-size' : 'content-length' |
20 | | - ); |
| 19 | + const contentEncoding = response.headers.get('content-encoding'); |
| 20 | + const contentLength = response.headers.get( |
| 21 | + contentEncoding ? 'x-file-size' : 'content-length' |
| 22 | + ); |
21 | 23 |
|
22 | | - const total = parseInt(contentLength || 0, 10); |
| 24 | + const total = parseInt(contentLength || 0, 10); |
23 | 25 |
|
24 | | - setSize(() => total); |
| 26 | + setSize(() => total); |
25 | 27 |
|
26 | | - let loaded = 0; |
| 28 | + let loaded = 0; |
27 | 29 |
|
28 | | - const stream = new ReadableStream({ |
29 | | - start(controller) { |
30 | | - setControllerCallback(controller); |
| 30 | + const stream = new ReadableStream({ |
| 31 | + start(controller) { |
| 32 | + setControllerCallback(controller); |
31 | 33 |
|
32 | | - const reader = response.body.getReader(); |
| 34 | + const reader = response.body.getReader(); |
33 | 35 |
|
34 | | - function read() { |
35 | | - return reader |
36 | | - .read() |
37 | | - .then(({ done, value }) => { |
38 | | - if (done) { |
39 | | - return controller.close(); |
40 | | - } |
| 36 | + function read() { |
| 37 | + return reader |
| 38 | + .read() |
| 39 | + .then(({ done, value }) => { |
| 40 | + if (done) { |
| 41 | + return controller.close(); |
| 42 | + } |
41 | 43 |
|
42 | | - loaded += value.byteLength; |
| 44 | + loaded += value.byteLength; |
43 | 45 |
|
44 | | - controller.enqueue(value); |
| 46 | + controller.enqueue(value); |
45 | 47 |
|
46 | | - setPercentageCallback({ loaded, total }); |
| 48 | + setPercentageCallback({ loaded, total }); |
47 | 49 |
|
48 | | - return read(); |
49 | | - }) |
50 | | - .catch((error) => { |
51 | | - setErrorCallback(error); |
52 | | - reader.cancel('Cancelled'); |
53 | | - return controller.error(error); |
54 | | - }); |
55 | | - } |
| 50 | + return read(); |
| 51 | + }) |
| 52 | + .catch((error) => { |
| 53 | + setErrorCallback(error); |
| 54 | + reader.cancel('Cancelled'); |
| 55 | + return controller.error(error); |
| 56 | + }); |
| 57 | + } |
56 | 58 |
|
57 | | - return read(); |
58 | | - }, |
59 | | - }); |
| 59 | + return read(); |
| 60 | + }, |
| 61 | + }); |
60 | 62 |
|
61 | | - return new Response(stream); |
62 | | -}; |
| 63 | + return new Response(stream); |
| 64 | + }; |
63 | 65 |
|
64 | | -export function jsDownload(data, filename, mime, bom) { |
| 66 | +export const jsDownload = (data, filename, mime, bom) => { |
65 | 67 | const blobData = typeof bom !== 'undefined' ? [bom, data] : [data]; |
66 | 68 | const blob = new Blob(blobData, { |
67 | 69 | type: mime || 'application/octet-stream', |
68 | 70 | }); |
69 | | - if (typeof window.navigator.msSaveBlob !== 'undefined') { |
70 | | - window.navigator.msSaveBlob(blob, filename); |
71 | | - } else { |
72 | | - const blobURL = |
73 | | - window.URL && window.URL.createObjectURL |
74 | | - ? window.URL.createObjectURL(blob) |
75 | | - : window.webkitURL.createObjectURL(blob); |
76 | | - const tempLink = document.createElement('a'); |
77 | | - tempLink.style.display = 'none'; |
78 | | - tempLink.href = blobURL; |
79 | | - tempLink.setAttribute('download', filename); |
80 | | - |
81 | | - if (typeof tempLink.download === 'undefined') { |
82 | | - tempLink.setAttribute('target', '_blank'); |
83 | | - } |
84 | 71 |
|
85 | | - document.body.appendChild(tempLink); |
86 | | - tempLink.click(); |
| 72 | + if (typeof window.navigator.msSaveBlob !== 'undefined') { |
| 73 | + return window.navigator.msSaveBlob(blob, filename); |
| 74 | + } |
87 | 75 |
|
88 | | - setTimeout(() => { |
89 | | - document.body.removeChild(tempLink); |
90 | | - window.URL.revokeObjectURL(blobURL); |
91 | | - }, 200); |
| 76 | + const blobURL = |
| 77 | + window.URL && window.URL.createObjectURL |
| 78 | + ? window.URL.createObjectURL(blob) |
| 79 | + : window.webkitURL.createObjectURL(blob); |
| 80 | + const tempLink = document.createElement('a'); |
| 81 | + tempLink.style.display = 'none'; |
| 82 | + tempLink.href = blobURL; |
| 83 | + tempLink.setAttribute('download', filename); |
| 84 | + |
| 85 | + if (typeof tempLink.download === 'undefined') { |
| 86 | + tempLink.setAttribute('target', '_blank'); |
92 | 87 | } |
93 | | -} |
| 88 | + |
| 89 | + document.body.appendChild(tempLink); |
| 90 | + tempLink.click(); |
| 91 | + |
| 92 | + return setTimeout(() => { |
| 93 | + document.body.removeChild(tempLink); |
| 94 | + window.URL.revokeObjectURL(blobURL); |
| 95 | + }, 200); |
| 96 | +}; |
94 | 97 |
|
95 | 98 | export default function useDownloader() { |
96 | 99 | const debugMode = process.env.REACT_APP_DEBUG_MODE; |
@@ -143,55 +146,53 @@ export default function useDownloader() { |
143 | 146 | }, [setControllerCallback]); |
144 | 147 |
|
145 | 148 | const handleDownload = useCallback( |
146 | | - (downloadUrl, filename) => { |
147 | | - async function startDownload() { |
148 | | - clearAllStateCallback(); |
149 | | - setError(() => null); |
150 | | - setIsInProgress(() => true); |
151 | | - |
152 | | - const interval = setInterval( |
153 | | - () => setElapsed((prevValue) => prevValue + 1), |
154 | | - debugMode ? 1 : 1000 |
155 | | - ); |
156 | | - const resolverWithProgress = resolver({ |
157 | | - setSize, |
158 | | - setControllerCallback, |
159 | | - setPercentageCallback, |
160 | | - setErrorCallback, |
161 | | - }); |
162 | | - |
163 | | - return fetch(downloadUrl, { |
164 | | - method: 'GET', |
| 149 | + async (downloadUrl, filename) => { |
| 150 | + if (isInProgress) return null; |
| 151 | + |
| 152 | + clearAllStateCallback(); |
| 153 | + setError(() => null); |
| 154 | + setIsInProgress(() => true); |
| 155 | + |
| 156 | + const interval = setInterval( |
| 157 | + () => setElapsed((prevValue) => prevValue + 1), |
| 158 | + debugMode ? 1 : 1000 |
| 159 | + ); |
| 160 | + const resolverWithProgress = resolver({ |
| 161 | + setSize, |
| 162 | + setControllerCallback, |
| 163 | + setPercentageCallback, |
| 164 | + setErrorCallback, |
| 165 | + }); |
| 166 | + |
| 167 | + return fetch(downloadUrl, { |
| 168 | + method: 'GET', |
| 169 | + }) |
| 170 | + .then(resolverWithProgress) |
| 171 | + .then((data) => { |
| 172 | + return data.blob(); |
165 | 173 | }) |
166 | | - .then(resolverWithProgress) |
167 | | - .then((data) => data.blob()) |
168 | | - .then((response) => jsDownload(response, filename)) |
169 | | - .then(() => { |
170 | | - clearAllStateCallback(); |
171 | | - |
172 | | - return clearInterval(interval); |
173 | | - }) |
174 | | - .catch((err) => { |
175 | | - clearAllStateCallback(); |
176 | | - setError((prevValue) => { |
177 | | - const { message } = err; |
178 | | - |
179 | | - if (message !== 'Failed to fetch') { |
180 | | - return { |
181 | | - errorMessage: err.message, |
182 | | - }; |
183 | | - } |
| 174 | + .then((response) => jsDownload(response, filename)) |
| 175 | + .then(() => { |
| 176 | + clearAllStateCallback(); |
184 | 177 |
|
185 | | - return prevValue; |
186 | | - }); |
| 178 | + return clearInterval(interval); |
| 179 | + }) |
| 180 | + .catch((err) => { |
| 181 | + clearAllStateCallback(); |
| 182 | + setError((prevValue) => { |
| 183 | + const { message } = err; |
| 184 | + |
| 185 | + if (message !== 'Failed to fetch') { |
| 186 | + return { |
| 187 | + errorMessage: err.message, |
| 188 | + }; |
| 189 | + } |
187 | 190 |
|
188 | | - return clearInterval(interval); |
| 191 | + return prevValue; |
189 | 192 | }); |
190 | | - } |
191 | 193 |
|
192 | | - if (!isInProgress) { |
193 | | - startDownload(); |
194 | | - } |
| 194 | + return clearInterval(interval); |
| 195 | + }); |
195 | 196 | }, |
196 | 197 | [ |
197 | 198 | isInProgress, |
|
0 commit comments