2023-04-10 05:49:50 +09:00
|
|
|
/*
|
|
|
|
* SPDX-License-Identifier: GPL-3.0
|
|
|
|
* Vencord Desktop, a desktop app aiming to give you a snappier Discord Experience
|
|
|
|
* Copyright (c) 2023 Vendicated and Vencord contributors
|
|
|
|
*/
|
|
|
|
|
2023-04-04 07:41:52 +09:00
|
|
|
import { createWriteStream } from "fs";
|
|
|
|
import type { IncomingMessage } from "http";
|
2023-04-10 05:49:50 +09:00
|
|
|
import { get, RequestOptions } from "https";
|
2023-04-04 07:41:52 +09:00
|
|
|
import { finished } from "stream/promises";
|
|
|
|
|
|
|
|
export async function downloadFile(url: string, file: string, options: RequestOptions = {}) {
|
|
|
|
const res = await simpleReq(url, options);
|
|
|
|
await finished(
|
2023-04-10 05:49:50 +09:00
|
|
|
res.pipe(
|
|
|
|
createWriteStream(file, {
|
|
|
|
autoClose: true
|
|
|
|
})
|
|
|
|
)
|
2023-04-04 07:41:52 +09:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function simpleReq(url: string, options: RequestOptions = {}) {
|
|
|
|
return new Promise<IncomingMessage>((resolve, reject) => {
|
|
|
|
get(url, options, res => {
|
|
|
|
const { statusCode, statusMessage, headers } = res;
|
2023-04-10 05:49:50 +09:00
|
|
|
if (statusCode! >= 400) return void reject(`${statusCode}: ${statusMessage} - ${url}`);
|
|
|
|
if (statusCode! >= 300) return simpleReq(headers.location!, options).then(resolve).catch(reject);
|
2023-04-04 07:41:52 +09:00
|
|
|
|
|
|
|
resolve(res);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function simpleGet(url: string, options: RequestOptions = {}) {
|
|
|
|
const res = await simpleReq(url, options);
|
|
|
|
|
|
|
|
return new Promise<Buffer>((resolve, reject) => {
|
|
|
|
const chunks = [] as Buffer[];
|
|
|
|
|
|
|
|
res.once("error", reject);
|
|
|
|
res.on("data", chunk => chunks.push(chunk));
|
|
|
|
res.once("end", () => resolve(Buffer.concat(chunks)));
|
|
|
|
});
|
|
|
|
}
|