node.js how to read file in stream from remote server - node.js

In node.js I am trying to watch and read a text file on filechange.For this, I am using below code and it is working absolutely fine.
var fs = require('fs'),bite_size = 256,readbytes = 0,file;
fs.open('testread.txt', 'r', function(err, fd) {
file = fd; //readsome();
var func = (function readsome() {
var stats = fs.fstatSync(file); // yes sometimes async does not make sense!
if(stats.size<readbytes+1) {
setTimeout(readsome, 1000);
}
else {
fs.read(file, new Buffer(bite_size), 0, bite_size, readbytes, function (err, bytecount, buff) {
console.log(buff.toString('utf-8', 0, bytecount));
readbytes+=bytecount;
process.nextTick(readsome);
});
}
})();
});
But this works only for local file and now I need to read text file from remote server ( may be linux or windows) in same way.
Please help , how can I use remote server login information like below
IP address = "XXX.XXX.XX.XX";,
username = username,
password = password,
filepath = "/root/testread.txt" or "D:\testread.txt"
and read the data from text file present at filepath in remote server.

fs uses the local filesystem.
To access remote files you need to use some network protocol (NTFS for Windows and ssh or ftp for linux for example)
Those protocols expose files to remote access, you cannot access the filesystem of a remote host not using them

Related

How can I transfer a file from Gmail using an app script by POSTing to a remote server

I'm using google apps scripting to write some addons to link gmail (or g suite email) to our crm system.
My current issue is trying to take a selected attachment, POST to a php script on a remote server which recreates the file (and goes from there to integrate into our crm).
Details of the code etc is below though in essence the file obviously isn't transferring correctly so something is wrong in the way the file is being received, or saved locally. The filesize is different and it won't open. So for example I select a PDF in the email which is 640k and the resulting file created on my server is over 1mb
I've setup my script with a card which includes a radio list of attachments. A button in the card attempts to post the file contents to the remote server where a receiving PHP script just saves it as a local file.
Getting attachment list to produce radio buttons:
var attachmenttosave = CardService.newSelectionInput().setType(CardService.SelectionInputType.RADIO_BUTTON).setTitle("Selected Attachment").setFieldName("attachmenttosave");
for(var i = 0; i < attachments.length; i++) {
var attachment=attachments[i];
attachmenttosave.addItem(attachment.getName(),i,false);
}
code which gets the attachment and POSTs to remote server:
var accessToken = e.messageMetadata.accessToken;
var accessToken = e.messageMetadata.accessToken;
GmailApp.setCurrentMessageAccessToken(accessToken);
var messageId = e.messageMetadata.messageId;
var message = GmailApp.getMessageById(messageId);
var atc=e['formInput'].attachmenttosave;
var attachments=message.getAttachments();
var fn=attachments[parseInt(atc)].getName();
var blob=attachments[parseInt(atc)].getAs(attachments[parseInt(atc)].getContentType());
var url="https://remote.server";
var payload={"typ":"GS_SaveAttachmentToCase","email":encodeURIComponent(user),"fn":encodeURIComponent(fn), 'att':(blob)};
var options={"method":"POST","payload":payload,"followRedirects":true,"muteHttpExceptions":true};
var result=UrlFetchApp.fetch(url, options);
var data = result.getContentText();
return CardService.newActionResponseBuilder().setNotification(CardService.newNotification().setText(data).setType(CardService.NotificationType.INFO)).build();
Receiving PHP script:
`
function GS_SaveAttachmentToCase(){
$fn=urldecode($_POST['fn']);
$fp = fopen("GS/" .$fn, "w+");
fwrite($fp, (utf8_decode ($_POST['att'])));
fclose($fp);
echo "done";
}
`
ok did some more research and solved this.
get the attachment contents using
blob=attachments[parseInt(atc)].getBytes();
in the receiving php script
fwrite($fp, (base64_decode ($_POST['att'])));

Accessing sqlite databases on remote machine with static adress (Electron / Nodejs / fs)

I am trying to access an sqlite file on a local area network machine located in "sqlite" folder, on windows file explorer I access it using the IP address "192.168.5.15/sqlite/db.sqlite", however when the electron aplication tries to access it, the path is converted to the local machine path "c:/application ....../192.168 ....".
please how can I just point the file directly using the static IP adress without converting it to local machine path?
here is the code:
// login_app.js
var route = require('../../libs/router.js'); //my lib
var dbPath = "192.168.5.15/sqlite/db.sqlite" // here is the path i want to keep it static
function login() {
var email = document.getElementById("email").value;
var password = document.getElementById("password").value;
if(email == "" || password == ""){
alert("Please Enter Your email & Password ...");
return;
} else{
var sql = require('sql.js'); //Import sqLite
var fs = require("fs"); //Import Files driver
var SHA256 = require("crypto-js/sha256"); //Crypting Library
try {
var filebuffer = fs.readFileSync(dbPath); //here is the problem it converts the file to local machine path c:/..../192....
if (filebuffer != null) {
var db = new SQL.Database(filebuffer);
} else {
var db = new SQL.Database();
console.log("No Database");
}
// Prepare an sql statement
var stmt = db.prepare("SELECT * FROM users WHERE email=:emailid");
// Bind values to the parameters and fetch the results of the query
var result = stmt.getAsObject({':emailid' : email});
if(result.password == null) alert('Account not existing, please contact Your Administrator ...');
else{
if(SHA256(password) == result.password) {
route.data.relocate('dashboard');
}
else alert('Wrong Login credentials, please try again ...');
}
} catch (e) {
console.log(e);
}
}
}
as suggested by GregHNZ var dbPath = "//192.168.5.15/sqlite/db.sqlite" worked, so just used double slash.
Thanks GregHNZ

Bluemix Nodejs FileTransferStep, documentation

I am a newbie to bluemix. I downloaded the client libraries. But I don't see API docs for Javascript. Where do I find that? How do I go about calling several of javascript functions which is neither in the nodejs client libs nor I could find it online?
about the Workload service call you have to edit your package.json file
to add a dependency on the iws-light module using an https link, as follows
"dependencies": {
"iws-light": "https://start.wa.ibmserviceengage.com/bluemix/iws-light.tgz"
}
then you have to open your shell, go to the root of your app and run:
npm install
after this you can require the Workload Scheduler service in your application:
var ws = require("iws-light");
and create a connection to Bluemix:
//retrieve service URL from Bluemix VCAP_SERVICES...
var wsConn;
if(process.env.VCAP_SERVICES) {
wsConn = ws.createConnection();
} else {
//...or set it on your own(if you're working in local)
var url = "your workload scheduler url";
wsConn = ws.createConnection(url);
}
//retrieve cloud agent
var agentName;
wsConn.getCloudAgent(function(data) {
agentName = data;
});
//set your timezone
wsConn.setTimezone({timezone: "Europe/Rome"}, function(err, data){
if(err){
console.log(err);
}
});
now you're ready to use the lib and create a process
and add to it a FileTransferStep:
//create a process
var process = new ws.WAProcess("ProcessName", "This process transfer a file every day from local to remote server");
//supported operations are ws.steps.FileTransferStep.OperationDownload or ws.steps.FileTransferStep.OperationUpload
var operation = ws.steps.FileTransferStep.OperationUpload;
//create FileTransferStep
var ftStep = new ws.steps.FileTransferStep(agentName, operation);
//supported protocols are AUTO, FTP, FTPS, SSH, WINDOWS;
ftStep.setProtocol(ws.steps.FileTransferStep.ProtocolAuto);
//set local file
var local = {
path: "local file path",
user: "local username",
password: "local password"
};
ftStep.setLocalFile(local.path, local.user, local.password);
//set remote file
var remote = {
path: "remote file path",
user: "remote username",
password: "remote password",
server: "remote server"
};
ftStep.setRemoteFile(remote.server, remote.path, remote.user, remote.password);
//the binary mode flag: true if it uses FTP binary mode
var binaryMode = true;
the passive mode flag: true if it uses FTP passive mode
var passiveMode = true;
//set timeout
var timeout = 5;
ftStep.setMode(binaryMode, passiveMode , timeout);
//add FileTransferStep to the process
process.addStep(ftStep);
//create a trigger
var trigger = new ws.TriggerFactory.everyDayAt(1, 7, 30);
//add Trigger to the process
process.addTrigger(trigger);
process.tasklibraryid = "your task library id";
//create and enable process
wsConn.createAndEnableProcess(process, function(err, data){
if(err){
console.log(error);
} else{
console.log("process created and enabled");
}
});
The code above creates a process using a file transfer step from node.js code, however I'm not sure if this is what you actually need.
If you can explain the scenario you are trying to implement, I can be more precise about which is the best way to implement this scenario using Workload Scheduler service.
Regards,
Gabriele

Windows 10 App - File downloading

I am working with Windows 10 universal app and i want to download a file in that. The file link to Sharepoint server. I have passed token in headr to a web service and then service returned byte array to my WinJS.
Now i want to save the file, how can i do this? I tried several code samples but not working.
var folder = Windows.Storage.ApplicationData.current.localFolder;
folder.createFileAsync("document.docx", Windows.Storage.CreationCollisionOption.replaceExisting).then(function (file) {
return Windows.Storage.FileIO.writeTextAsync(file, result.response);
}).then(function () {
//saved
});
I am using above code and it is creating new file but no content is placed there. Please suggest what to do.
You never open the file for WriteAccess. I have included code from my working app. First do this command
StorageFile ageFile = await local.CreateFileAsync("Age.txt", CreationCollisionOption.FailIfExists);
then do this:
// Get the file.
var ageFile = await local.OpenStreamForWriteAsync("Age.txt",CreationCollisionOption.OpenIfExists);
// Read the data.
using (StreamWriter streamWriter = new StreamWriter(ageFile))
{
streamWriter.WriteLine(cmbAgeGroup.SelectedIndex + ";" + DateTime.Now);
streamWriter.Flush();
}
ageFile.Dispose();

direct (non-tcp) connection to redis from nodejs

hello all
I looked at the at the redis-node-client source (relevant part is shown bellow) and I see that it connects to redis via the 'net' package, which is TCP based.
line 370
exports.createClient = function (port, host, options) {
var port = port || exports.DEFAULT_PORT;
var host = host || exports.DEFAULT_HOST;
var client = new Client(net.createConnection(port, host), options);
client.port = port;
client.host = host;
return client;
};
I was wondering if there's a more direct client for redis, preferably via domain-sockets or something of that sort. Im using redis localy, as cache, without going over the wire so its unnecessary to encode/decode messages with TCP headers...
thank you
Unix Domain Socket support appears to have landed in Redis as of Nov 4th.
http://code.google.com/p/redis/issues/detail?id=231
To connect to a Unix Domain Socket, you need to supply the pathname to net.createConnection. Maybe something like this in redis-node-client:
exports.createSocketClient = function (path, options) {
var client = new Client(net.createConnection(path), options);
client.path = path;
return client;
};

Resources