Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolve invalid option -- 'o' #497

Merged
merged 1 commit into from
Dec 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 9 additions & 14 deletions packages/server/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"loglevel": "^1.7.0",
"loglevel-plugin-prefix": "^0.8.4",
"mv": "^2.1.1",
"semver": "^7.3.8",
"swagger-jsdoc": "^6.2.5",
"swagger-ui-express": "^4.6.0"
},
Expand Down
11 changes: 11 additions & 0 deletions packages/server/src/process.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
const util = require('util');
const log = require('loglevel').getLogger('Process');
const exec = util.promisify(require('child_process').exec);
const execSync = require('child_process').execSync;
const spawn = require('child_process').spawn;
const extend = require('./util').extend;

const Process = {

/**
* @param {string} cmd
* @returns {string}
*/
executeSync(cmd, options) {
const stdout = execSync(cmd, options);
return Buffer.from(stdout).toString().trim();
},

/**
* @param {string} cmd
* @returns {Promise.<string>}
Expand Down
45 changes: 35 additions & 10 deletions packages/server/src/scanimage.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,32 @@ const CmdBuilder = require('./command-builder');
/** @type {Configuration} */
const Config = require('./config');
const Constants = require('./constants');
const Process = require('./process');
const semver = require('semver');

class Scanimage {
get version() {
if (this._version === undefined) {
const result = Process.executeSync(`${Config.scanimage} -V`);
this._version = /.*backend version (.*)/.exec(result)[1];
}
return this._version;
}

get supportsOutputFlag() {
return semver.satisfies(this.version, '>=1.0.28');
}
}

class ScanimageCommand {
constructor() {
this.scanimage = new Scanimage();
}

/**
* @returns {string}
*/
static devices() {
devices() {
return new CmdBuilder(Config.scanimage)
.arg('-L')
.build();
Expand All @@ -19,7 +39,7 @@ class Scanimage {
* @param {string} deviceId
* @returns {string}
*/
static features(deviceId) {
features(deviceId) {
return new CmdBuilder(Config.scanimage)
.arg('-d', deviceId)
.arg('-A')
Expand All @@ -30,7 +50,7 @@ class Scanimage {
* @param {number} page
* @returns {string}
*/
static filename(page) {
filename(page) {
const number = `000${page}`.slice(-4);
return `${Config.tempDirectory}/${Constants.TEMP_FILESTEM}-0-${number}.tif`;
}
Expand All @@ -39,7 +59,7 @@ class Scanimage {
* @param {ScanRequest} request
* @returns {string}
*/
static scan(request) {
scan(request) {
log.debug(JSON.stringify(request));
const params = request.params;
const cmdBuilder = new CmdBuilder(Config.scanimage);
Expand Down Expand Up @@ -83,19 +103,24 @@ class Scanimage {
if (params.mode === 'Lineart' && params.dynamicLineart === false) {
cmdBuilder.arg('--disable-dynamic-lineart=yes');
}
if ([Constants.BATCH_AUTO, Constants.BATCH_COLLATE_STANDARD, Constants.BATCH_COLLATE_REVERSE]
.includes(request.batch)) {
if ([Constants.BATCH_AUTO, Constants.BATCH_COLLATE_STANDARD, Constants.BATCH_COLLATE_REVERSE].includes(request.batch)) {
const pattern = `${Config.tempDirectory}/${Constants.TEMP_FILESTEM}-${request.index}-%04d.tif`;
cmdBuilder.arg(`--batch=${pattern}`);
} else {
if ('isPreview' in params && params.isPreview) {
cmdBuilder.arg(`-o ${Config.previewDirectory}/preview.tif`);
const outputFile = 'isPreview' in params && params.isPreview
? `${Config.previewDirectory}/preview.tif`
: this.filename(request.index);

if (this.scanimage.supportsOutputFlag) {
cmdBuilder.arg('-o', outputFile);
} else {
cmdBuilder.arg(`-o ${Scanimage.filename(request.index)}`);
cmdBuilder.arg(`> '${outputFile}'`);
}
}
return cmdBuilder.build();
}
}

module.exports = Scanimage;
const scanimageCommand = new ScanimageCommand();

module.exports = scanimageCommand;
9 changes: 9 additions & 0 deletions packages/server/test/config.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/* eslint-env mocha */
const assert = require('assert');
const Config = require('../src/config');

describe('Config', () => {
it.skip('scanimageVersion', () => {
assert.strictEqual(Config.scanimageVersion, '1.0.31');
});
});
61 changes: 61 additions & 0 deletions packages/server/test/scanimage-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/* eslint-env mocha */
const assert = require('assert');
const Scanimage = require('../src/scanimage');

const requestScan = {
params: {
deviceId: 'deviceId',
resolution: '150',
format: 'TIF',
isPreview: false
}
};

const requestPreview = {
params: {
deviceId: 'deviceId',
resolution: '150',
format: 'TIF',
isPreview: true
}
};

function commandFor(version, request) {
const temp = Scanimage.scanimage._version;
Scanimage.scanimage._version = version;
const command = Scanimage.scan(request);
Scanimage.scanimage._version = temp;
return command;
}

describe('ScanimageCommand', () => {
it('scanimageVersion:1.0.27:scan', () => {
const command = commandFor('1.0.27', requestScan);
assert.ok(command.match(/.*scanimage.* > 'data\/temp\/~tmp-scan-0-ined.tif'/));
});

it('scanimageVersion:1.0.27:preview', () => {
const command = commandFor('1.0.27', requestPreview);
assert.ok(command.match(/.*scanimage.* > 'data\/preview\/preview.tif'/));
});

it('scanimageVersion:1.0.28:scan', () => {
const command = commandFor('1.0.28', requestScan);
assert.ok(command.match(/.*scanimage.* -o 'data\/temp\/~tmp-scan-0-ined.tif'/));
});

it('scanimageVersion:1.0.28:preview', () => {
const command = commandFor('1.0.28', requestPreview);
assert.ok(command.match(/.*scanimage.* -o 'data\/preview\/preview.tif'/));
});

it('scanimageVersion:1.0.31:scan', () => {
const command = commandFor('1.0.31', requestScan);
assert.ok(command.match(/.*scanimage.* -o 'data\/temp\/~tmp-scan-0-ined.tif'/));
});

it('scanimageVersion:1.0.31:preview', () => {
const command = commandFor('1.0.31', requestPreview);
assert.ok(command.match(/.*scanimage.* -o 'data\/preview\/preview.tif'/));
});
});