Skip to content

Commit

Permalink
Add afterScan hook for user defined code
Browse files Browse the repository at this point in the history
  • Loading branch information
sbs20 committed Nov 30, 2022
1 parent ceec01f commit e4ce5f0
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 6 deletions.
11 changes: 11 additions & 0 deletions packages/server/config/config.default.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,5 +110,16 @@ module.exports = {
// device.features['-x'].default = 215;
// device.features['-y'].default = 297;
// }
},

/**
* This method is called after a scan has completed with the resultant
* FileInfo.
* @param {FileInfo} fileInfo
* @returns {Promise.<any>}
*/
async afterScan(fileInfo) {
// Copy the file to the home directory
// return await Process.spawn(`cp '${fileInfo.fullname}' ~/`);
}
};
2 changes: 1 addition & 1 deletion packages/server/src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class Config {
constructor() {
this.init();
this.addEnvironment();
userOptions.applyToConfig(this);
userOptions.afterConfig(this);
}

init() {
Expand Down
1 change: 1 addition & 0 deletions packages/server/src/configure.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ function logRequest(req) {
*/
function initialize(rootPath) {
if (rootPath) {
log.warn(`Running with altered rootPath: ${rootPath}`);
// Only required for running in development
Object.assign(Config, {
devicesPath: rootPath + Config.devicesPath,
Expand Down
2 changes: 1 addition & 1 deletion packages/server/src/devices.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class Devices {
file.save(JSON.stringify(devices.map(d => d.string), null, 2));
}

userOptions.applyToDevices(devices);
userOptions.afterDevices(devices);
return devices;
}

Expand Down
2 changes: 2 additions & 0 deletions packages/server/src/scan-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const Filters = require('./filters');
const Process = require('./process');
const Request = require('./request');
const Scanimage = require('./scanimage');
const userOptions = require('./user-options');
const Util = require('./util');

class ScanController {
Expand Down Expand Up @@ -170,6 +171,7 @@ class ScanController {

if (this.finishUp) {
const file = await this.finish();
await userOptions.afterScan(file);
return {
file
};
Expand Down
19 changes: 15 additions & 4 deletions packages/server/src/user-options.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,35 @@ class UserOptions {
}

/**
* Applies user overrides from config.local.js to the configuration
* Passes a config reference to config.local.js for customisation
* @param {Configuration} config
*/
applyToConfig(config) {
afterConfig(config) {
if (this.local && this.local.afterConfig) {
this.local.afterConfig(config);
}
}

/**
* Applies user overrides from config.local.js to the devices
* Passes a devices reference to config.local.js for customisation
* @param {ScanDevice[]} devices
*/
applyToDevices(devices) {
afterDevices(devices) {
if (this.local && this.local.afterDevices) {
this.local.afterDevices(devices);
}
}

/**
* Passes a fileInfo of the scan result to config.local.js
* @param {FileInfo} fileInfo
* @returns {Promise.<any>}
*/
async afterScan(fileInfo) {
if (this.local && this.local.afterScan) {
return await this.local.afterScan(fileInfo);
}
}
}

const userOptions = new UserOptions();
Expand Down

0 comments on commit e4ce5f0

Please sign in to comment.