mirror of
https://github.com/gchq/CyberChef.git
synced 2025-04-20 23:06:16 -04:00
Change run() functions to be async
This commit is contained in:
parent
74c2a2b5cb
commit
a0b94bba4e
3 changed files with 24 additions and 68 deletions
|
@ -36,27 +36,14 @@ class DitherImage extends Operation {
|
||||||
* @param {Object[]} args
|
* @param {Object[]} args
|
||||||
* @returns {byteArray}
|
* @returns {byteArray}
|
||||||
*/
|
*/
|
||||||
run(input, args) {
|
async run(input, args) {
|
||||||
const type = Magic.magicFileType(input);
|
const type = Magic.magicFileType(input);
|
||||||
|
|
||||||
if (type && type.mime.indexOf("image") === 0){
|
if (type && type.mime.indexOf("image") === 0){
|
||||||
return new Promise((resolve, reject) => {
|
const image = await jimp.read(Buffer.from(input));
|
||||||
jimp.read(Buffer.from(input))
|
image.dither565();
|
||||||
.then(image => {
|
const imageBuffer = await image.getBufferAsync(jimp.AUTO);
|
||||||
image
|
return [...imageBuffer];
|
||||||
.dither565()
|
|
||||||
.getBuffer(jimp.AUTO, (error, result) => {
|
|
||||||
if (error){
|
|
||||||
reject(new OperationError("Error getting the new image buffer"));
|
|
||||||
} else {
|
|
||||||
resolve([...result]);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
})
|
|
||||||
.catch(err => {
|
|
||||||
reject(new OperationError("Error applying a dither effect to the image."));
|
|
||||||
});
|
|
||||||
});
|
|
||||||
} else {
|
} else {
|
||||||
throw new OperationError("Invalid file type.");
|
throw new OperationError("Invalid file type.");
|
||||||
}
|
}
|
||||||
|
|
|
@ -57,7 +57,7 @@ class ResizeImage extends Operation {
|
||||||
* @param {Object[]} args
|
* @param {Object[]} args
|
||||||
* @returns {byteArray}
|
* @returns {byteArray}
|
||||||
*/
|
*/
|
||||||
run(input, args) {
|
async run(input, args) {
|
||||||
let width = args[0],
|
let width = args[0],
|
||||||
height = args[1];
|
height = args[1];
|
||||||
const unit = args[2],
|
const unit = args[2],
|
||||||
|
@ -67,37 +67,20 @@ class ResizeImage extends Operation {
|
||||||
if (!type || type.mime.indexOf("image") !== 0){
|
if (!type || type.mime.indexOf("image") !== 0){
|
||||||
throw new OperationError("Invalid file type.");
|
throw new OperationError("Invalid file type.");
|
||||||
}
|
}
|
||||||
|
const image = await jimp.read(Buffer.from(input));
|
||||||
|
|
||||||
return new Promise((resolve, reject) => {
|
if (unit === "Percent") {
|
||||||
jimp.read(Buffer.from(input))
|
width = image.getWidth() * (width / 100);
|
||||||
.then(image => {
|
height = image.getHeight() * (height / 100);
|
||||||
if (unit === "Percent") {
|
}
|
||||||
width = image.getWidth() * (width / 100);
|
if (aspect) {
|
||||||
height = image.getHeight() * (height / 100);
|
image.scaleToFit(width, height);
|
||||||
}
|
} else {
|
||||||
if (aspect) {
|
image.resize(width, height);
|
||||||
image
|
}
|
||||||
.scaleToFit(width, height)
|
|
||||||
.getBuffer(jimp.AUTO, (error, result) => {
|
const imageBuffer = await image.getBufferAsync(jimp.AUTO);
|
||||||
if (error){
|
return [...imageBuffer];
|
||||||
reject(new OperationError("Error scaling the image."));
|
|
||||||
} else {
|
|
||||||
resolve([...result]);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
image
|
|
||||||
.resize(width, height)
|
|
||||||
.getBuffer(jimp.AUTO, (error, result) => {
|
|
||||||
if (error){
|
|
||||||
reject(new OperationError("Error scaling the image."));
|
|
||||||
} else {
|
|
||||||
resolve([...result]);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -42,28 +42,15 @@ class RotateImage extends Operation {
|
||||||
* @param {Object[]} args
|
* @param {Object[]} args
|
||||||
* @returns {byteArray}
|
* @returns {byteArray}
|
||||||
*/
|
*/
|
||||||
run(input, args) {
|
async run(input, args) {
|
||||||
const [degrees] = args;
|
const [degrees] = args;
|
||||||
const type = Magic.magicFileType(input);
|
const type = Magic.magicFileType(input);
|
||||||
|
|
||||||
if (type && type.mime.indexOf("image") === 0){
|
if (type && type.mime.indexOf("image") === 0){
|
||||||
return new Promise((resolve, reject) => {
|
const image = await jimp.read(Buffer.from(input));
|
||||||
jimp.read(Buffer.from(input))
|
image.rotate(degrees);
|
||||||
.then(image => {
|
const imageBuffer = await image.getBufferAsync(jimp.AUTO);
|
||||||
image
|
return [...imageBuffer];
|
||||||
.rotate(degrees)
|
|
||||||
.getBuffer(jimp.AUTO, (error, result) => {
|
|
||||||
if (error){
|
|
||||||
reject(new OperationError("Error getting the new image buffer"));
|
|
||||||
} else {
|
|
||||||
resolve([...result]);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
})
|
|
||||||
.catch(err => {
|
|
||||||
reject(new OperationError("Error reading the input image."));
|
|
||||||
});
|
|
||||||
});
|
|
||||||
} else {
|
} else {
|
||||||
throw new OperationError("Invalid file type.");
|
throw new OperationError("Invalid file type.");
|
||||||
}
|
}
|
||||||
|
@ -71,7 +58,6 @@ class RotateImage extends Operation {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Displays the rotated image using HTML for web apps
|
* Displays the rotated image using HTML for web apps
|
||||||
*
|
|
||||||
* @param {byteArray} data
|
* @param {byteArray} data
|
||||||
* @returns {html}
|
* @returns {html}
|
||||||
*/
|
*/
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue