Skip to content

Commit

Permalink
snapshot: lazy load sharp
Browse files Browse the repository at this point in the history
  • Loading branch information
koush committed Nov 22, 2023
1 parent fb82567 commit 30891e0
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 13 deletions.
23 changes: 15 additions & 8 deletions plugins/snapshot/src/image-reader.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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,
});
Expand Down
10 changes: 5 additions & 5 deletions plugins/snapshot/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -303,7 +303,7 @@ class SnapshotMixin extends SettingsMixinDeviceBase<Camera> 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({
Expand Down Expand Up @@ -367,7 +367,7 @@ class SnapshotMixin extends SettingsMixinDeviceBase<Camera> 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({
Expand Down Expand Up @@ -585,7 +585,7 @@ class SnapshotPlugin extends AutoenableMixinProvider implements MixinProvider, B
]
};

if (sharpInstance) {
if (loadSharp()) {
manifest.devices.push(
{
name: 'Image Reader',
Expand Down Expand Up @@ -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 && {
Expand Down

0 comments on commit 30891e0

Please sign in to comment.