Fix attachments

This commit is contained in:
Rory&
2026-04-15 21:44:13 +02:00
parent 5b501fa8da
commit 103922e841
16 changed files with 141 additions and 117 deletions
+8 -10
View File
@@ -171,12 +171,11 @@ async function processMedia(media: UnfurledMediaItem, messageId: string, batchId
const realAtt = Attachment.create({
filename: attEnt.userFilename,
url: media.url,
proxy_url: media.proxy_url,
size: attEnt.size,
height: attEnt.height,
width: attEnt.width,
content_type: attEnt.contentType || attEnt.userOriginalContentType,
channel_id: channel.id,
});
await realAtt.save();
@@ -396,12 +395,11 @@ export async function handleMessage(opts: MessageOptions): Promise<Message> {
const realAtt = Attachment.create({
filename: attEnt.userFilename,
url: `${conf.cdn.endpointPublic}/${cloneRespBody.new_path}`,
proxy_url: `${conf.cdn.endpointPublic}/${cloneRespBody.new_path}`,
size: attEnt.size,
height: attEnt.height,
width: attEnt.width,
content_type: attEnt.contentType || attEnt.userOriginalContentType,
channel_id: channel.id,
});
await realAtt.save();
return { attachment: realAtt, index: att.index };
@@ -744,22 +742,22 @@ export async function handleMessage(opts: MessageOptions): Promise<Message> {
const footer = embed.footer;
const footerAttachment = fetchAttachment(footer?.icon_url);
if (footerAttachment !== undefined) {
footer!.icon_url = footerAttachment.url;
footer!.proxy_icon_url = footerAttachment.proxy_url;
footer!.icon_url = footerAttachment.toJSON().url;
footer!.proxy_icon_url = footerAttachment.toJSON().proxy_url;
}
const image = embed.image;
const imageAttachment = fetchAttachment(image?.url);
if (imageAttachment !== undefined) {
image!.url = imageAttachment.url;
image!.proxy_url = imageAttachment.proxy_url;
image!.url = imageAttachment.toJSON().url;
image!.proxy_url = imageAttachment.toJSON().proxy_url;
}
const author = embed.author;
const authorAttachment = fetchAttachment(author?.icon_url);
if (authorAttachment !== undefined) {
author!.icon_url = authorAttachment.url;
author!.proxy_icon_url = authorAttachment.proxy_url;
author!.icon_url = authorAttachment.toJSON().url;
author!.proxy_icon_url = authorAttachment.toJSON().proxy_url;
}
}
message.attachments = message.attachments?.filter((_, index) => {
+5 -3
View File
@@ -1,5 +1,5 @@
import { handleMessage, postHandleMessage } from "@spacebar/api";
import { Attachment, Channel, Config, DiscordApiErrors, emitEvent, FieldErrors, Message, MessageCreateEvent, uploadFile, ValidateName, Webhook } from "@spacebar/util";
import { Attachment, Channel, Config, DiscordApiErrors, emitEvent, FieldErrors, Message, MessageCreateEvent, Snowflake, uploadFile, ValidateName, Webhook } from "@spacebar/util";
import { Request, Response } from "express";
import { HTTPError } from "lambert-server";
import { MoreThan } from "typeorm";
@@ -7,6 +7,7 @@ import { WebhookExecuteSchema } from "@spacebar/schemas";
export const executeWebhook = async (req: Request, res: Response) => {
const body = req.body as WebhookExecuteSchema;
const messageId = Snowflake.generate();
const { webhook_id, token } = req.params as { [key: string]: string };
@@ -87,8 +88,8 @@ export const executeWebhook = async (req: Request, res: Response) => {
const files = (req.files as Express.Multer.File[]) ?? [];
for (const currFile of files) {
try {
const file = await uploadFile(`/attachments/${sendChannel.id}`, currFile);
attachments.push(Attachment.create({ ...file, proxy_url: file.url }));
const file = await uploadFile(`/attachments/${sendChannel.id}/${messageId}`, currFile);
attachments.push(Attachment.create(file));
} catch (error) {
if (wait) res.status(400).json({ message: error?.toString() });
return;
@@ -106,6 +107,7 @@ export const executeWebhook = async (req: Request, res: Response) => {
: undefined,
} as Parameters<typeof handleMessage>[0];
const message = await handleMessage({
id: messageId,
...bodyMsg,
username: body.username || webhook.name,
avatar_url: body.avatar_url || webhook.avatar,