Skip to content

Commit

Permalink
Download function bugfix
Browse files Browse the repository at this point in the history
  • Loading branch information
6174 committed Jan 20, 2024
1 parent 4567752 commit 37ae1b5
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import { getAppTmpDir } from "../utils/get-appdata-dir";
import { checkIfInstalled } from "../comfyui/bootstrap";
import * as fsExtra from "fs-extra"
import logger from "../utils/logger";
import { URL } from 'url';

const appTmpDir = getAppTmpDir();

/**
Expand Down Expand Up @@ -94,21 +96,25 @@ async function unzipInstall(dispatcher: TaskEventDispatcher, files: string[]): P
return true;
}


async function copyInstall(dispatcher: TaskEventDispatcher, files: string[], jsPathName: string | null = null): Promise<boolean> {
for (const url of files) {
let cleanUrl: string = url;
if (cleanUrl.endsWith('/')) {
cleanUrl = cleanUrl.slice(0, -1);
}
try {
let myurl = new URL(cleanUrl);
let pathname = myurl.pathname;
let filename = path.basename(pathname);
let ext = path.extname(pathname);
const fileNameWithExt = filename + ext;
if (cleanUrl.endsWith('.py')) {
dispatcher({
message: `Start copy ${cleanUrl} to install`
});
await downloadUrl(dispatcher, url, EXTENTION_FOLDER);
await downloadUrl(dispatcher, url, path.resolve(EXTENTION_FOLDER, fileNameWithExt));
} else {
const targetPath: string = path.join(WEB_EXTENTION_FOLDER, jsPathName || '');
const targetPath: string = path.join(WEB_EXTENTION_FOLDER, jsPathName || '', fileNameWithExt);
if (!fs.existsSync(targetPath)) {
fs.mkdirSync(targetPath, { recursive: true });
}
Expand Down
3 changes: 2 additions & 1 deletion apps/node/src/modules/utils/__test__/download-url.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import path from "path";
import { downloadUrl } from "../download-url";
import { getAppDataDir } from "../get-appdata-dir";

Expand All @@ -7,7 +8,7 @@ async function testDownload() {
try {
const ret = await downloadUrl((event) => {
console.log(event)
}, "https://repo.anaconda.com/miniconda/Miniconda3-latest-MacOSX-x86_64.sh", dataURL);
}, "https://repo.anaconda.com/miniconda/Miniconda3-latest-MacOSX-x86_64.sh", path.resolve(dataURL, "Miniconda3-latest-MacOSX-x86_64.sh"));
} catch(err) {
console.log(err);
}
Expand Down
9 changes: 4 additions & 5 deletions apps/node/src/modules/utils/download-url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,14 @@ import { TaskEventDispatcher } from '../task-queue/task-queue';
import logger from './logger';
import { HttpsProxyAgent } from 'https-proxy-agent';
import { systemProxy, systemProxyString } from './env';
import { sys } from 'typescript';

import * as fsExtra from "fs-extra";
export async function downloadUrl(dispatch: TaskEventDispatcher, url: string, targetPath: string): Promise<void> {
const filename: string = path.basename(url);
const filePath: string = path.join(targetPath, filename);
dispatch({
message: `Downloading ${url}`
})
});
try {
await fsExtra.ensureDir(path.dirname(targetPath));
const headers: { [key: string]: string } = {
'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',
};
Expand Down Expand Up @@ -47,7 +46,7 @@ export async function downloadUrl(dispatch: TaskEventDispatcher, url: string, ta
const finished = util.promisify(stream.finished);

const bufferStream = new stream.PassThrough();
const writeStream = fs.createWriteStream(filePath);
const writeStream = fs.createWriteStream(targetPath);

bufferStream.on('error', (err: any) => {
logger.error(`Error with buffer stream: ${err.message}`);
Expand Down

0 comments on commit 37ae1b5

Please sign in to comment.