Skip to content

Commit 2392962

Browse files
committed
fix: 修正启动下载rag的流程
1 parent 2cb22df commit 2392962

File tree

1 file changed

+37
-34
lines changed

1 file changed

+37
-34
lines changed

src/ragService.ts

Lines changed: 37 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ import * as path from 'path';
44
import * as fs from 'fs/promises';
55
import * as net from 'net';
66
import * as os from 'os';
7-
import * as https from 'https';
7+
import axios from 'axios';
88
import * as crypto from 'crypto';
99

1010
// Define a variable to store the extension path
1111
let EXTENSION_PATH: string = '';
1212

13-
const isDevMode = __dirname.includes('dist');
13+
const isDevMode = !__dirname.includes('dist');
1414
// Dynamically set Python script or EXE path
1515
const getPythonScriptPath = (extensionPath: string) => {
1616
if (isDevMode) {
@@ -67,13 +67,14 @@ class RagService {
6767
await fs.mkdir(distPath, { recursive: true });
6868

6969
let shouldDownload = false;
70+
let remoteMd5 = '';
7071

7172
// Check if rag.exe exists
7273
try {
7374
await fs.access(exePath, fs.constants.F_OK);
7475

7576
// Download MD5 from GitHub
76-
const remoteMd5 = await this.downloadText('https://github.com/yefansky/CodeReDesign/releases/download/latest/md5.txt');
77+
remoteMd5 = await this.downloadText('https://github.com/yefansky/CodeReDesign/releases/download/latest/md5.txt');
7778

7879
// Calculate local rag.exe MD5
7980
const localMd5 = await this.calculateFileMd5(exePath);
@@ -101,7 +102,6 @@ class RagService {
101102

102103
// Verify downloaded file's MD5
103104
const newMd5 = await this.calculateFileMd5(exePath);
104-
const remoteMd5 = await this.downloadText('https://github.com/yefansky/CodeReDesign/releases/download/latest/md5.txt');
105105
if (newMd5.toLowerCase() !== remoteMd5.trim().toLowerCase()) {
106106
throw new Error('Downloaded rag.exe MD5 verification failed');
107107
}
@@ -123,41 +123,44 @@ class RagService {
123123
}
124124

125125
private async downloadText(url: string): Promise<string> {
126-
return new Promise((resolve, reject) => {
127-
https.get(url, (res) => {
128-
let data = '';
129-
res.on('data', (chunk) => { data += chunk; });
130-
res.on('end', () => {
131-
if (res.statusCode !== 200) {
132-
reject(new Error(`Failed to download text from ${url}: Status ${res.statusCode}`));
133-
} else {
134-
resolve(data);
135-
}
136-
});
137-
}).on('error', (err) => {
138-
reject(new Error(`Failed to download text from ${url}: ${err.message}`));
126+
try {
127+
const response = await axios.get(url, {
128+
responseType: 'text',
129+
maxRedirects: 5 // Default, can increase if needed
139130
});
140-
});
131+
console.log(`Downloaded ${url}, status: ${response.status}, redirects: ${response.request._redirectable._redirectCount}`);
132+
if (response.status !== 200) {
133+
throw new Error(`Failed to download text from ${url}: Status ${response.status}`);
134+
}
135+
return response.data.trim();
136+
} catch (err ) {
137+
throw new Error(`Failed to download text from ${url}: ${(err as Error).message}`);
138+
}
141139
}
142-
140+
143141
private async downloadFile(url: string, dest: string): Promise<void> {
144-
return new Promise((resolve, reject) => {
145-
const file = require('fs').createWriteStream(dest);
146-
https.get(url, (res) => {
147-
if (res.statusCode !== 200) {
148-
reject(new Error(`Failed to download file from ${url}: Status ${res.statusCode}`));
149-
return;
150-
}
151-
res.pipe(file);
152-
file.on('finish', () => {
153-
file.close();
154-
resolve();
142+
try {
143+
const response = await axios.get(url, {
144+
responseType: 'stream',
145+
maxRedirects: 5 // Default, can increase if needed
146+
});
147+
console.log(`Downloaded ${url}, status: ${response.status}, redirects: ${response.request._redirectable._redirectCount}`);
148+
if (response.status !== 200) {
149+
throw new Error(`Failed to download file from ${url}: Status ${response.status}`);
150+
}
151+
const writer = require('fs').createWriteStream(dest);
152+
response.data.pipe(writer);
153+
return new Promise((resolve, reject) => {
154+
writer.on('finish', resolve);
155+
writer.on('error', (err: Error) => {
156+
require('fs').unlink(dest, () => {}); // Clean up partial download
157+
reject(new Error(`Failed to write file to ${dest}: ${err.message}`));
155158
});
156-
}).on('error', (err) => {
157-
require('fs').unlink(dest, () => {}); // Clean up partial download
158-
reject(new Error(`Failed to download file from ${url}: ${err.message}`));
159159
});
160-
});
160+
} catch (err) {
161+
require('fs').unlink(dest, () => {}); // Clean up partial download
162+
throw new Error(`Failed to download file from ${url}: ${(err as Error).message}`);
163+
}
161164
}
162165

163166
private async calculateFileMd5(filePath: string): Promise<string> {

0 commit comments

Comments
 (0)