Equicord/src/equicordplugins/favouriteAnything/native.ts
Davri 8f2cc6d52a
Rewrite favouriteAnything (#830)
* Add PendingReplyStore types

* Update uploadHandler and embedMedia types

* Update scroller types

* Update favouriteAnything

* Fix find

* Revert the devs array

* Move prop types to types.ts, separate AttachmentPreview lookups

* Update copyright

* Inline imports

* Fix crash

* Minor changes

* Replace useResizeObserver

* Add hostname checks to fetchAttachment

* Use managed style instead of direct import

* Update description

* Move ImageUtils to top

* change find

---------

Co-authored-by: thororen1234 <78185467+thororen1234@users.noreply.github.com>
2026-02-25 08:47:42 -05:00

31 lines
1.1 KiB
TypeScript

/*
* Vencord, a Discord client mod
* Copyright (c) 2026 Vendicated and contributors
* SPDX-License-Identifier: GPL-3.0-or-later
*/
import { MessageAttachment } from "@vencord/discord-types";
const allowedHosts = new Set([
"cdn.discordapp.com",
"images-ext-1.discordapp.net",
"images-ext-2.discordapp.net",
"media.discordapp.net"
]);
// Discord has very strict CORS rules for which types of assets can be fetched from where (CDN/Media proxy),
// and most binary file types are prohibited by both. This function serves as a simple bypass.
export async function fetchAttachment(_: unknown, attachment: MessageAttachment) {
const { content_type, filename } = attachment;
const url = URL.parse(attachment.url);
if (!url || !allowedHosts.has(url.hostname)) throw new Error("Invalid URL");
const res = await fetch(url, { headers: { Accept: "*/*" } });
if (!res.ok) throw new Error("Server error");
const blob = await res.blob();
const type = blob.type || content_type || "application/octet-stream";
const data = await blob.arrayBuffer();
return { type, data, filename };
}