domotz-remote-pawn
Version:
Domotz Agent
159 lines (139 loc) • 6.32 kB
JavaScript
/**
* This file is part of Domotz Agent.
*
* @license
* Domotz Agent is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Domotz Agent is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Domotz Agent. If not, see <http://www.gnu.org/licenses/>.
*
* @requires module:q
* @requires module:fs
* @requires module:child_process
* @requires module:network_tools/scp
* @requires module:persistence/file
* @requires module:utils/platform
* @copyright Copyright (C) Domotz Inc
*/
/**
* The scp upload Options
* @typedef {Object} scpUploadOptions
* @property {int} [timeout] - The time to wait for the scp upload command execution
* @property {string} filePath - The destination file path on the device. It defines where the content will be stored
* @property {string} content - The content of the file to be stored on the device
* @property {string} [port=22] - The SCP port
*/
/**
* The scp upload Execution Callback
* @callback scpUploadCallback
* @param {string} output - The stdout from the scp upload command execution
* @param {ErrorResult} [error] - Will be present if the execution resulted in an error
*/
/**
* The scp download Options
* @typedef {Object} scpDownloadOptions
* @property {int} [timeout] - The time to wait for the scp download command execution
* @property {string} filePath - The source file path on the device. It defines the location of the file to be downloaded.
* @property {string} [port=22] - The SCP port
*/
/**
* The scp download Execution Result
* @typedef scpDownloadResult
* @property {string} [content] - The content of file downloaded from the device, if the execution did not result in an error
* @property {string} stdout - The stdout from the scp download command execution
*/
/**
* The scp download Execution Callback
* @callback scpDownloadCallback
* @param {scpDownloadResult} output - The scp download execution result
* @param {ErrorResult} [error] - Will be present if the execution resulted in an error
*/
const q = require('q');
const fs = require('fs');
const childProcess = require('child_process');
const scp = require('../../network_tools/scp');
const fileUtils = require('../../persistence/file').factory(fs, q);
const platform = require('../../utils/platform').factory(fs);
var sandboxConsole;
const scpSandboxLogger = {
decorateLogs: function () {
return sandboxConsole;
},
};
/**
* Transfers a file content to a remote server using SCP.
* @function
* @private
* @readonly
* @param {scpSandboxLogger} myConsole - The Domotz Sandbox console
* @param {scpUploadOptions} options - The scp upload execution options
* @param {scpUploadCallback} callback - The scp upload callback
*/
function upload(myConsole, options, callback) {
var message;
sandboxConsole = myConsole;
sandboxConsole.verbose('scp.upload options: ' + JSON.stringify(options));
if (!options.content) {
message = "D.device.scp.upload requires a 'content' in options parameters";
sandboxConsole.error(message);
callback(undefined, { message: message });
return;
}
const scpNode = scp.factory(fileUtils, childProcess, q, platform, scpSandboxLogger);
const content = Buffer.from(options.content).toString('base64');
var promise = scpNode.exec(scpNode.WRITE, options.username, options.host, options.filePath, options.password, options, content);
return promise
.then(function (result) {
if (result.return_code === 0) {
sandboxConsole.debug('scp.upload Success: ' + JSON.stringify(result));
callback(result.stdout, undefined);
} else {
sandboxConsole.error('scp.upload Error: ' + JSON.stringify(result));
callback(result.stdout, { message: result.stderr });
}
})
.catch(function (error) {
sandboxConsole.error('scp.upload: ' + JSON.stringify(error));
callback(undefined, { message: error.message });
});
}
/**
* Retrieves a file content from a remote server using SCP.
* @function
* @private
* @readonly
* @param {scpSandboxLogger} myConsole - The Domotz Sandbox console
* @param {scpDownloadOptions} options - The scp download execution options
* @param {scpDownloadCallback} callback - The scp download callback
*/
function download(myConsole, options, callback) {
sandboxConsole = myConsole;
sandboxConsole.verbose('scp.upload options: ' + JSON.stringify(options));
const scpNode = scp.factory(fileUtils, childProcess, q, platform, scpSandboxLogger);
var promise = scpNode.exec(scpNode.READ, options.username, options.host, options.filePath, options.password, options);
return promise
.then(function (result) {
if (result.return_code === 0) {
sandboxConsole.debug('scp.download Success: ' + JSON.stringify(result));
const content = Buffer.from(result.content, 'base64').toString('utf8');
callback({ content: content, stdout: result.stdout }, undefined);
} else {
sandboxConsole.error('scp.download Error:' + JSON.stringify(result));
callback({ stdout: result.stdout }, { message: result.stderr });
}
})
.catch(function (error) {
sandboxConsole.error('scp.download Error: ' + JSON.stringify(error));
callback(undefined, { message: error.message });
});
}
module.exports.upload = upload;
module.exports.download = download;