2021-08-19 14:19:14 +00:00
|
|
|
import fetch from "node-fetch";
|
|
|
|
import fs from "fs";
|
2022-02-02 17:01:33 +00:00
|
|
|
import { fileTypeFromBuffer, fileTypeFromFile } from "file-type";
|
2020-10-18 21:53:35 +00:00
|
|
|
|
2021-11-29 21:27:13 +00:00
|
|
|
const formats = ["image/jpeg", "image/png", "image/webp", "image/gif", "video/mp4", "video/webm", "video/quicktime"];
|
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
|
|
|
|
2022-07-26 15:38:42 +00:00
|
|
|
export const servers = JSON.parse(fs.readFileSync(new URL("../config/servers.json", import.meta.url), { 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")) {
|
2022-02-02 17:01:33 +00:00
|
|
|
const imageType = await fileTypeFromFile(image);
|
2020-11-26 17:48:19 +00:00
|
|
|
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;
|
|
|
|
}
|
2021-11-20 06:55:25 +00:00
|
|
|
const imageBuffer = await imageRequest.arrayBuffer();
|
2022-02-02 17:01:33 +00:00
|
|
|
const imageType = await fileTypeFromBuffer(imageBuffer);
|
2020-11-26 17:48:19 +00:00
|
|
|
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
|
|
|
}
|