Skip to content

Commit

Permalink
Add error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
j433866 committed Mar 7, 2019
1 parent 4a7ea46 commit 1031429
Show file tree
Hide file tree
Showing 14 changed files with 348 additions and 144 deletions.
34 changes: 21 additions & 13 deletions src/core/operations/BlurImage.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -53,21 +53,29 @@ class BlurImage extends Operation {
const type = Magic.magicFileType(input);

if (type && type.mime.indexOf("image") === 0){
const image = await jimp.read(Buffer.from(input));

switch (blurType){
case "Fast":
image.blur(blurAmount);
break;
case "Gaussian":
if (ENVIRONMENT_IS_WORKER())
self.sendStatusMessage("Gaussian blurring image. This will take a while...");
image.gaussian(blurAmount);
break;
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);
break;
case "Gaussian":
if (ENVIRONMENT_IS_WORKER())
self.sendStatusMessage("Gaussian blurring image. This will take a while...");
image.gaussian(blurAmount);
break;
}

const imageBuffer = await image.getBufferAsync(jimp.AUTO);
return [...imageBuffer];
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.");
}
Expand Down
22 changes: 15 additions & 7 deletions src/core/operations/ContainImage.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,21 @@ class ContainImage extends Operation {
throw new OperationError("Invalid file type.");
}

const image = await jimp.read(Buffer.from(input));

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];
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})`);
}
}

/**
Expand Down
21 changes: 15 additions & 6 deletions src/core/operations/CoverImage.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,21 @@ class CoverImage extends Operation {
throw new OperationError("Invalid file type.");
}

const image = await jimp.read(Buffer.from(input));
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];
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})`);
}
}

/**
Expand Down
37 changes: 23 additions & 14 deletions src/core/operations/CropImage.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -98,22 +98,31 @@ class CropImage extends Operation {
throw new OperationError("Invalid file type.");
}

const image = await jimp.read(Buffer.from(input));
if (ENVIRONMENT_IS_WORKER())
self.sendStatusMessage("Cropping image...");
if (autocrop) {
image.autocrop({
tolerance: (autoTolerance / 100),
cropOnlyFrames: autoFrames,
cropSymmetric: autoSymmetric,
leaveBorder: autoBorder
});
} else {
image.crop(xPos, yPos, width, height);
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) {
image.autocrop({
tolerance: (autoTolerance / 100),
cropOnlyFrames: autoFrames,
cropSymmetric: autoSymmetric,
leaveBorder: autoBorder
});
} else {
image.crop(xPos, yPos, width, height);
}

const imageBuffer = await image.getBufferAsync(jimp.AUTO);
return [...imageBuffer];
const imageBuffer = await image.getBufferAsync(jimp.AUTO);
return [...imageBuffer];
} catch (err) {
throw new OperationError(`Error cropping image. (${err})`);
}
}

/**
Expand Down
21 changes: 15 additions & 6 deletions src/core/operations/DitherImage.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
if (ENVIRONMENT_IS_WORKER())
self.sendStatusMessage("Applying dither to image...");
image.dither565();
const imageBuffer = await image.getBufferAsync(jimp.AUTO);
return [...imageBuffer];
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.");
}
Expand Down
34 changes: 21 additions & 13 deletions src/core/operations/FlipImage.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -49,21 +49,29 @@ class FlipImage extends Operation {
throw new OperationError("Invalid input file type.");
}

const image = await jimp.read(Buffer.from(input));

if (ENVIRONMENT_IS_WORKER())
self.sendStatusMessage("Flipping image...");
switch (flipAxis){
case "Horizontal":
image.flip(true, false);
break;
case "Vertical":
image.flip(false, true);
break;
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){
case "Horizontal":
image.flip(true, false);
break;
case "Vertical":
image.flip(false, true);
break;
}

const imageBuffer = await image.getBufferAsync(jimp.AUTO);
return [...imageBuffer];
const imageBuffer = await image.getBufferAsync(jimp.AUTO);
return [...imageBuffer];
} catch (err) {
throw new OperationError(`Error flipping image. (${err})`);
}
}

/**
Expand Down
33 changes: 21 additions & 12 deletions src/core/operations/ImageBrightnessContrast.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -58,20 +58,29 @@ class ImageBrightnessContrast extends Operation {
throw new OperationError("Invalid file type.");
}

const image = await jimp.read(Buffer.from(input));
if (brightness !== 0) {
if (ENVIRONMENT_IS_WORKER())
self.sendStatusMessage("Changing image brightness...");
image.brightness(brightness / 100);
}
if (contrast !== 0) {
if (ENVIRONMENT_IS_WORKER())
self.sendStatusMessage("Changing image contrast...");
image.contrast(contrast / 100);
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...");
image.brightness(brightness / 100);
}
if (contrast !== 0) {
if (ENVIRONMENT_IS_WORKER())
self.sendStatusMessage("Changing image contrast...");
image.contrast(contrast / 100);
}

const imageBuffer = await image.getBufferAsync(jimp.AUTO);
return [...imageBuffer];
const imageBuffer = await image.getBufferAsync(jimp.AUTO);
return [...imageBuffer];
} catch (err) {
throw new OperationError(`Error adjusting image brightness / contrast. (${err})`);
}
}

/**
Expand Down
27 changes: 18 additions & 9 deletions src/core/operations/ImageFilter.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,26 @@ class ImageFilter extends Operation {
throw new OperationError("Invalid file type.");
}

const image = await jimp.read(Buffer.from(input));
if (ENVIRONMENT_IS_WORKER())
self.sendStatusMessage("Applying " + filterType.toLowerCase() + " filter to image...");
if (filterType === "Greyscale") {
image.greyscale();
} else {
image.sepia();
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") {
image.greyscale();
} else {
image.sepia();
}

const imageBuffer = await image.getBufferAsync(jimp.AUTO);
return [...imageBuffer];
const imageBuffer = await image.getBufferAsync(jimp.AUTO);
return [...imageBuffer];
} catch (err) {
throw new OperationError(`Error applying filter to image. (${err})`);
}
}

/**
Expand Down
72 changes: 40 additions & 32 deletions src/core/operations/ImageHueSaturationLightness.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -66,40 +66,48 @@ class ImageHueSaturationLightness extends Operation {
throw new OperationError("Invalid file type.");
}

const image = await jimp.read(Buffer.from(input));

if (hue !== 0) {
if (ENVIRONMENT_IS_WORKER())
self.sendStatusMessage("Changing image hue...");
image.colour([
{
apply: "hue",
params: [hue]
}
]);
}
if (saturation !== 0) {
if (ENVIRONMENT_IS_WORKER())
self.sendStatusMessage("Changing image saturation...");
image.colour([
{
apply: "saturate",
params: [saturation]
}
]);
let image;
try {
image = await jimp.read(Buffer.from(input));
} catch (err) {
throw new OperationError(`Error loading image. (${err})`);
}
if (lightness !== 0) {
if (ENVIRONMENT_IS_WORKER())
self.sendStatusMessage("Changing image lightness...");
image.colour([
{
apply: "lighten",
params: [lightness]
}
]);
try {
if (hue !== 0) {
if (ENVIRONMENT_IS_WORKER())
self.sendStatusMessage("Changing image hue...");
image.colour([
{
apply: "hue",
params: [hue]
}
]);
}
if (saturation !== 0) {
if (ENVIRONMENT_IS_WORKER())
self.sendStatusMessage("Changing image saturation...");
image.colour([
{
apply: "saturate",
params: [saturation]
}
]);
}
if (lightness !== 0) {
if (ENVIRONMENT_IS_WORKER())
self.sendStatusMessage("Changing image lightness...");
image.colour([
{
apply: "lighten",
params: [lightness]
}
]);
}
const imageBuffer = await image.getBufferAsync(jimp.AUTO);
return [...imageBuffer];
} catch (err) {
throw new OperationError(`Error adjusting image hue / saturation / lightness. (${err})`);
}
const imageBuffer = await image.getBufferAsync(jimp.AUTO);
return [...imageBuffer];
}

/**
Expand Down
Loading

0 comments on commit 1031429

Please sign in to comment.