Skip to content

Commit

Permalink
Update upload.js
Browse files Browse the repository at this point in the history
  • Loading branch information
TheRealDax committed Nov 29, 2023
1 parent 3d6746a commit ffe255a
Showing 1 changed file with 40 additions and 19 deletions.
59 changes: 40 additions & 19 deletions endpoints/upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,18 @@
* application/json:
* example:
* result: File send successful
* '400':
* description: The file size is too large or an invalid file type was detected.
* content:
* application/json:
* example:
* error: File size is too large or invalid file type
* '404':
* description: The file extension was not detected, and the file was sent as a URL.
* description: Invalid file type detected, and the file was sent as a URL.
* content:
* application/json:
* example:
* result: File extension was not detected, file was sent as a URL
* result: Invalid file type. File was sent to Discord as a URL
* '500':
* description: An error occurred during the file upload process.
* content:
Expand All @@ -60,6 +66,8 @@ const axios = require('axios');
const intents = [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages];
const { fromBuffer } = require('file-type-cjs-fix/file-type');

MAX_FILE_SIZE = 1024 * 1024 * 8;

const upload = async (req, res) => {
try {
const { authorization: token } = req.headers;
Expand All @@ -76,26 +84,39 @@ const upload = async (req, res) => {
console.log(`Logged in ${client.user.id}`);
});

const guild = await client.guilds.fetch(serverid);
console.log(`${guild.name} ${guild.id}`);
const channel = await client.channels.fetch(channelid);
const upload = await axios.get(file, { responseType: 'arraybuffer' });
const buffer = Buffer.from(upload.data);
const fileType = await fromBuffer(buffer);
if (fileType) {
const attachment = new AttachmentBuilder(buffer, { name: `${name}.${fileType.ext}` });
await channel.send({
content: message,
files: [attachment],
});
const response = await axios.head(file);

if (response.headers['content-type'].includes('text/html')) {
const guild = await client.guilds.fetch(serverid);
console.log(`${guild.name} ${guild.id}`);
const channel = await client.channels.fetch(channelid);

await channel.send(`${message}\n${file}`);
client.removeAllListeners();
client.destroy();
return res.status(200).json({ result: 'File send successful' });
return res.status(404).json({ result: 'Invalid file type. File was sent to Discord as a URL' });
} else {
await channel.send(`${message} ${file}`);
client.removeAllListeners();
client.destroy();
return res.status(404).json({ result: 'File extension was not detected, file was sent as a url' });
const fileSize = parseInt(response.headers['content-length']);
if (fileSize && fileSize > MAX_FILE_SIZE) {
return res.status(400).json({ error: 'File size is too large' });
}

const guild = await client.guilds.fetch(serverid);
console.log(`${guild.name} ${guild.id}`);
const channel = await client.channels.fetch(channelid);

const upload = await axios.get(file, { responseType: 'arraybuffer' });
const buffer = Buffer.from(upload.data);
const fileType = await fromBuffer(buffer);

const attachment = new AttachmentBuilder(buffer, { name: `${name}.${fileType.ext}` });
await channel.send({
content: message,
files: [attachment],
});
client.removeAllListeners();
client.destroy();
return res.status(200).json({ result: 'File send successful' });
}
} catch (error) {
console.log(error);
Expand Down

0 comments on commit ffe255a

Please sign in to comment.