diff --git a/packages/app-cli/app/app.ts b/packages/app-cli/app/app.ts index 8b3835bdc..8c345b519 100644 --- a/packages/app-cli/app/app.ts +++ b/packages/app-cli/app/app.ts @@ -408,6 +408,8 @@ class Application extends BaseApplication { this.initRedux(); + if (!shim.sharpEnabled()) this.logger().warn('Sharp is disabled - certain image-related features will not be available'); + // If we have some arguments left at this point, it's a command // so execute it. if (argv.length) { diff --git a/packages/app-cli/app/main.js b/packages/app-cli/app/main.js index a1cc0b983..2b4f38d6f 100644 --- a/packages/app-cli/app/main.js +++ b/packages/app-cli/app/main.js @@ -22,7 +22,6 @@ const Setting = require('@joplin/lib/models/Setting').default; const Revision = require('@joplin/lib/models/Revision').default; const Logger = require('@joplin/utils/Logger').default; const FsDriverNode = require('@joplin/lib/fs-driver-node').default; -const sharp = require('sharp'); const { shimInit } = require('@joplin/lib/shim-init-node.js'); const shim = require('@joplin/lib/shim').default; const { _ } = require('@joplin/lib/locale'); @@ -32,6 +31,14 @@ const envFromArgs = require('@joplin/lib/envFromArgs'); const nodeSqlite = require('sqlite3'); const initLib = require('@joplin/lib/initLib').default; +let sharp = null; +try { + sharp = require('sharp'); +} catch (error) { + // Don't print an error or it will pollute stdout every time the app is started. A warning will + // be printed in app.ts +} + const env = envFromArgs(process.argv); const fsDriver = new FsDriverNode(); diff --git a/packages/lib/shim-init-node.ts b/packages/lib/shim-init-node.ts index 3e77dc276..2c0dec44a 100644 --- a/packages/lib/shim-init-node.ts +++ b/packages/lib/shim-init-node.ts @@ -18,6 +18,7 @@ import crypto from './services/e2ee/crypto'; import FileApiDriverLocal from './file-api-driver-local'; import * as mimeUtils from './mime-utils'; import BaseItem from './models/BaseItem'; +import { Size } from '@joplin/utils/types'; const { _ } = require('./locale'); const http = require('http'); const https = require('https'); @@ -146,6 +147,10 @@ function shimInit(options: ShimInitOptions = null) { return shim.fsDriver_; }; + shim.sharpEnabled = () => { + return !!sharp; + }; + shim.dgram = () => { return dgram; }; @@ -270,10 +275,17 @@ function shimInit(options: ShimInitOptions = null) { return await saveOriginalImage(); } else { // For the CLI tool - const image = sharp(filePath); - const md = await image.metadata(); - if (md.width <= maxDim && md.height <= maxDim) { + let md: Size = null; + // eslint-disable-next-line @typescript-eslint/no-explicit-any + let image: any = null; + + if (sharp) { + image = sharp(filePath); + md = await image.metadata(); + } + + if (!md || (md.width <= maxDim && md.height <= maxDim)) { await shim.fsDriver().copy(filePath, targetPath); return true; } diff --git a/packages/lib/shim.ts b/packages/lib/shim.ts index b835674a9..68d690a4b 100644 --- a/packages/lib/shim.ts +++ b/packages/lib/shim.ts @@ -299,6 +299,10 @@ const shim = { throw new Error('Not implemented: fsDriver'); }, + sharpEnabled: (): boolean => { + return true; + }, + FileApiDriverLocal: null as typeof FileApiDriverLocal, // eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied