mod-manager/src/io/file_downloder.ts

20 lines
728 B
TypeScript
Raw Normal View History

2022-08-03 10:00:36 -04:00
import path from "path";
import * as https from "https";
import {createWriteStream} from "fs";
import DownloadError from "../errors/download_error.js";
2022-08-04 09:24:39 -04:00
import Mods from "../mods/mods.js";
2022-08-03 10:00:36 -04:00
export default class FileDownloader {
static downloadMod(task: DownloadTask): void {
https.get(task.url, res => {
2022-08-04 09:24:39 -04:00
const filePath = path.join(Mods.MODS_FOLDER_PATH, task.fileName);
2022-08-03 10:00:36 -04:00
const writeStream = createWriteStream(filePath);
res.pipe(writeStream);
writeStream.on("finish", () => writeStream.close());
writeStream.on('error', () => {
throw new DownloadError(`Failed to download ${task.fileName} from ${task.url}`)
})
})
}
}