Add error handling

This commit is contained in:
j433866 2019-03-07 11:19:04 +00:00
parent 4a7ea469d4
commit 1031429550
14 changed files with 348 additions and 144 deletions

View File

@ -53,8 +53,13 @@ class BlurImage extends Operation {
const type = Magic.magicFileType(input);
if (type && type.mime.indexOf("image") === 0){
const image = await jimp.read(Buffer.from(input));
let image;
try {
image = await jimp.read(Buffer.from(input));
} catch (err) {
throw new OperationError(`Error loading image. (${err})`);
}
try {
switch (blurType){
case "Fast":
image.blur(blurAmount);
@ -68,6 +73,9 @@ class BlurImage extends Operation {
const imageBuffer = await image.getBufferAsync(jimp.AUTO);
return [...imageBuffer];
} catch (err) {
throw new OperationError(`Error blurring image. (${err})`);
}
} else {
throw new OperationError("Invalid file type.");
}

View File

@ -106,13 +106,21 @@ class ContainImage extends Operation {
throw new OperationError("Invalid file type.");
}
const image = await jimp.read(Buffer.from(input));
let image;
try {
image = await jimp.read(Buffer.from(input));
} catch (err) {
throw new OperationError(`Error loading image. (${err})`);
}
try {
if (ENVIRONMENT_IS_WORKER())
self.sendStatusMessage("Containing image...");
image.contain(width, height, alignMap[hAlign] | alignMap[vAlign], resizeMap[alg]);
const imageBuffer = await image.getBufferAsync(jimp.AUTO);
return [...imageBuffer];
} catch (err) {
throw new OperationError(`Error containing image. (${err})`);
}
}
/**

View File

@ -106,12 +106,21 @@ class CoverImage extends Operation {
throw new OperationError("Invalid file type.");
}
const image = await jimp.read(Buffer.from(input));
let image;
try {
image = await jimp.read(Buffer.from(input));
} catch (err) {
throw new OperationError(`Error loading image. (${err})`);
}
try {
if (ENVIRONMENT_IS_WORKER())
self.sendStatusMessage("Covering image...");
image.cover(width, height, alignMap[hAlign] | alignMap[vAlign], resizeMap[alg]);
const imageBuffer = await image.getBufferAsync(jimp.AUTO);
return [...imageBuffer];
} catch (err) {
throw new OperationError(`Error covering image. (${err})`);
}
}
/**

View File

@ -98,7 +98,13 @@ class CropImage extends Operation {
throw new OperationError("Invalid file type.");
}
const image = await jimp.read(Buffer.from(input));
let image;
try {
image = await jimp.read(Buffer.from(input));
} catch (err) {
throw new OperationError(`Error loading image. (${err})`);
}
try {
if (ENVIRONMENT_IS_WORKER())
self.sendStatusMessage("Cropping image...");
if (autocrop) {
@ -114,6 +120,9 @@ class CropImage extends Operation {
const imageBuffer = await image.getBufferAsync(jimp.AUTO);
return [...imageBuffer];
} catch (err) {
throw new OperationError(`Error cropping image. (${err})`);
}
}
/**

View File

@ -40,12 +40,21 @@ class DitherImage extends Operation {
const type = Magic.magicFileType(input);
if (type && type.mime.indexOf("image") === 0){
const image = await jimp.read(Buffer.from(input));
let image;
try {
image = await jimp.read(Buffer.from(input));
} catch (err) {
throw new OperationError(`Error loading image. (${err})`);
}
try {
if (ENVIRONMENT_IS_WORKER())
self.sendStatusMessage("Applying dither to image...");
image.dither565();
const imageBuffer = await image.getBufferAsync(jimp.AUTO);
return [...imageBuffer];
} catch (err) {
throw new OperationError(`Error applying dither to image. (${err})`);
}
} else {
throw new OperationError("Invalid file type.");
}

View File

@ -49,8 +49,13 @@ class FlipImage extends Operation {
throw new OperationError("Invalid input file type.");
}
const image = await jimp.read(Buffer.from(input));
let image;
try {
image = await jimp.read(Buffer.from(input));
} catch (err) {
throw new OperationError(`Error loading image. (${err})`);
}
try {
if (ENVIRONMENT_IS_WORKER())
self.sendStatusMessage("Flipping image...");
switch (flipAxis){
@ -64,6 +69,9 @@ class FlipImage extends Operation {
const imageBuffer = await image.getBufferAsync(jimp.AUTO);
return [...imageBuffer];
} catch (err) {
throw new OperationError(`Error flipping image. (${err})`);
}
}
/**

View File

@ -58,7 +58,13 @@ class ImageBrightnessContrast extends Operation {
throw new OperationError("Invalid file type.");
}
const image = await jimp.read(Buffer.from(input));
let image;
try {
image = await jimp.read(Buffer.from(input));
} catch (err) {
throw new OperationError(`Error loading image. (${err})`);
}
try {
if (brightness !== 0) {
if (ENVIRONMENT_IS_WORKER())
self.sendStatusMessage("Changing image brightness...");
@ -72,6 +78,9 @@ class ImageBrightnessContrast extends Operation {
const imageBuffer = await image.getBufferAsync(jimp.AUTO);
return [...imageBuffer];
} catch (err) {
throw new OperationError(`Error adjusting image brightness / contrast. (${err})`);
}
}
/**

View File

@ -52,7 +52,13 @@ class ImageFilter extends Operation {
throw new OperationError("Invalid file type.");
}
const image = await jimp.read(Buffer.from(input));
let image;
try {
image = await jimp.read(Buffer.from(input));
} catch (err) {
throw new OperationError(`Error loading image. (${err})`);
}
try {
if (ENVIRONMENT_IS_WORKER())
self.sendStatusMessage("Applying " + filterType.toLowerCase() + " filter to image...");
if (filterType === "Greyscale") {
@ -63,6 +69,9 @@ class ImageFilter extends Operation {
const imageBuffer = await image.getBufferAsync(jimp.AUTO);
return [...imageBuffer];
} catch (err) {
throw new OperationError(`Error applying filter to image. (${err})`);
}
}
/**

View File

@ -66,8 +66,13 @@ class ImageHueSaturationLightness extends Operation {
throw new OperationError("Invalid file type.");
}
const image = await jimp.read(Buffer.from(input));
let image;
try {
image = await jimp.read(Buffer.from(input));
} catch (err) {
throw new OperationError(`Error loading image. (${err})`);
}
try {
if (hue !== 0) {
if (ENVIRONMENT_IS_WORKER())
self.sendStatusMessage("Changing image hue...");
@ -100,6 +105,9 @@ class ImageHueSaturationLightness extends Operation {
}
const imageBuffer = await image.getBufferAsync(jimp.AUTO);
return [...imageBuffer];
} catch (err) {
throw new OperationError(`Error adjusting image hue / saturation / lightness. (${err})`);
}
}
/**

View File

@ -51,13 +51,22 @@ class ImageOpacity extends Operation {
throw new OperationError("Invalid file type.");
}
const image = await jimp.read(Buffer.from(input));
let image;
try {
image = await jimp.read(Buffer.from(input));
} catch (err) {
throw new OperationError(`Error loading image. (${err})`);
}
try {
if (ENVIRONMENT_IS_WORKER())
self.sendStatusMessage("Changing image opacity...");
image.opacity(opacity / 100);
const imageBuffer = await image.getBufferAsync(jimp.MIME_PNG);
return [...imageBuffer];
} catch (err) {
throw new OperateionError(`Error changing image opacity. (${err})`);
}
}
/**

View File

@ -41,12 +41,22 @@ class InvertImage extends Operation {
if (!type || type.mime.indexOf("image") !== 0) {
throw new OperationError("Invalid input file format.");
}
const image = await jimp.read(Buffer.from(input));
let image;
try {
image = await jimp.read(Buffer.from(input));
} catch (err) {
throw new OperationError(`Error loading image. (${err})`);
}
try {
if (ENVIRONMENT_IS_WORKER())
self.sendStatusMessage("Inverting image...");
image.invert();
const imageBuffer = await image.getBufferAsync(jimp.AUTO);
return [...imageBuffer];
} catch (err) {
throw new OperationError(`Error inverting image. (${err})`);
}
}
/**

View File

@ -0,0 +1,91 @@
/**
* @author j433866 [j433866@gmail.com]
* @copyright Crown Copyright 2019
* @license Apache-2.0
*/
import Operation from "../Operation";
import OperationError from "../errors/OperationError";
import Magic from "../lib/Magic";
import { toBase64 } from "../lib/Base64";
import jimp from "jimp";
/**
* Normalise Image operation
*/
class NormaliseImage extends Operation {
/**
* NormaliseImage constructor
*/
constructor() {
super();
this.name = "Normalise Image";
this.module = "Image";
this.description = "Normalise the image colours.";
this.infoURL = "";
this.inputType = "byteArray";
this.outputType = "byteArray";
this.presentType= "html";
this.args = [
/* Example arguments. See the project wiki for full details.
{
name: "First arg",
type: "string",
value: "Don't Panic"
},
{
name: "Second arg",
type: "number",
value: 42
}
*/
];
}
/**
* @param {byteArray} input
* @param {Object[]} args
* @returns {byteArray}
*/
async run(input, args) {
// const [firstArg, secondArg] = args;
const type = Magic.magicFileType(input);
if (!type || type.mime.indexOf("image") !== 0){
throw new OperationError("Invalid file type.");
}
const image = await jimp.read(Buffer.from(input));
image.normalize();
const imageBuffer = await image.getBufferAsync(jimp.AUTO);
return [...imageBuffer];
}
/**
* Displays the normalised image using HTML for web apps
* @param {byteArray} data
* @returns {html}
*/
present(data) {
if (!data.length) return "";
let dataURI = "data:";
const type = Magic.magicFileType(data);
if (type && type.mime.indexOf("image") === 0){
dataURI += type.mime + ";";
} else {
throw new OperationError("Invalid file type.");
}
dataURI += "base64," + toBase64(data);
return "<img src='" + dataURI + "'>";
}
}
export default NormaliseImage;

View File

@ -91,8 +91,13 @@ class ResizeImage extends Operation {
throw new OperationError("Invalid file type.");
}
const image = await jimp.read(Buffer.from(input));
let image;
try {
image = await jimp.read(Buffer.from(input));
} catch (err) {
throw new OperationError(`Error loading image. (${err})`);
}
try {
if (unit === "Percent") {
width = image.getWidth() * (width / 100);
height = image.getHeight() * (height / 100);
@ -108,6 +113,9 @@ class ResizeImage extends Operation {
const imageBuffer = await image.getBufferAsync(jimp.AUTO);
return [...imageBuffer];
} catch (err) {
throw new OperationError(`Error resizing image. (${err})`);
}
}
/**

View File

@ -47,12 +47,21 @@ class RotateImage extends Operation {
const type = Magic.magicFileType(input);
if (type && type.mime.indexOf("image") === 0){
const image = await jimp.read(Buffer.from(input));
let image;
try {
image = await jimp.read(Buffer.from(input));
} catch (err) {
throw new OperationError(`Error loading image. (${err})`);
}
try {
if (ENVIRONMENT_IS_WORKER())
self.sendStatusMessage("Rotating image...");
image.rotate(degrees);
const imageBuffer = await image.getBufferAsync(jimp.AUTO);
return [...imageBuffer];
} catch (err) {
throw new OperationError(`Error rotating image. (${err})`);
}
} else {
throw new OperationError("Invalid file type.");
}