mirror of
https://github.com/ZeppelinBot/Zeppelin.git
synced 2025-05-10 12:25:02 +00:00
Migrate ChannelArchiver to new Plugin structure, isOwner -> pluginUtils
This commit is contained in:
parent
a3d0ec03d9
commit
90ee4ad909
6 changed files with 174 additions and 0 deletions
29
backend/src/plugins/ChannelArchiver/rehostAttachment.ts
Normal file
29
backend/src/plugins/ChannelArchiver/rehostAttachment.ts
Normal file
|
@ -0,0 +1,29 @@
|
|||
import { Attachment, TextChannel } from "eris";
|
||||
import { downloadFile } from "src/utils";
|
||||
import fs from "fs";
|
||||
const fsp = fs.promises;
|
||||
|
||||
const MAX_ATTACHMENT_REHOST_SIZE = 1024 * 1024 * 8;
|
||||
|
||||
export async function rehostAttachment(attachment: Attachment, targetChannel: TextChannel): Promise<string> {
|
||||
if (attachment.size > MAX_ATTACHMENT_REHOST_SIZE) {
|
||||
return "Attachment too big to rehost";
|
||||
}
|
||||
|
||||
let downloaded;
|
||||
try {
|
||||
downloaded = await downloadFile(attachment.url, 3);
|
||||
} catch (e) {
|
||||
return "Failed to download attachment after 3 tries";
|
||||
}
|
||||
|
||||
try {
|
||||
const rehostMessage = await targetChannel.createMessage(`Rehost of attachment ${attachment.id}`, {
|
||||
name: attachment.filename,
|
||||
file: await fsp.readFile(downloaded.path),
|
||||
});
|
||||
return rehostMessage.attachments[0].url;
|
||||
} catch (e) {
|
||||
return "Failed to rehost attachment";
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue