2021-08-19 14:19:14 +00:00
|
|
|
import fetch from "node-fetch";
|
|
|
|
import fs from "fs";
|
|
|
|
import fileType from "file-type";
|
2020-10-18 21:53:35 +00:00
|
|
|
|
2021-04-19 14:31:39 +00:00
|
|
|
const formats = ["image/jpeg", "image/png", "image/webp", "image/gif", "video/mp4", "video/webm", "video/mov"];
|
2020-08-28 02:34:12 +00:00
|
|
|
|
2021-08-19 14:19:14 +00:00
|
|
|
export const jobs = {};
|
2021-01-18 20:11:28 +00:00
|
|
|
|
2021-08-19 14:19:14 +00:00
|
|
|
export const connections = new Map();
|
2021-01-18 20:11:28 +00:00
|
|
|
|
2021-08-19 14:19:14 +00:00
|
|
|
export const servers = JSON.parse(fs.readFileSync("./servers.json", { encoding: "utf8" })).image;
|
Class commands, improved sharding, and many other changes (#88)
* Load commands recursively
* Sort commands
* Missed a couple of spots
* missed even more spots apparently
* Ported commands in "fun" category to new class-based format, added babel eslint plugin
* Ported general commands, removed old/unneeded stuff, replaced moment with day, many more fixes I lost track of
* Missed a spot
* Removed unnecessary abort-controller package, add deprecation warning for mongo database
* Added imagereload, clarified premature end message
* Fixed docker-compose path issue, added total bot uptime to stats, more fixes for various parts
* Converted image commands into classes, fixed reload, ignore another WS event, cleaned up command handler and image runner
* Converted music/soundboard commands to class format
* Cleanup unnecessary logs
* awful tag command class port
* I literally somehow just learned that you can leave out the constructor in classes
* Pass client directly to commands/events, cleaned up command handler
* Migrated bot to eris-sharder, fixed some error handling stuff
* Remove unused modules
* Fixed type returning
* Switched back to Eris stable
* Some fixes and cleanup
* might wanna correct this
* Implement image command ratelimiting
* Added Bot token prefix, added imagestats, added running endpoint to API
2021-04-12 16:16:12 +00:00
|
|
|
|
2021-08-19 14:19:14 +00:00
|
|
|
export async function getType(image, extraReturnTypes) {
|
2020-11-26 17:48:19 +00:00
|
|
|
if (!image.startsWith("http")) {
|
|
|
|
const imageType = await fileType.fromFile(image);
|
|
|
|
if (imageType && formats.includes(imageType.mime)) {
|
|
|
|
return imageType.mime;
|
|
|
|
}
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
let type;
|
Class commands, improved sharding, and many other changes (#88)
* Load commands recursively
* Sort commands
* Missed a couple of spots
* missed even more spots apparently
* Ported commands in "fun" category to new class-based format, added babel eslint plugin
* Ported general commands, removed old/unneeded stuff, replaced moment with day, many more fixes I lost track of
* Missed a spot
* Removed unnecessary abort-controller package, add deprecation warning for mongo database
* Added imagereload, clarified premature end message
* Fixed docker-compose path issue, added total bot uptime to stats, more fixes for various parts
* Converted image commands into classes, fixed reload, ignore another WS event, cleaned up command handler and image runner
* Converted music/soundboard commands to class format
* Cleanup unnecessary logs
* awful tag command class port
* I literally somehow just learned that you can leave out the constructor in classes
* Pass client directly to commands/events, cleaned up command handler
* Migrated bot to eris-sharder, fixed some error handling stuff
* Remove unused modules
* Fixed type returning
* Switched back to Eris stable
* Some fixes and cleanup
* might wanna correct this
* Implement image command ratelimiting
* Added Bot token prefix, added imagestats, added running endpoint to API
2021-04-12 16:16:12 +00:00
|
|
|
const controller = new AbortController(); // eslint-disable-line no-undef
|
2020-11-26 17:48:19 +00:00
|
|
|
const timeout = setTimeout(() => {
|
|
|
|
controller.abort();
|
|
|
|
}, 25000);
|
|
|
|
try {
|
2021-05-22 15:10:42 +00:00
|
|
|
const imageRequest = await fetch(image, {
|
|
|
|
signal: controller.signal, headers: {
|
|
|
|
"Range": "bytes=0-1023"
|
|
|
|
}
|
|
|
|
});
|
2021-01-04 16:29:18 +00:00
|
|
|
clearTimeout(timeout);
|
2021-05-15 03:02:50 +00:00
|
|
|
const size = imageRequest.headers.has("Content-Range") ? imageRequest.headers.get("Content-Range").split("/")[1] : imageRequest.headers.get("Content-Length");
|
2021-06-28 22:59:05 +00:00
|
|
|
if (parseInt(size) > 26214400 && extraReturnTypes) { // 25 MB
|
2021-05-07 03:01:30 +00:00
|
|
|
type = "large";
|
|
|
|
return type;
|
|
|
|
}
|
2020-11-26 17:48:19 +00:00
|
|
|
const imageBuffer = await imageRequest.buffer();
|
|
|
|
const imageType = await fileType.fromBuffer(imageBuffer);
|
|
|
|
if (imageType && formats.includes(imageType.mime)) {
|
|
|
|
type = imageType.mime;
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
if (error.name === "AbortError") {
|
|
|
|
throw Error("Timed out");
|
|
|
|
} else {
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
clearTimeout(timeout);
|
|
|
|
}
|
|
|
|
return type;
|
2021-08-19 14:19:14 +00:00
|
|
|
}
|