Refactor image job object, fixed image commands that don't require an existing image

This commit is contained in:
Essem 2021-12-09 22:31:52 -06:00
parent 30bcb7a969
commit 39ebae8cd7
No known key found for this signature in database
GPG Key ID: 7D497397CC3A2A8C
5 changed files with 28 additions and 12 deletions

View File

@ -31,3 +31,18 @@ A client sends *requests* (T-messages) to a server, which subsequently *replies*
- Twait tag[2] jid[4]
- Rwait tag[2]
- Rinit tag[2] max_jobs[2] formats[j]
### Job Object
The job object is formatted like this:
```json
{
"cmd": string, // name of internal image command, e.g. caption
"path": string, // canonical image URL, used for getting the actual image
"url": string, // original image URL, used for message filtering
"params": { // content varies depending on the command, some common parameters are listed here
"type": string, // mime type of output, should usually be the same as input
"delay": integer, // for manually specifying GIF frame delay, set to 0 to use the default delay
...
}
}
```

View File

@ -241,7 +241,7 @@ const runJob = (job, ws) => {
const object = JSON.parse(job.msg);
// If the image has a path, it must also have a type
if (object.path && !object.type) {
if (object.path && !object.params.type) {
reject(new TypeError("Unknown image type"));
}

View File

@ -47,7 +47,8 @@ class ImageCommand extends Command {
runningCommands.set(this.message.author.id, this.message.createdAt);
const magickParams = {
cmd: this.constructor.command
cmd: this.constructor.command,
params: {}
};
if (this.constructor.requiresImage) {
@ -64,9 +65,9 @@ class ImageCommand extends Command {
return "I've been rate-limited by Tenor. Please try uploading your GIF elsewhere.";
}
magickParams.path = image.path;
magickParams.type = image.type;
magickParams.params.type = image.type;
magickParams.url = image.url; // technically not required but can be useful for text filtering
magickParams.delay = image.delay ? image.delay : 0;
magickParams.params.delay = image.delay ? image.delay : 0;
if (this.constructor.requiresGIF) magickParams.onlyGIF = true;
} catch (e) {
runningCommands.delete(this.message.author.id);
@ -84,15 +85,15 @@ class ImageCommand extends Command {
switch (typeof this.params) {
case "function":
Object.assign(magickParams, this.params(magickParams.url, magickParams.delay));
Object.assign(magickParams.params, this.params(magickParams.url, magickParams.delay));
break;
case "object":
Object.assign(magickParams, this.params);
Object.assign(magickParams.params, this.params);
break;
}
let status;
if (magickParams.type === "image/gif") {
if (magickParams.params.type === "image/gif") {
status = await this.processMessage(this.message);
} else {
this.client.sendChannelTyping(this.message.channel.id);

View File

@ -11,7 +11,7 @@ function run(object) {
// If the image has a path, it must also have a type
let promise = new Promise((resolveTest) => { resolveTest(); }); // no-op
if (object.path) {
if (object.type !== "image/gif" && object.onlyGIF) resolve({
if (object.params.type !== "image/gif" && object.onlyGIF) resolve({
buffer: Buffer.alloc(0),
fileExtension: "nogif"
});
@ -20,10 +20,10 @@ function run(object) {
// Convert from a MIME type (e.g. "image/png") to something ImageMagick understands (e.g. "png").
// Don't set `type` directly on the object we are passed as it will be read afterwards.
// If no image type is given (say, the command generates its own image), make it a PNG.
const fileExtension = object.type ? object.type.split("/")[1] : "png";
const fileExtension = object.params.type ? object.params.type.split("/")[1] : "png";
promise.then(buf => {
object.data = buf;
const objectWithFixedType = Object.assign({}, object, {type: fileExtension});
object.params.data = buf;
const objectWithFixedType = Object.assign({}, object.params, {type: fileExtension});
try {
const result = magick[object.cmd](objectWithFixedType);
const returnObject = {

View File

@ -71,7 +71,7 @@ class ImageWorker extends BaseServiceWorker {
if (connection.conn.readyState !== 0 && connection.conn.readyState !== 1) {
continue;
}
if (!connection.formats[object.cmd].includes(object.type)) continue;
if (object.params.type && !connection.formats[object.cmd].includes(object.params.type)) continue;
idealServers.push({
addr: address,
load: connection.njobs / connection.max