|
1 | | -import makeDir from 'make-dir'; |
2 | | -import fss from 'fs'; |
3 | | -import axios from 'axios'; |
4 | | -import pMap from 'p-map'; |
5 | | -import path from 'path'; |
6 | | -import adapter from 'axios/lib/adapters/http'; |
7 | | -import computeFileHash from './computeFileHash'; |
8 | | - |
9 | | -const fs = fss.promises; |
10 | | - |
11 | | -function getUri(url) { |
12 | | - return new URL(url).href; |
13 | | -} |
| 1 | +import { ipcRenderer } from 'electron'; |
14 | 2 |
|
15 | 3 | export const downloadInstanceFiles = async ( |
16 | 4 | arr, |
17 | 5 | updatePercentage, |
18 | 6 | threads = 4 |
19 | 7 | ) => { |
20 | | - let downloaded = 0; |
21 | | - await pMap( |
| 8 | + ipcRenderer.on('download-instance-files-progress', (e, p) => |
| 9 | + updatePercentage(p) |
| 10 | + ); |
| 11 | + const res = await ipcRenderer.invoke( |
| 12 | + 'download-instance-files', |
22 | 13 | arr, |
23 | | - async item => { |
24 | | - let counter = 0; |
25 | | - let res = false; |
26 | | - if (!item.path || !item.url) { |
27 | | - console.warn('Skipping', item); |
28 | | - return; |
29 | | - } |
30 | | - do { |
31 | | - counter += 1; |
32 | | - if (counter !== 1) { |
33 | | - await new Promise(resolve => setTimeout(resolve, 5000)); |
34 | | - } |
35 | | - |
36 | | - try { |
37 | | - res = await downloadFileInstance( |
38 | | - item.path, |
39 | | - item.url, |
40 | | - item.sha1, |
41 | | - item.legacyPath |
42 | | - ); |
43 | | - } catch { |
44 | | - // Do nothing |
45 | | - } |
46 | | - } while (!res && counter < 3); |
47 | | - downloaded += 1; |
48 | | - if ( |
49 | | - updatePercentage && |
50 | | - (downloaded % 5 === 0 || downloaded === arr.length) |
51 | | - ) { |
52 | | - updatePercentage(downloaded); |
53 | | - } |
54 | | - }, |
55 | | - { concurrency: threads } |
| 14 | + updatePercentage !== undefined, |
| 15 | + threads |
56 | 16 | ); |
57 | | -}; |
58 | | - |
59 | | -const downloadFileInstance = async (fileName, url, sha1, legacyPath) => { |
60 | | - let encodedUrl; |
61 | | - try { |
62 | | - const filePath = path.dirname(fileName); |
63 | | - try { |
64 | | - await fs.access(fileName); |
65 | | - if (legacyPath) await fs.access(legacyPath); |
66 | | - const checksum = await computeFileHash(fileName); |
67 | | - const legacyChecksum = legacyPath && (await computeFileHash(legacyPath)); |
68 | | - if (checksum === sha1 && (!legacyPath || legacyChecksum === sha1)) { |
69 | | - return true; |
70 | | - } |
71 | | - } catch { |
72 | | - await makeDir(filePath); |
73 | | - if (legacyPath) await makeDir(path.dirname(legacyPath)); |
74 | | - } |
75 | | - |
76 | | - encodedUrl = getUri(url); |
77 | | - |
78 | | - const { data } = await axios.get(encodedUrl, { |
79 | | - responseType: 'stream', |
80 | | - responseEncoding: null, |
81 | | - adapter, |
82 | | - timeout: 60000 * 20 |
83 | | - }); |
84 | | - |
85 | | - const wStream = fss.createWriteStream(fileName, { |
86 | | - encoding: null |
87 | | - }); |
88 | | - |
89 | | - data.pipe(wStream); |
90 | | - let wStreamLegacy; |
91 | | - if (legacyPath) { |
92 | | - wStreamLegacy = fss.createWriteStream(legacyPath, { |
93 | | - encoding: null |
94 | | - }); |
95 | | - data.pipe(wStreamLegacy); |
96 | | - } |
97 | | - |
98 | | - await new Promise((resolve, reject) => { |
99 | | - data.on('error', err => { |
100 | | - console.error(err); |
101 | | - reject(err); |
102 | | - }); |
103 | | - |
104 | | - data.on('end', () => { |
105 | | - wStream.end(); |
106 | | - wStream.close(); |
107 | | - if (legacyPath) { |
108 | | - wStreamLegacy.end(); |
109 | | - wStreamLegacy.close(); |
110 | | - } |
111 | | - resolve(); |
112 | | - }); |
113 | | - }); |
114 | | - return true; |
115 | | - } catch (e) { |
116 | | - console.error( |
117 | | - `Error while downloading <${url} | ${encodedUrl}> to <${fileName}> --> ${e.message}` |
118 | | - ); |
119 | | - return false; |
120 | | - } |
| 17 | + console.log('Back:', res); |
| 18 | + ipcRenderer.removeAllListeners('download-instance-files-progress'); |
| 19 | + return res; |
121 | 20 | }; |
122 | 21 |
|
123 | 22 | export const downloadFile = async (fileName, url, onProgress) => { |
124 | | - await makeDir(path.dirname(fileName)); |
125 | | - |
126 | | - const encodedUrl = getUri(url); |
127 | | - |
128 | | - const { data, headers } = await axios.get(encodedUrl, { |
129 | | - responseType: 'stream', |
130 | | - responseEncoding: null, |
131 | | - adapter, |
132 | | - timeout: 60000 * 20 |
133 | | - }); |
134 | | - |
135 | | - const out = fss.createWriteStream(fileName, { encoding: null }); |
136 | | - data.pipe(out); |
137 | | - |
138 | | - // Save variable to know progress |
139 | | - let receivedBytes = 0; |
140 | | - const totalBytes = parseInt(headers['content-length'], 10); |
141 | | - |
142 | | - data.on('data', chunk => { |
143 | | - // Update the received bytes |
144 | | - receivedBytes += chunk.length; |
145 | | - if (onProgress) { |
146 | | - onProgress(parseInt(((receivedBytes * 100) / totalBytes).toFixed(1), 10)); |
147 | | - } |
148 | | - }); |
149 | | - |
150 | | - return new Promise((resolve, reject) => { |
151 | | - data.on('end', () => { |
152 | | - out.end(); |
153 | | - out.close(); |
154 | | - resolve(); |
155 | | - }); |
156 | | - |
157 | | - data.on('error', () => { |
158 | | - reject(); |
159 | | - }); |
160 | | - }); |
| 23 | + ipcRenderer.on('download-file-progress', (e, p) => onProgress(p)); |
| 24 | + const res = await ipcRenderer.invoke( |
| 25 | + 'download-file', |
| 26 | + fileName, |
| 27 | + url, |
| 28 | + onProgress !== undefined |
| 29 | + ); |
| 30 | + console.log('Back-single:', res); |
| 31 | + ipcRenderer.removeAllListeners('download-file-progress'); |
| 32 | + return res; |
161 | 33 | }; |
0 commit comments