Hi Kai,
Sorry for the delay in getting back to you on this important topic.
PI 1.8 comes with the new NetworkTransfer core JavaScript object. Here's an example to upload a list of files to a remote FTP site:
#include <pjsr/DataType.jsh>
#include <pjsr/SeekMode.jsh>
function FileUploader()
{
this.uploadFiles = function( remoteURL, files )
{
function upload( maxSize )
{
if ( this.count < this.file.size )
{
var begin = this.count;
var end = Math.min( begin+maxSize, this.file.size );
this.count = end;
this.file.seek( begin, SeekMode_FromBegin );
return this.file.read( DataType_ByteArray, end-begin );
}
return new ByteArray;
}
var T = new NetworkTransfer;
// T.setSSL( true/*useSSL*/, true/*forceSSL*/ );
T.onUploadDataRequested = upload;
T.file = new File;
for ( var i = 0; i < files.length; ++i )
{
T.setURL( remoteURL + (new FileInfo(files[i])).nameAndSuffix, "user_name", "user_password" );
T.file.openForReading( files[i] );
T.count = 0;
if ( !T.upload( T.file.size ) )
console.writeln( T.errorInformation );
console.writeln( T.responseCode );
T.file.close();
}
}
}
var myFTPUpload = new FileUploader();
myFTPUpload.uploadFiles( "ftp://somesite.com/foo/bar/",
["/path/to/first_file.jpg", "/path/to/second_file.jpg"] );
The NetworkTransfer object supports the FTP, HTTP (including HTTP POST operations) and SMTP protocols at least. It also supports SSL/TLS. In the example above, you can enable SSL for FTP transfers by removing the comment in front of the line "T.setSSL( ...".
Note that PCL 2.0 also includes a fully functional NetworkTransfer class, for which the NetworkTransfer JS object is just a wrapper. Hence, the PCL documentation for this class will give you all the information needed to work with its JS counterpart:
http://pixinsight.com/developer/pcl/doc/html/classpcl_1_1NetworkTransfer.htmlHere is another example to send an email message from PixInsight:
#include <pjsr/DataType.jsh>
#include <pjsr/SeekMode.jsh>
function EmailSender()
{
this.upload = function( maxSize )
{
if ( this.count < this.message.length )
{
var begin = this.count;
var end = Math.min( begin+maxSize, this.message.length );
this.count = end;
return new ByteArray( this.message.substring( begin, end ) );
}
return new ByteArray;
};
this.send = function( message, from, to )
{
var T = new NetworkTransfer;
T.setURL( "smtp://mail.myhost.com", "smtp_user_name", "smtp_user_password" );
// T.setSSL( true, true );
T.onUploadDataRequested = this.upload;
T.message = message;
T.count = 0;
if ( !T.smtp( from, to ) )
console.writeln( T.errorInformation );
console.writeln( T.responseCode );
};
}
#define FROM "me@myhost.com"
#define TO "you@yourhost.com"
#define CC "she@herhost.com"
var message =
"Date: " + (new Date).toString() + "\n" +
"To: <" + TO + ">\n" +
"From: <" + FROM + ">\n" +
"Cc: <" + CC + ">\n" +
"Subject: This is a test message sent from PixInsight\n" +
"\n" + /* empty line to separate headers from body, see RFC5322 */
"The body of the message starts here.\n" +
"\n" +
"It could be a lot of lines, could be MIME encoded, whatever.\n" +
"Check RFC5322 for more information.\n";
var email = new EmailSender;
email.send( message, FROM, [TO, CC] );
An
Important note regarding SSL/TLS: In most cases you don't need a dedicated SSL certificate installed on your site or on the site you download/upload data to/from. NetworkTransfer allows you to relax strict SSL verification, so you can use a self-signed, generic certificate. For example:
var T = new NetworkTransfer;
T.setSSL( true/*useSSL*/, true/*forceSSL*/, false/*verifyPeer*/, false/*verifyHost*/ );
By disabling the "verifyPeer" and "verifyHost" features, you can use a self-signed SSL certificate, and you don't need a certificate for your particular domain. Hence, the "generic" certificates available on most hosting providers can be used. Note that a self-signed, generic certificate is perfectly valid to enable SSL encryption, which allows you to protect user names and passwords. Sending plain text passwords is *very* dangerous.