domotz-remote-pawn
Version:
Domotz Agent
59 lines (52 loc) • 2.07 kB
JavaScript
/** This file is part of Domotz Agent.
* Copyright (C) 2016 Domotz Ltd
*
* 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/>.
**/
/**
* Created by Tommaso Latini <tommaso@domotz.com> on 30/12/15.
*
* Command by remote:
* {
* "path": <str> path of the destination file
* "relative": <boolean> true if the path is relative to DOMOTZ ROOT DIR
* false if the path is absolute
* "content": <str> content of the file
* }
*
* N.B fileUtils.writeFile was designed to write small and text file
* (magnitude of KB)
*/
var saveFile = function (message, resourceLocator) {
var fileUtils = resourceLocator.utils.file;
var join = resourceLocator.path.join;
var myConsole = resourceLocator.log.decorateLogs();
var payload = message.payload;
var absPath = join(process.env.DOMOTZ_ROOT_DIR, payload.path);
var content = new Buffer(payload.content, 'base64');
return {
describe: function () {
return message.correlationId + ' - saveFile - ' + 'Path: ' + absPath + '. ' + 'Content Length: ' + content.length;
},
execute: function (callback) {
fileUtils
.writeFile(absPath, content, true)
.catch(function (err) {
myConsole.error('Error writing file %s', err.toString());
})
.finally(callback);
},
};
};
module.exports.command = saveFile;