mirror of
https://github.com/Vendicated/Vencord.git
synced 2024-09-19 22:20:34 +00:00
Stricter url fetch rules, use fetch, add image to readme
This commit is contained in:
parent
6c2222e295
commit
f20de977b3
4 changed files with 33 additions and 31 deletions
|
@ -1,3 +0,0 @@
|
|||
# FileViewer
|
||||
|
||||
Allows you to Preview PDF Files without having to download them
|
|
@ -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();
|
||||
});
|
||||
}
|
||||
|
5
src/plugins/pdfViewer.desktop/README.md
Normal file
5
src/plugins/pdfViewer.desktop/README.md
Normal 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)
|
28
src/plugins/pdfViewer.desktop/native.ts
Normal file
28
src/plugins/pdfViewer.desktop/native.ts
Normal 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);
|
||||
}
|
Loading…
Reference in a new issue