Announcement

Collapse
No announcement yet.

SMB in JavaScript Reader

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • SMB in JavaScript Reader

    Has anyone delved into coding a successful SMB connection in the JavaScript Reader? If so, can you share some examples please?

    Thanks!
    Matt

  • #2
    mirth uses jcifs for smb. You can probably find some samples on stack overflow using the library.

    Comment


    • #3
      I have scrapped using SMB all together in Mirth in favor of another routine, accomplishing the same thing with very minimal overhead.

      Comment


      • #4
        Hello,


        below is some code i made.
        It basically get a file from a share and copy to the local machine.


        Code:
        var auth = new Packages.jcifs.smb.NtlmPasswordAuthentication(null, "username", "password");
        
        try
        {
            var remoteFile  = new Packages.jcifs.smb.SmbFile( new java.lang.String("smb://ip/xx/xx/xx.pdf"), auth);
        
            if(remoteFile.exists())
            {
                var destination = "C:/xx/xx.pdf";
            
                var localFile = java.nio.file.Paths.get(destination);
                var inputStr=remoteFile.getInputStream();
                java.nio.file.Files.copy(inputStr, localFile,java.nio.file.StandardCopyOption.REPLACE_EXISTING);
                
                inputStr.close();    
            }
            else
                logger.error("File " + FilePath + " does not exist!");
            
        }
        catch(err)
        {
        
        }
        Best Regards,
        Alex Neiva

        Comment


        • #5
          SmbFileConnection

          We use the functions below, for example to read PDFs linked in HL7 ORU / MDM files into the channel-attachment.

          For large files, we have an implementation with inputStream, just moving the file and not storing it in mirth.

          Code:
          /**
          	Lesen einer Datei via SMB-Verbindung
          
          	@param {String} smbShare - ServerName
          	@param {String} smbDirectory - FileDirectory
          	@param {String} smbFile - FileName
          	@param {String} smbUsername - smbDomain\\smbUserName
          	@param {String} smbPassword - smbPassword
          	@param {Number} smbTimeout - Timeout in milliseconds
          	@param {String} smbSchemeProperties - smbSchemeProperties default null
          	@return {String} File as String (nicht Base64)
          */
          function ctReadFileFromSMBPlain_03(smbShare, smbDirectory, smbFile, smbUsername, smbPassword, smbTimeout, smbSchemeProperties) {
          	
          	// http://www.mirthcorp.com/community/forums/showthread.php?t=6738&highlight=smb+javascript - MC v2.	- SmbFileConnection(strHost, strUsername, strPassword)
          	// http://www.mirthcorp.com/community/forums/showthread.php?t=15391 - MC v3.1.x - v3.2.x				- SmbFileConnection(String share, String domainAndUser, String password, int timeout)
          	// MC v3.3.x																			- SmbFileConnection(String share, FileSystemConnectionOptions fileSystemOptions, int timeout)
          	try {
          		// connect to cifs server and get pdf file
          		var smbFileSystemOptions = new com.mirth.connect.connectors.file.FileSystemConnectionOptions(smbUsername, smbPassword, smbSchemeProperties);
          		var client = new com.mirth.connect.connectors.file.filesystems.SmbFileConnection(smbShare, smbFileSystemOptions, smbTimeout);
          
          		var inputStream = client.readFile(smbFile, smbDirectory);
          		//var inputByteArr = org.apache.commons.io.IOUtils.toByteArray(inputStream);
          		var inputString = org.apache.commons.io.IOUtils.toString(inputStream);
          		inputStream.close();
          
          		//pdfContentBase64 = FileUtil.encode(inputByteArr);
          		//return pdfContentBase64;
          		return inputString;
          		
          	} catch (err) {
          		throw(err);
          	} finally {
          		client.destroy();
          	}
          }
          Code:
          /**
          	Lesen einer Datei via SMB-Verbindung
          
          	@param {String} smbShare - ServerName
          	@param {String} smbDirectory - FileDirectory
          	@param {String} smbFile - FileName
          	@param {String} smbUsername - smbDomain\\smbUserName
          	@param {String} smbPassword - smbPassword
          	@param {Number} smbTimeout - Timeout in milliseconds
          	@param {String} smbSchemeProperties - smbSchemeProperties default null
          	@return {String} File as Base64 String
          */
          function ctReadFileFromSMBBase64_03(smbShare, smbDirectory, smbFile, smbUsername, smbPassword, smbTimeout, smbSchemeProperties) {
          	
          	// http://www.mirthcorp.com/community/forums/showthread.php?t=6738&highlight=smb+javascript - MC v2.	- SmbFileConnection(strHost, strUsername, strPassword)
          	// http://www.mirthcorp.com/community/forums/showthread.php?t=15391 - MC v3.1.x - v3.2.x				- SmbFileConnection(String share, String domainAndUser, String password, int timeout)
          	// MC v3.3.x																			- SmbFileConnection(String share, FileSystemConnectionOptions fileSystemOptions, int timeout)
          	try {
          		// connect to cifs server and get pdf file
          		var smbFileSystemOptions = new com.mirth.connect.connectors.file.FileSystemConnectionOptions(smbUsername, smbPassword, smbSchemeProperties);
          		var client = new com.mirth.connect.connectors.file.filesystems.SmbFileConnection(smbShare, smbFileSystemOptions, smbTimeout);
          
          		var inputStream = client.readFile(smbFile, smbDirectory);
          		var inputByteArr = org.apache.commons.io.IOUtils.toByteArray(inputStream);
          		inputStream.close();
          
          		pdfContentBase64 = FileUtil.encode(inputByteArr);
          		return pdfContentBase64;
          		
          	} catch (err) {
          		throw(err);
          	} finally {
          		client.destroy();
          	}
          }
          Code:
          /* Implementierungsnotizen
             Im Javascript-Transformer werden Dateien via Inputstream/Outputstream byteweise verschoben.
             Die Implementierung baut darauf, als Delimited-Nachricht die Liste der Dateien zu erhalten:
             z.B. 
             ssi0127,daten_dev$/to/,datei1.csv,ssi0127,daten_dev$/from/,Export.csv
             Die Implementierung funktioniert fuer verschiedene Server aber nur fuer SMB-Shares
          */
          
          // SMB User and Password
          smbUsername = configurationMap.get('sks_user');//'domain\user';
          smbPassword = configurationMap.get('sks_user_pwd');//'**';
          
          // Variablen
          var smbSchemeProperties = null;
          var smbTimeout = '120000';
          
          var smbFileSystemOptions = new com.mirth.connect.connectors.file.FileSystemConnectionOptions(smbUsername, smbPassword, smbSchemeProperties);
          
          // Dateien kopieren
          for each (row in msg['row']) {
          	try {
          		// Quelle:
          		var smbShare = row['srcSrv'].toString(); //'ssi0127';
          		var smbFile = row['srcFile'].toString(); //'DownloadHospindex_20171112.zip';
          		var smbDirectory = row['srcDir'].toString(); //'/daten_dev$/kgr/emedista/kisim/to_int/';
          		var smbConn = new com.mirth.connect.connectors.file.filesystems.SmbFileConnection(smbShare, smbFileSystemOptions, smbTimeout);
          		
          		// Ziel:
          		var smbShare2 = row['destSrv'].toString(); //'ssi0127';
          		var smbFile2 = row['destFile'].toString();//DownloadHospindex.zip
          		var smbDirectory2 = row['destDir'].toString(); //'/daten_dev$/kgr/emedista/kisim/test/';
          		var smbConn2 = new com.mirth.connect.connectors.file.filesystems.SmbFileConnection(smbShare2, smbFileSystemOptions, smbTimeout);
          	
          		if (smbConn.exists(smbFile, smbDirectory)) {
          			// InputStream
          			var myInputStream = smbConn.readFile(smbFile, smbDirectory);		//read File
          			// -> OutputStream
          			smbConn2.writeFile(smbFile2 + '.tmp', smbDirectory2, false, myInputStream);	//copy File //Append false
          			// Variablen schliessen
          			myInputStream.close();
          			
          			// tmp-Datei umbenennen
          			smbConn2.move(smbFile2 + '.tmp', smbDirectory2, smbFile2, smbDirectory2);
          		}
          	}
          	catch(e) {}
          	finally {
          		// Variablen schliessen
          		smbConn.destroy();
          		smbConn2.destroy();
          	}
          }

          Comment

          Working...
          X