Clean up image API code paths (#49)
* Document image.js a bit * Close image.js sockets in all code paths I'm not sure whether sockets get GC'd when the function returns * Remove getFormat It was only called from one place, and the object property names were quite confusing * Clean up image.js conditional a bit I had to write out an entire truth table for this and work it all out Thinking hard * Move actual ImageMagick calling into separate file This gets rid of the weird, brain-melting ouroboros of code that recurses across threads and processes. * Reduce amount of getType wrangling This amounted to an awful lot of dead conditionals after the image commands were all modified to pass in image types anyway. This has also led to two different implementations diverging, which causes bugs like GIF commands applied to non-GIFs erroring instead of providing a user-friendly message. * Unify image-runner return type, clarify image type This allows us to remove the fromAPI parameter from image-runner, and helps greatly clarify the behavior around image types. * Deduplicate GIF code, fix "not a GIF" handling The special "nogif" value is now stored as the image type instead of its value, as the value must always be a Buffer now--no loosely-typed shenanigans.
This commit is contained in:
parent
9069ed5a34
commit
3de4858b5a
9 changed files with 89 additions and 85 deletions
27
api/index.js
27
api/index.js
|
@ -2,8 +2,7 @@
|
|||
|
||||
require("dotenv").config();
|
||||
const os = require("os");
|
||||
const magick = require("../utils/image.js");
|
||||
const execPromise = require("util").promisify(require("child_process").exec);
|
||||
const { run } = require("../utils/image-runner.js");
|
||||
const net = require("net");
|
||||
const dgram = require("dgram"); // for UDP servers
|
||||
const socket = dgram.createSocket("udp4"); // Our universal UDP socket, this might cause issues and we may have to use a seperate socket for each connection
|
||||
|
@ -95,31 +94,17 @@ const runJob = (job) => {
|
|||
log(`Job ${job.uuid} starting...`, job.num);
|
||||
|
||||
const object = JSON.parse(job.msg);
|
||||
let type;
|
||||
if (object.path) {
|
||||
type = object.type;
|
||||
if (!object.type) {
|
||||
type = await magick.getType(object.path);
|
||||
}
|
||||
if (!type) {
|
||||
reject(new TypeError("Unknown image type"));
|
||||
}
|
||||
object.type = type.split("/")[1];
|
||||
if (object.type !== "gif" && object.onlyGIF) reject(new TypeError(`Expected a GIF, got ${object.type}`));
|
||||
object.delay = object.delay ? object.delay : 0;
|
||||
}
|
||||
|
||||
if (object.type === "gif" && !object.delay) {
|
||||
const delay = (await execPromise(`ffprobe -v 0 -of csv=p=0 -select_streams v:0 -show_entries stream=r_frame_rate ${object.path}`)).stdout.replace("\n", "");
|
||||
object.delay = (100 / delay.split("/")[0]) * delay.split("/")[1];
|
||||
// If the image has a path, it must also have a type
|
||||
if (object.path && !object.type) {
|
||||
reject(new TypeError("Unknown image type"));
|
||||
}
|
||||
|
||||
log(`Job ${job.uuid} started`, job.num);
|
||||
const data = await magick.run(object, true);
|
||||
const {buffer, fileExtension} = await run(object);
|
||||
|
||||
log(`Sending result of job ${job.uuid} back to the bot`, job.num);
|
||||
const server = net.createServer(function(tcpSocket) {
|
||||
tcpSocket.write(Buffer.concat([Buffer.from(type ? type : "image/png"), Buffer.from("\n"), data]), (err) => {
|
||||
tcpSocket.write(Buffer.concat([Buffer.from(fileExtension), Buffer.from("\n"), buffer]), (err) => {
|
||||
if (err) console.error(err);
|
||||
tcpSocket.end(() => {
|
||||
server.close();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue