web stats
javascript sftp file name check - Mirth Community

Go Back   Mirth Community > Mirth Connect > Support

Reply
 
Thread Tools Display Modes
  #1  
Old 12-02-2014, 09:51 AM
appsbyaaron's Avatar
appsbyaaron appsbyaaron is offline
Mirth Guru
 
Join Date: Nov 2011
Location: Florida, USA
Posts: 377
appsbyaaron is on a distinguished road
Default javascript sftp file name check

I can connect to the sftp folder but I just need to see if the file exists by the file name. It sounds easy but for some reason I just keep getting errored...



//Create Function.
function loopRun(fileName, filePath2, fileName2) {
//fileName is the location and file on the MIRTH Appliance
//filePath2 is the SFTP directory
//fileName2 is the SFTP file name
logger.info('in the function');
try {
contents1 = FileUtil.read(fileName);

//Get the File Size
contents2 = FileUtil.readBytes(fileName);
kb1 = contents2.length / 1024;

FileUtil.write(goodFile, true, fileName+'\n');

//If the backup file is found then check to see if the SFTP move file is there.
//Connect to SFTP
/* The file checks on all SFTP folders fails because we are not connecting to them. */
logger.info('About to connect');

session.connect();
var channel = session.openChannel('sftp');
channel.connect();
logger.info('Connected');
channel.cd(filePath2); //Go to the folder for the file.
logger.info('Changed path to : '+filePath2);

try {
logger.info('read file : '+fileName2); //never comes up so I know it fails here
var contents3 = FileUtil.read(fileName2);
FileUtil.write(goodFile, true, filePath2+fileName2+'\n');

//Get the File Size
contents4 = FileUtil.readBytes(fileName2);
kb2 = contents4.length / 1024;
logger.info('contents4 : '+contents4);

//If the file sizes are different then make a note of it.
if (kb1 != kb2) {
fileName = fileName + kb1;
fileName2 = fileName2 + kb2;
FileUtil.write(differentFile, true, fileName+'\n');
FileUtil.write(differentFile, true, filePath2+fileName2+'\n\n');
}
}
catch(err) {
logger.info('Err 1');

FileUtil.write(missingFile, true, filePath2+fileName2+'\n');
//Close SFTP Connection
channel.exit();
session.disconnect();
}
}
catch(err) {
logger.info('Err 2');
FileUtil.write(missingFile, true, fileName+'\n');
//Close SFTP Connection
channel.exit();
session.disconnect();
}
//Close SFTP Connection
channel.exit();
session.disconnect();
logger.info('Clean up');

//Clean up
fileName = '';
fileName2 = '';
contents1 = '';
contents2 = '';
contents3 = '';
contents4 = '';

kb1 = 0;
kb2 = 0;
}
Reply With Quote
  #2  
Old 12-02-2014, 10:08 AM
cory_cole cory_cole is offline
Mirth Guru
 
Join Date: Mar 2012
Posts: 1,360
cory_cole is on a distinguished road
Default

Your second try does not have a catch. Your best bet is to find out what the error is.
Reply With Quote
  #3  
Old 12-02-2014, 10:15 AM
appsbyaaron's Avatar
appsbyaaron appsbyaaron is offline
Mirth Guru
 
Join Date: Nov 2011
Location: Florida, USA
Posts: 377
appsbyaaron is on a distinguished road
Default

Quote:
Originally Posted by cory_cole View Post
Your second try does not have a catch. Your best bet is to find out what the error is.
The catch for the second Try is the logger.info('Err 1'); highlighted in red.

How can I get the actual error?
Reply With Quote
  #4  
Old 12-02-2014, 10:21 AM
appsbyaaron's Avatar
appsbyaaron appsbyaaron is offline
Mirth Guru
 
Join Date: Nov 2011
Location: Florida, USA
Posts: 377
appsbyaaron is on a distinguished road
Default

here is the logger messages...
Attached Images
File Type: png filecheck1.png (9.9 KB, 7 views)
Reply With Quote
  #5  
Old 12-02-2014, 10:22 AM
cory_cole cory_cole is offline
Mirth Guru
 
Join Date: Mar 2012
Posts: 1,360
cory_cole is on a distinguished road
Default

try {
logger.info('read file : '+fileName2); //never comes up so I know it fails here
var contents3 = FileUtil.read(fileName2);
FileUtil.write(goodFile, true, filePath2+fileName2+'\n');

//Get the File Size
contents4 = FileUtil.readBytes(fileName2);
kb2 = contents4.length / 1024;
logger.info('contents4 : '+contents4);

//If the file sizes are different then make a note of it.
if (kb1 != kb2) {
fileName = fileName + kb1;
fileName2 = fileName2 + kb2;
FileUtil.write(differentFile, true, fileName+'\n');
FileUtil.write(differentFile, true, filePath2+fileName2+'\n\n');
}//This ends the second try
}//This ends the first try
catch(err) {//Therefore this is the catch for the first try
logger.info('Err 1');





try {
logger.info('read file : '+fileName2); //never comes up so I know it fails here
var contents3 = FileUtil.read(fileName2);
FileUtil.write(goodFile, true, filePath2+fileName2+'\n');

//Get the File Size
contents4 = FileUtil.readBytes(fileName2);
kb2 = contents4.length / 1024;
logger.info('contents4 : '+contents4);

//If the file sizes are different then make a note of it.
if (kb1 != kb2) {
fileName = fileName + kb1;
fileName2 = fileName2 + kb2;
FileUtil.write(differentFile, true, fileName+'\n');
FileUtil.write(differentFile, true, filePath2+fileName2+'\n\n');
}
catch(err){
logger.info(err);//may need err.toString();
}
}
catch(err) {
logger.info('Err 1');
Reply With Quote
  #6  
Old 12-02-2014, 10:36 AM
appsbyaaron's Avatar
appsbyaaron appsbyaaron is offline
Mirth Guru
 
Join Date: Nov 2011
Location: Florida, USA
Posts: 377
appsbyaaron is on a distinguished road
Default

...the second Try is within the main Try.

Try-ception Sorry the attached image is crap...the limitations on the image attachments won't let me include a better pic.
Attached Images
File Type: png nested_try2.png (22.4 KB, 6 views)
Reply With Quote
  #7  
Old 12-02-2014, 10:56 AM
sarcophyton sarcophyton is offline
OBX.2 Kenobi
 
Join Date: Jan 2012
Posts: 67
sarcophyton is on a distinguished road
Default

You might find this code useful.



importPackage(com.jcraft.jsch);
importPackage(Packages.java.io);

var xmlMsg = '';
var list = new Packages.java.util.ArrayList();


try {
var folder = new File($('localPath'));
var flist = folder.listFiles();

if (flist.length < 1){ // logger.info("No files to send");
return list;
}

// Only send file if size hasn't changed since last poll
var pson = globalChannelMap.get('fson');
var fson = [];
var fmatches = [];
for (var i = 0; i < flist.length; i++) {
var fobj = {};
fobj.fname = flist[i].getName(); //logger.info("fname: "+fobj.fname);
fobj.fsize = flist[i].length();
fson.push(fobj);
for (var j=0; j < pson.length; j++) {
var pobj = pson[j];
if ((fobj.fname == pobj.fname) && (fobj.fsize == pobj.fsize)) {
fmatches.push(flist[i]); // logger.info("match: "+fobj.fname);
}
}
}
globalChannelMap.put('fson',fson);

if (fmatches.length < 1) { // logger.info("No matches");
return list;
}


var jsch = new JSch();
jsch.setConfig('StrictHostKeyChecking','no');
var session = jsch.getSession('username','mysftp.site.com',22);
session.setTimeout(20000);
session.setPassword('password');
session.connect();
var channel = session.openChannel('sftp');
channel.connect();

var sftpChannel = new ChannelSftp();
sftpChannel = channel;

for (var k = 0; k < fmatches.length; k++) {

var fmatch = fmatches[k];
var fname = fmatch.getName(); //logger.info("sending: "+fname);
var fsize = fmatch.length();
var dotPos = fname.indexOf('.hl7');
var outfname = 'FILE_' + fname.slice(0,dotPos) + '_' + DateUtil.getCurrentDate('yyyyMMddHHmmss').toString () + '.hl7';

try {
sftpChannel.put($('localPath') + fname, $('sftpPath') + outfname);

// move file to processed path
fmatch.renameTo(new File($('processedPath'), outfname));

//logger.info('sent');
xmlMsg = '<result><File>' + fname + '</File><Size>' + fsize + '</Size><Message>Success</Message></result>';
list.add(xmlMsg);
} catch(sftperr) {
logger.error('Error handled in SFTP Write! Mirth says: ' + sftperr.toString());
xmlMsg = '<result><File>' + fname + '</File><Size>' + fsize + '</Size><Message><![CDATA[' + sftperr.toString() + ']]></Message></result>';
list.add(xmlMsg);
}
}
sftpChannel.exit();
session.disconnect();

} catch(err) {

logger.error('Error handled in SFTP Write! Mirth says: ' + err.toString());
xmlMsg = '<result><Message><![CDATA[' + err.toString() + ']]></Message></result>';
list.add(xmlMsg);

}

return list;
Reply With Quote
  #8  
Old 12-02-2014, 10:57 AM
appsbyaaron's Avatar
appsbyaaron appsbyaaron is offline
Mirth Guru
 
Join Date: Nov 2011
Location: Florida, USA
Posts: 377
appsbyaaron is on a distinguished road
Default

Ok I see that the "var contents3 = FileUtil.read(fileName2);" is the issue. when I comment this out it continues further without issue.

"contents4 = FileUtil.readBytes(fileName2);" also fails.

Again, I just want to check to see if the FILE exists and see what the file size is. I don't really need to ready the whole thing.
Reply With Quote
  #9  
Old 12-02-2014, 11:03 AM
cory_cole cory_cole is offline
Mirth Guru
 
Join Date: Mar 2012
Posts: 1,360
cory_cole is on a distinguished road
Default

Using java.io.File

File f = new File(filePathString);
if(f.exists() && !f.isDirectory())


f.length will return the size in bytes.
Reply With Quote
  #10  
Old 12-03-2014, 04:47 AM
appsbyaaron's Avatar
appsbyaaron appsbyaaron is offline
Mirth Guru
 
Join Date: Nov 2011
Location: Florida, USA
Posts: 377
appsbyaaron is on a distinguished road
Default

Quote:
Originally Posted by cory_cole View Post
Using java.io.File

File f = new File(filePathString);
if(f.exists() && !f.isDirectory())

f.length will return the size in bytes.
That's not working either. I made sure that I have read acccess on the sftp account that I am using. I can connect via FileZilla and access the file.

Does MIRTH use Java (java.io.File) natively? I thought I could use any java and javascript within a Javascript writer destination.
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -8. The time now is 07:21 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2020, vBulletin Solutions, Inc.
Mirth Corporation