diff --git a/plugins/snapshot/src/image-reader.ts b/plugins/snapshot/src/image-reader.ts index 42999f9089..12700b4d60 100644 --- a/plugins/snapshot/src/image-reader.ts +++ b/plugins/snapshot/src/image-reader.ts @@ -1,13 +1,20 @@ import sdk, { BufferConverter, Image, ImageOptions, MediaObject, MediaObjectOptions, ScryptedDeviceBase, ScryptedMimeTypes } from "@scrypted/sdk"; import type sharp from 'sharp'; -export let sharpInstance: typeof sharp; -try { - sharpInstance = require('sharp'); - console.log('sharp loaded'); -} -catch (e) { - console.warn('sharp failed to load, scrypted server may be out of date', e); +let hasLoadedSharp = false; +let sharpInstance: typeof sharp; +export function loadSharp() { + if (!hasLoadedSharp) { + hasLoadedSharp = true; + try { + sharpInstance = require('sharp'); + console.log('sharp loaded'); + } + catch (e) { + console.warn('sharp failed to load, scrypted server may be out of date', e); + } + } + return !!sharpInstance; } export const ImageReaderNativeId = 'imagereader'; @@ -98,7 +105,7 @@ export class VipsImage implements Image { } } -export async function loadVipsImage(data: Buffer|string, sourceId: string) { +export async function loadVipsImage(data: Buffer | string, sourceId: string) { const image = sharpInstance(data, { failOnError: false, }); diff --git a/plugins/snapshot/src/main.ts b/plugins/snapshot/src/main.ts index a7f0f59950..2bde358158 100644 --- a/plugins/snapshot/src/main.ts +++ b/plugins/snapshot/src/main.ts @@ -9,7 +9,7 @@ import https from 'https'; import path from 'path'; import MimeType from 'whatwg-mimetype'; import { ffmpegFilterImage, ffmpegFilterImageBuffer } from './ffmpeg-image-filter'; -import { ImageReader, ImageReaderNativeId, loadVipsImage, sharpInstance } from './image-reader'; +import { ImageReader, ImageReaderNativeId, loadVipsImage, loadSharp } from './image-reader'; import { ImageWriter, ImageWriterNativeId } from './image-writer'; const { mediaManager, systemManager } = sdk; @@ -303,7 +303,7 @@ class SnapshotMixin extends SettingsMixinDeviceBase implements Camera { }, async () => { this.debugConsole?.log("Resizing picture from camera", options?.picture); - if (sharpInstance) { + if (loadSharp()) { const vips = await loadVipsImage(picture, this.id); try { const ret = await vips.toBuffer({ @@ -367,7 +367,7 @@ class SnapshotMixin extends SettingsMixinDeviceBase implements Camera { const xmax = Math.max(...this.storageSettings.values.snapshotCropScale.map(([x, y]) => x)) / 100; const ymax = Math.max(...this.storageSettings.values.snapshotCropScale.map(([x, y]) => y)) / 100; - if (sharpInstance) { + if (loadSharp()) { const vips = await loadVipsImage(picture, this.id); try { const ret = await vips.toBuffer({ @@ -585,7 +585,7 @@ class SnapshotPlugin extends AutoenableMixinProvider implements MixinProvider, B ] }; - if (sharpInstance) { + if (loadSharp()) { manifest.devices.push( { name: 'Image Reader', @@ -654,7 +654,7 @@ class SnapshotPlugin extends AutoenableMixinProvider implements MixinProvider, B }); const filename = ffmpegInput.url?.startsWith('file:') && new URL(ffmpegInput.url).pathname; - if (filename && sharpInstance) { + if (filename && loadSharp()) { const vips = await loadVipsImage(filename, options?.sourceId); const resize = width && {