Skip to content

TagSpaces is an offline, open source, document manager with tagging support

chore: optimizing tmb generation

Ilian Sapundshiev6075eb82c16a
diff --git a/app/services/thumbsgenerator.ts b/app/services/thumbsgenerator.ts
index b1ffa2c42..be4afb2f8 100644
--- a/app/services/thumbsgenerator.ts
+++ b/app/services/thumbsgenerator.ts
@@ -31,7 +31,7 @@ import PlatformIO from '../services/platform-facade';
import { Pro } from '../pro';
let maxSize = AppConfig.maxThumbSize;
-const bgColor = AppConfig.thumbBgColor;
+const thumbnailBackgroundColor = AppConfig.thumbBgColor;
export const supportedMisc = ['url', 'html'];
export const supportedImgs = [
@@ -294,6 +294,8 @@ export function generateThumbnailPromise(fileURL: string, fileSize: number) {
return Pro.ThumbsGenerator.generateUTIFThumbnail(fileURLEscaped, maxSize);
} else if (Pro && ext === 'cr2') {
return Pro.ThumbsGenerator.generateUTIFThumbnail(fileURLEscaped, maxSize);
+ } else if (Pro && ext === 'psd') {
+ return Pro.ThumbsGenerator.generatePSDThumbnail(fileURLEscaped, maxSize);
} else if (fileSize && fileSize < maxFileSize) {
return generateImageThumbnail(fileURLEscaped);
}
@@ -338,135 +340,132 @@ export function generateImageThumbnail(
maxTmbSize?: number
): Promise<string> {
return new Promise(resolve => {
- let canvas: HTMLCanvasElement = document.createElement('canvas');
- const ctx = canvas.getContext('2d');
- let img: HTMLImageElement = new Image();
-
- if (maxTmbSize && maxTmbSize > maxSize) {
- maxSize = maxTmbSize;
- }
-
- const errorHandler = err => {
- console.warn(
- 'Error while generating thumbnail for: ' +
- fileURL +
- ' - ' +
- JSON.stringify(err)
- );
- resolve('');
- };
-
- img.crossOrigin = 'anonymous';
- img.onload = () => {
- // EXIF extraction not need because the image are rotated
- // automatically in Chrome version 81 and higher
- // EXIF.getData(img as any, function() {
- // // TODO Use EXIF only for jpegs
- // const orientation = EXIF.getTag(this, 'Orientation');
- // /*
- // 1 - 0 degrees – the correct orientation, no adjustment is required.
- // 2 - 0 degrees, mirrored – image has been flipped back-to-front.
- // 3 - 180 degrees – image is upside down.
- // 4 - 180 degrees, mirrored – image is upside down and flipped back-to-front.
- // 5 - 90 degrees – image is on its side.
- // 6 - 90 degrees, mirrored – image is on its side and flipped back-to-front.
- // 7 - 270 degrees – image is on its far side.
- // 8 - 270 degrees, mirrored – image is on its far side and flipped back-to-front.
- // */
- // let angleInRadians;
- // switch (orientation) {
- // case 8:
- // angleInRadians = 270 * (Math.PI / 180);
- // break;
- // case 3:
- // angleInRadians = 180 * (Math.PI / 180);
- // break;
- // case 6:
- // angleInRadians = 90 * (Math.PI / 180);
- // break;
- // case 1:
- // // ctx.rotate(0);
- // break;
- // default:
- // // ctx.rotate(0);
- // }
- let maxSizeWidth = maxSize;
- if (img.width < maxSize) {
- maxSizeWidth = img.width;
- }
- let maxSizeHeight = maxSize;
- if (img.height < maxSize) {
- maxSizeHeight = img.height;
- }
- if (img.width >= img.height) {
- canvas.width = maxSizeWidth;
- canvas.height = (maxSizeWidth * img.height) / img.width;
- } else {
- canvas.height = maxSizeHeight;
- canvas.width = (maxSizeHeight * img.width) / img.height;
+ try {
+ let canvas: HTMLCanvasElement = document.createElement('canvas');
+ const ctx = canvas.getContext('2d');
+ let img: HTMLImageElement = new Image();
+ if (maxTmbSize && maxTmbSize > maxSize) {
+ maxSize = maxTmbSize;
}
+ img.crossOrigin = 'anonymous';
+ img.onload = () => {
+ // EXIF extraction not need because the image are rotated
+ // automatically in Chrome version 81 and higher
+ // EXIF.getData(img as any, function() {
+ // // TODO Use EXIF only for jpegs
+ // const orientation = EXIF.getTag(this, 'Orientation');
+ // /*
+ // 1 - 0 degrees – the correct orientation, no adjustment is required.
+ // 2 - 0 degrees, mirrored – image has been flipped back-to-front.
+ // 3 - 180 degrees – image is upside down.
+ // 4 - 180 degrees, mirrored – image is upside down and flipped back-to-front.
+ // 5 - 90 degrees – image is on its side.
+ // 6 - 90 degrees, mirrored – image is on its side and flipped back-to-front.
+ // 7 - 270 degrees – image is on its far side.
+ // 8 - 270 degrees, mirrored – image is on its far side and flipped back-to-front.
+ // */
+ // let angleInRadians;
+ // switch (orientation) {
+ // case 8:
+ // angleInRadians = 270 * (Math.PI / 180);
+ // break;
+ // case 3:
+ // angleInRadians = 180 * (Math.PI / 180);
+ // break;
+ // case 6:
+ // angleInRadians = 90 * (Math.PI / 180);
+ // break;
+ // case 1:
+ // // ctx.rotate(0);
+ // break;
+ // default:
+ // // ctx.rotate(0);
+ // }
+ let maxSizeWidth = maxSize;
+ if (img.width < maxSize) {
+ maxSizeWidth = img.width;
+ }
+ let maxSizeHeight = maxSize;
+ if (img.height < maxSize) {
+ maxSizeHeight = img.height;
+ }
+ if (img.width >= img.height) {
+ canvas.width = maxSizeWidth;
+ canvas.height = (maxSizeWidth * img.height) / img.width;
+ } else {
+ canvas.height = maxSizeHeight;
+ canvas.width = (maxSizeHeight * img.width) / img.height;
+ }
- const { width, height } = canvas;
- const x = canvas.width / 2;
- const y = canvas.height / 2;
+ const { width, height } = canvas;
+ const x = canvas.width / 2;
+ const y = canvas.height / 2;
- ctx.translate(x, y);
- // ctx.rotate(angleInRadians);
- ctx.fillStyle = bgColor;
- ctx.fillRect(-width / 2, -height / 2, width, height);
- ctx.drawImage(img, -width / 2, -height / 2, width, height);
- // ctx.rotate(-angleInRadians);
- ctx.translate(-x, -y);
- const dataurl = canvas.toDataURL(AppConfig.thumbType);
- resolve(dataurl);
- img = null;
- canvas = null;
- // });
- };
- img.src = fileURL;
- img.onerror = errorHandler;
+ ctx.translate(x, y);
+ // ctx.rotate(angleInRadians);
+ ctx.fillStyle = thumbnailBackgroundColor;
+ ctx.fillRect(-width / 2, -height / 2, width, height);
+ ctx.drawImage(img, -width / 2, -height / 2, width, height);
+ // ctx.rotate(-angleInRadians);
+ ctx.translate(-x, -y);
+ const dataurl = canvas.toDataURL(AppConfig.thumbType);
+ resolve(dataurl);
+ img = null;
+ canvas = null;
+ // });
+ };
+ img.src = fileURL;
+ img.onerror = err => {
+ console.warn(`Error loading: ${fileURL} for tmb gen with: ${err} `);
+ resolve('');
+ };
+ } catch (e) {
+ console.warn(`Error creating image thumb for : ${fileURL} with: ${e}`);
+ resolve('');
+ }
});
}
function generateVideoThumbnail(fileURL): Promise<string> {
return new Promise(resolve => {
- let canvas: HTMLCanvasElement = document.createElement('canvas');
- const ctx = canvas.getContext('2d');
- let img: HTMLImageElement = new Image();
- let video: HTMLVideoElement = document.createElement('video');
- const captureTime = 1.5; // time in seconds at which to capture the image from the video
+ try {
+ let canvas: HTMLCanvasElement = document.createElement('canvas');
+ const ctx = canvas.getContext('2d');
+ let img: HTMLImageElement = new Image();
+ let video: HTMLVideoElement = document.createElement('video');
+ const captureTime = 1.5; // time in seconds at which to capture the image from the video
- const errorHandler = err => {
- console.warn(
- 'Error while generating thumbnail for: ' +
- fileURL +
- ' - ' +
- JSON.stringify(err)
- );
- resolve('');
- };
+ video.onloadedmetadata = () => {
+ video.currentTime = Math.min(Math.max(0, captureTime), video.duration);
+ if (video.videoWidth >= video.videoHeight) {
+ canvas.width = maxSize;
+ canvas.height = (maxSize * video.videoHeight) / video.videoWidth;
+ } else {
+ canvas.height = maxSize;
+ canvas.width = (maxSize * video.videoWidth) / video.videoHeight;
+ }
+ };
- video.onloadedmetadata = () => {
- video.currentTime = Math.min(Math.max(0, captureTime), video.duration);
- if (video.videoWidth >= video.videoHeight) {
- canvas.width = maxSize;
- canvas.height = (maxSize * video.videoHeight) / video.videoWidth;
- } else {
- canvas.height = maxSize;
- canvas.width = (maxSize * video.videoWidth) / video.videoHeight;
- }
- };
-
- video.onseeked = () => {
- ctx.drawImage(video, 0, 0, canvas.width, canvas.height);
- const dataurl = canvas.toDataURL(AppConfig.thumbType);
- img.onerror = errorHandler;
- resolve(dataurl);
- img = null;
- canvas = null;
- video = null;
- };
- video.onerror = errorHandler;
- video.src = fileURL;
+ video.onseeked = () => {
+ ctx.drawImage(video, 0, 0, canvas.width, canvas.height);
+ const dataurl = canvas.toDataURL(AppConfig.thumbType);
+ img.onerror = err => {
+ console.warn(`Error loading: ${fileURL} for tmb gen with: ${err} `);
+ resolve('');
+ };
+ resolve(dataurl);
+ img = null;
+ canvas = null;
+ video = null;
+ };
+ video.onerror = err => {
+ console.warn(`Error opening: ${fileURL} for tmb gen with: ${err} `);
+ resolve('');
+ };
+ video.src = fileURL;
+ } catch (e) {
+ console.warn(`Error creating video thumb for : ${fileURL} with: ${e}`);
+ resolve('');
+ }
});
}