1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-18 09:35:20 +02:00

Cli: Make Sharp package optional

This commit is contained in:
Laurent Cozic 2024-12-12 16:35:02 +01:00
parent d1ea90cd5c
commit 388d124d91
4 changed files with 29 additions and 4 deletions

View File

@ -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) {

View File

@ -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();

View File

@ -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;
}

View File

@ -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