Skip to content

Commit

Permalink
Leave the previous readFile params
Browse files Browse the repository at this point in the history
  • Loading branch information
Retribution98 committed Jan 27, 2025
1 parent 3d2917c commit 8899e7d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
16 changes: 10 additions & 6 deletions src/bindings/js/node/scripts/lib/utils.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const path = require('node:path');
const https = require('node:https');
const fs = require('node:fs/promises');
const { createWriteStream } = require('node:fs');
Expand Down Expand Up @@ -56,15 +57,18 @@ async function checkIfPathExists(path) {
*
* @function downloadFile
* @param {string} url - The file URL.
* @param {string} filePath - Path to downloaded file.
* @param {string} filename - The filename of result file.
* @param {string} destination - The destination path of result file.
* @param {string} [proxy=null] - (Optional) The proxy URL.
* @returns {Promise<string>} - Path to downloaded file.
*/
function downloadFile(url, filePath, proxy = null) {
console.log(`Downloading file by link: ${url} to ${filePath}.`);
function downloadFile(url, destination, filename, proxy = null) {
console.log(`Downloading file by link: ${url} to ${destination}`
+ `with filename: ${filename}`);

const timeout = 5000;
const file = createWriteStream(filePath);
const fullPath = path.resolve(destination, filename);
const file = createWriteStream(fullPath);

if (new URL(url).protocol === 'http:')
throw new Error('Http link doesn\'t support');
Expand Down Expand Up @@ -94,8 +98,8 @@ function downloadFile(url, filePath, proxy = null) {

file.on('finish', () => {
file.close();
console.log(`File was successfully downloaded to '${filePath}'.`);
resolve(filePath);
console.log(`File was successfully downloaded to '${fullPath}'.`);
resolve(fullPath);
});
});

Expand Down
16 changes: 13 additions & 3 deletions src/bindings/js/node/tests/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,21 @@ async function downloadTestModel(model) {
const { env } = process;
const proxyUrl = env.http_proxy || env.HTTP_PROXY || env.npm_config_proxy;

const modelExists = await checkIfPathExists(model.xml);
if (!modelExists) await downloadFile(model.xmlURL, model.xml, proxyUrl);
const modelExists = await checkIfPathExists(model.xml);
if (!modelExists) await downloadFile(
model.xmlURL,
path.dirname(model.xml),
path.basename(model.xml),
proxyUrl,
);

const weightsExists = await checkIfPathExists(model.bin);
if (!weightsExists) await downloadFile(model.binURL, model.bin, proxyUrl);
if (!weightsExists) await downloadFile(
model.binURL,
path.dirname(model.bin),
path.basename(model.bin),
proxyUrl,
);

} catch(error) {
console.error(`Failed to download the model: ${error}.`);
Expand Down

0 comments on commit 8899e7d

Please sign in to comment.