(dispatch: TaskEventDispatcher, url: string, targetPath: string, sha?: string)
| 108 | } |
| 109 | |
| 110 | export async function downloadUrlPro(dispatch: TaskEventDispatcher, url: string, targetPath: string, sha?: string): Promise<void> { |
| 111 | const { systemProxy } = await getSystemProxy(); |
| 112 | const filename: string = path.basename(url); |
| 113 | const tmpFilePath: string = targetPath + ".tmp"; |
| 114 | const isCivitai = url.includes("civitai"); |
| 115 | |
| 116 | try { |
| 117 | |
| 118 | if (fs.existsSync(targetPath)) { |
| 119 | const msg = `File ${filename} already exists. Skipping download.`; |
| 120 | if (sha) { |
| 121 | if (await verifySHA(targetPath, sha)) { |
| 122 | logger.info(msg); |
| 123 | return; |
| 124 | } |
| 125 | } else { |
| 126 | logger.info(msg); |
| 127 | return; |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | const fileSize: number = fs.existsSync(tmpFilePath) ? fs.statSync(tmpFilePath).size : 0; |
| 132 | let headers: Record<string, string> = fileSize > 0 ? { Range: `bytes=${fileSize}-` } : {}; |
| 133 | |
| 134 | if(isCivitai) { |
| 135 | const token = getCivitAIToken(false); |
| 136 | if (token) { |
| 137 | headers["Authorization"] = token |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | const response: Response = await fetch(url, { |
| 142 | headers: { |
| 143 | ...headers, |
| 144 | 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3', |
| 145 | }, |
| 146 | agent: systemProxy.http_proxy ? new HttpsProxyAgent(systemProxy.http_proxy as string) : undefined, |
| 147 | }); |
| 148 | |
| 149 | if (!response.ok) { |
| 150 | throw new Error(`Failed to download from ${url}. Status: ${response.status} ${response.statusText}`); |
| 151 | } |
| 152 | |
| 153 | if(isCivitai && response.redirected && response.url.startsWith('https://civitai.com/login')) { |
| 154 | // is download from civitai but this model need login to download |
| 155 | throw new Error(`Failed to download this model without config civitai token. You can config your token in settings before continue`); |
| 156 | } |
| 157 | |
| 158 | console.log("Download Reponse from " + url); |
| 159 | |
| 160 | const totalSize = Number(response.headers.get('content-length')) + fileSize; |
| 161 | let downloadedSize = fileSize; |
| 162 | |
| 163 | const progressBar = new progress('[:bar] :percent :etas', { |
| 164 | complete: '=', |
| 165 | incomplete: ' ', |
| 166 | width: 40, |
| 167 | total: totalSize, |
no test coverage detected