Stricter url fetch rules, use fetch, add image to readme

This commit is contained in:
TheGreenPig 2024-09-18 01:48:34 +02:00
parent 6c2222e295
commit f20de977b3
4 changed files with 33 additions and 31 deletions

View file

@ -1,3 +0,0 @@
# FileViewer
Allows you to Preview PDF Files without having to download them

View file

@ -1,28 +0,0 @@
/*
* Vencord, a Discord client mod
* Copyright (c) 2024 Vendicated and contributors
* SPDX-License-Identifier: GPL-3.0-or-later
*/
import { IpcMainInvokeEvent } from "electron";
import { request } from "https";
export async function getBufferResponse(_: IpcMainInvokeEvent, url: string) {
return new Promise<Buffer>((resolve, reject) => {
const req = request(new URL(url), { method: "GET" }, res => {
const chunks: Uint8Array[] = [];
res.on("data", function (chunk) {
chunks.push(chunk);
});
res.on("end", function () {
const body = Buffer.concat(chunks);
resolve(body);
});
});
req.on("error", reject);
req.end();
});
}

View file

@ -0,0 +1,5 @@
# PdfViewer
Allows you to Preview PDF Files without having to download them
![](https://github.com/user-attachments/assets/f403dff5-5edc-4e57-8503-73f709df4842)

View file

@ -0,0 +1,28 @@
/*
* Vencord, a Discord client mod
* Copyright (c) 2024 Vendicated and contributors
* SPDX-License-Identifier: GPL-3.0-or-later
*/
import { IpcMainInvokeEvent } from "electron";
const urlChecks = [
(url: URL) => url.host === "cdn.discordapp.com",
(url: URL) => url.pathname.startsWith("/attachments/"),
(url: URL) => url.pathname.endsWith(".pdf")
];
export async function getBufferResponse(_: IpcMainInvokeEvent, url: string): Promise<Buffer> {
const urlObj = new URL(url);
if (!urlChecks.every(check => check(urlObj))) {
throw new Error("Invalid URL");
}
const response = await fetch(url);
if (!response.ok) {
throw new Error(`Failed to fetch: ${response.statusText}`);
}
const arrayBuffer = await response.arrayBuffer();
return Buffer.from(arrayBuffer);
}