Skip to content

Commit c5ef6e8

Browse files
authored
Merge pull request #302 from moonrailgun/moonrailgun/add-config-tip-before-download-in-guest
add config tip before download model in guest
2 parents 696917d + 86e4774 commit c5ef6e8

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

apps/node/src/modules/utils/download-url.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export async function downloadUrl(dispatch: TaskEventDispatcher, url: string, ta
3535
secureProtocol: 'TLSv1_2_method',
3636
};
3737
const response: Response = await fetch(
38-
url, {
38+
url, {
3939
headers,
4040
agent: systemProxy.http_proxy ? new HttpsProxyAgent(systemProxy.http_proxy as string, agentOptions) : undefined,
4141
});
@@ -52,7 +52,7 @@ export async function downloadUrl(dispatch: TaskEventDispatcher, url: string, ta
5252
});
5353

5454
const finished = util.promisify(stream.finished);
55-
55+
5656
const bufferStream = new stream.PassThrough();
5757
const writeStream = fs.createWriteStream(targetPath);
5858

@@ -111,6 +111,7 @@ export async function downloadUrlPro(dispatch: TaskEventDispatcher, url: string,
111111
const { systemProxy } = await getSystemProxy();
112112
const filename: string = path.basename(url);
113113
const tmpFilePath: string = targetPath + ".tmp";
114+
const isCivitai = url.includes("civitai");
114115

115116
try {
116117

@@ -130,7 +131,7 @@ export async function downloadUrlPro(dispatch: TaskEventDispatcher, url: string,
130131
const fileSize: number = fs.existsSync(tmpFilePath) ? fs.statSync(tmpFilePath).size : 0;
131132
let headers: Record<string, string> = fileSize > 0 ? { Range: `bytes=${fileSize}-` } : {};
132133

133-
if(url.indexOf("civitai") > -1) {
134+
if(isCivitai) {
134135
const token = getCivitAIToken(false);
135136
if (token) {
136137
headers["Authorization"] = token
@@ -147,10 +148,15 @@ export async function downloadUrlPro(dispatch: TaskEventDispatcher, url: string,
147148

148149
if (!response.ok) {
149150
throw new Error(`Failed to download from ${url}. Status: ${response.status} ${response.statusText}`);
150-
} else {
151-
console.log("Download Reponse from " + url);
152151
}
153152

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+
154160
const totalSize = Number(response.headers.get('content-length')) + fileSize;
155161
let downloadedSize = fileSize;
156162

@@ -234,4 +240,4 @@ export async function downloadUrlPro(dispatch: TaskEventDispatcher, url: string,
234240
} catch (error) {
235241
throw new Error(`Error downloading from ${url}: ${error}`);
236242
}
237-
}
243+
}

0 commit comments

Comments
 (0)